473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Calling C Library DLLs from C Sharp

Dear All,

I am trying to call the data type unsigned char * included in the C DLL and whole structure from C Sharp.
My draft code is below

Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
  2.         public struct Converted
  3.         {
  4.             public int width;
  5.             public int height;
  6.             public IntPtr Buf;  // which is unsigned char* in C and is it           correct ?
  7.             //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
  8.  
  9.         };
  10.  
  11. Converted c = new Converted();
  12. [DllImport("Test.dll", CharSet = CharSet.Auto, EntryPoint="Loading")]
  13. public static extern Converted Loading(ref IntPtr buf, int size, int type); // shoul I use ref?
  14.  
  15. i.data is syste.array
  16. c.Buf = Loading(ref i.data, i.size, type);
  17.  
  18.  
I encountered
1. to pass unsigned char * from C to C#
2. return structure of Converted from C to C#
3. System.array of C# to IntPtr of C# or unsigned char *

I hope some one will advise me to get it well for my dll and looking forward to hearign from you soon.


Thanks in advance
Mar 18 '10 #1
2 2418
Dear All,

I am trying to call the data type unsigned char * included in the C DLL and whole structure from C Sharp.

My draft code is below

Expand|Select|Wrap|Line Numbers
  1. // dll.cpp
  2. // Image Display and BufferMemory
  3. typedef
  4. struct ConvertedImage 
  5. {
  6. int height; 
  7. int size; 
  8. unsigned char *greyBMPBuf; 
  9. }ConvertedImage; 
  10.  
  11. MATCH_IMPORTS ConvertedImage Convert2BMPGrey(unsigned char *bufFromDevice, int size, int image_type);
  12.  
  13. // main.cs
  14. [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)] 
  15. public struct ConvertedImage 
  16. {
  17. public int width; 
  18. public int height; 
  19. public int size; // size = imagesize is imagewidth * imgaeheight 
  20. [MarshalAs(UnmanagedType.LPArray)] 
  21. public byte[] greyBMPBuf; 
  22. };
  23.  
  24. ConvertedImage cImage; 
  25.  
  26. [DllImport("Match.dll", CharSet = CharSet.Auto, EntryPoint = "Convert2BMPGrey")] 
  27. public static extern ConvertedImage Convert2BMPGrey([MarshalAs(UnmanagedType.LPArray)]byte[] bufImage, int size, int image_type); 
  28.  
I encountered
1. to pass unsigned char * from C to C#
2. return structure of Converted from C to C#
3. System.array of C# to IntPtr of C# or unsigned char *
4. the error message"Method's type signature is not PInvoke compatible"

Please advise me for my dll and C# InteropServices and looking forward to hearing from you good news.


Thanks in advance
Mar 19 '10 #2
tlhintoq
3,525 Expert 2GB
Please don't double-post your questions. It divides attempts to help you in an organized and cohesive manner. Your threads have been merged
Mar 19 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: jr | last post by:
I am working in VC++ so this may be microsoft specific though I don't think so. Excuse me if it is. Assuming it isn't, can someone generally explain the purpose of having both - is it purely a...
3
by: mdaetwyler | last post by:
Hi all I am trying to call a DB/2 v8.2 stored procedure from Perl DBI and am getting an error message telling me, that the routine could not be found in the library path. SQL0444N Routine...
3
by: Kenric Li | last post by:
Hello, May I know how I can call a calss from a dll implemented by C#, from Visual C++.net? There is no header file that comes with it. But a documentation with the list of the class methods...
6
by: Amjad | last post by:
Hi, I want to make a project that calls and executes a function (VB code) made in a seperate file in the Application Folder. I know I can create the function in my project and call it internally,...
2
by: Daniel Lidström | last post by:
I'm using a library called fyba. This library reads and writes files in a format called sosi. fyba uses the following code to determine if the calling process has own methods to handle errors,...
46
by: Usenet User | last post by:
I need to be able to call the same API function from VB, which will reside in different DLLs. While all of the functions have the same signature and name, DLL file names are not known at compile...
4
by: yhebib | last post by:
Hello All, I've been browsing and reading all articles I could find on technet ,msdn and other knowledgeable sources to understand the issue I'm dealing with. However, I did not find so far how...
6
by: Frederick Gotham | last post by:
I gather that, rather than the C++ Standard defining the functionality of the Standard Library features which it has in common with C, it makes reference to a C Standard which defines their...
2
by: =?Utf-8?B?VmljdG9yIExhaQ==?= | last post by:
Hi, I wrote an simple application using VS2005 C#. I am trying to call some functions in my other dll file. This dll is written in EVC for running on Windows CE 5.0. This dll file has the...
6
by: Even | last post by:
Hi all, As far as I know, relative address will be assigned by program at link time.Right? Ok, here we go. So if we use a static library, it will give the program an relative address(i mean...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.