need help for c method wrapper

Announcements and discussion for PInvoke

Moderators: Luke Jefferson, Robert

need help for c method wrapper

Postby dasda » Wed Aug 24, 2011 11:26 am


I am currently wrapping a library written in C from a thrid party for using it with .NET C#.
I have done 90% of all methods but for some reason I am getting stuck on this one and I would kindly ask some more experienced poeple for help.

The Dll is compiled with calling convention cdecl

[b]here is the c method signature:[/b]
File name: navAPI.h

#ifdef ******_EXPORTS
#define DLLDIR __declspec(dllexport) // export DLL information

#define DLLDIR __declspec(dllimport) // import DLL information


extern "C" {

BOOL DLLDIR avAPI_GetFrame(LPDWORD buffer, DWORD buffersize, LPDWORD timestamp);


[b]here is a code snipped from a working demo:[/b]

unsigned long buf[256];
DWORD dwTimestamp;
BOOL bRet;

bRet=avAPI_GetFrame(buf,256*sizeof(unsigned long),&dwTimestamp);

and here my last approach to implement it in .NET[/b]

[DllImport("avAPI.dll", EntryPoint = "avAPI_GetFrame", SetLastError = true)]
private static extern bool avAPI_GetFrame128([Out][MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U4, SizeConst = 128)] out uint[] buffer,
[In] uint buffersize, out uint timestamp);

I think the marshaling of the buffer array is the problem. I am not sure how to send only a pointer to an array without initializing it on the managed side and the get an initizialized array out of the C method.
The demo from the manufacturer is just doing this if I interpret everything correct. It declares an array of unsigned long and 256 elements but I see no initialization before calling the method.
the method could be called with different buffer sizes and in my tests I just used 128 but it fails too when using 256. The buffer size is calculated this way: 4 * <emelent count of the buffer>

Thank you!

edit: SOLVED
I think I have got it...
It is so simple ;^)
I have removed all fancy Marshal attributes for the array and removed the out keyword and voila a get my array ;^) :

[DllImport("avAPI.dll", EntryPoint = "avAPI_GetFrame", SetLastError = true)]
private static extern bool avAPI_GetFrame128([Out] uint[] buffer,
[In] uint buffersize, out uint timestamp);

If someone has some further hints (could this lead to memory leaks?) I would really like to read it!

Posts: 1
Joined: Wed Aug 24, 2011 11:12 am

Return to PInvoke.Net

Who is online

Users browsing this forum: No registered users and 0 guests