figuring out c# declarations from a DLL of C functions

T

Ted Sung

Hi,

I'm trying to use a C function ( long int myFunc() ) from a DLL in my
C# program. I use DllImport("dllname") a C function and declare it as

[DllImport("dllname")]
public static extern int myFunc();

At first I used

public static extern long myFunc();

but that returned a bizarre value. I incorrectly assumed a C long int
mapped
to C# long. Using C# int worked. But here's my question -

Since the DLL is known, is there a tool that will list the C#
equivalents for
the functions as long as the variable types are standard (i.e., int,
long, float, etc. )? Does it seems reasonable that C# should be able
to gather this info from the DLL?

Finally, what is the name of the process of using the [DllImport...]
specification for loading external functions?
Is this PInvoke?
Are there any books/articles describing this in detail, particularly
with
accessing and running C functions that return pointers to structures.

Thanks,

Ted
 
G

Guest

yep, it's Platform Invoke. look in your .net sdk documentation, look under P/Invoke, it contains a standard C/C++ datatype to C# equivalent table, and also a lot of information on more complicated string, array, struct/union, input/output, callback, GC related marshalling topics

I'm also looking for ways to make the process easier. Dumpbin doesn't supply a whole lot of information (maybe I don't know how to use it, I don't know how much information is actually included in the DLLs). it's a pain to dig up information on poorly documented C APIs, like the original typedefs, when you only have the binary dlls to work with, not the C header files

----- Ted Sung wrote: ----

Hi

I'm trying to use a C function ( long int myFunc() ) from a DLL in m
C# program. I use DllImport("dllname") a C function and declare it a

[DllImport("dllname")
public static extern int myFunc()

At first I used

public static extern long myFunc()

but that returned a bizarre value. I incorrectly assumed a C long in
mappe
to C# long. Using C# int worked. But here's my question

Since the DLL is known, is there a tool that will list the C
equivalents fo
the functions as long as the variable types are standard (i.e., int
long, float, etc. )? Does it seems reasonable that C# should be abl
to gather this info from the DLL

Finally, what is the name of the process of using the [DllImport...
specification for loading external functions
Is this PInvoke
Are there any books/articles describing this in detail, particularl
wit
accessing and running C functions that return pointers to structures

Thanks

Te
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top