How to call functions inside C dll that take pointers as arguments from C #
This is my first post here!
I'm trying to make a windows forms program using C # that will use a pre-compiled C library. It will access the smart card and expose it. For a library, I have .dll, .lib and .h and no source. There are several structures specified in the .h file. The most interesting .dll functions expect pointers to allocated structures as arguments. I have called functions inside .dll like this: For example, the function
EID_API int WINAPI EidStartup(int nApiVersion);
will be called as
[DllImport("CelikApi.dll")]//the name of the .dll
public static extern int EidStartup(int nApiVersion);
Now my problem is that I cannot find the equivalent of C pointers that point to dynamically allocated structures in memory in C #, so I don't know what to pass as an argument for functions that take C pointers.
I don't have much experience with C #, but to me, using it seemed like the easiest way to make the program I need. I've tried with C ++, but Visual Studio 2010 doesn't have IntelliSense for C ++ / CLR. If you can point me to something better, feel free to do so.
a source to share
You can do something like
[DllImport("Operations.dll")]
public static extern void Operation(
[MarshalAs(UnmanagedType.LPArray)]ushort[] inData,
int inSize1, int inSize2,
[MarshalAs(UnmanagedType.LPArray)]int[] outCoords,
ref int outCoordsSize);
This code will get a dynamically allocated array of unsigned short messages (ushort in C #) as well as several size parameters (inSize1 and inSize2) and put the results in an outCoords array of size outCoordsSize.
Your C code cannot allocate memory and expect C # to play with it; C # should allocate all the memory that your C code reproduces with. In the above case, you can put the size of the outCoords array in outCoordsSize, and then replace the outCoordsSize value with the amount of memory you are using (which cannot exceed the amount of memory you originally allocated without exception).
a source to share