Red Gate forums :: View topic - need help for c method wrapper
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
PInvoke.Net
PInvoke.Net forum

need help for c method wrapper

Search in PInvoke.Net forum
Post new topic   Reply to topic
Jump to:  
Author Message
dasda



Joined: 24 Aug 2011
Posts: 1

PostPosted: Wed Aug 24, 2011 11:26 am    Post subject: need help for c method wrapper Reply with quote

Hello,

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]
[code]
/*******************************************************
File name: navAPI.h

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

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

#endif

extern "C" {

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

[/code]

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

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

bRet=avAPI_GetFrame(buf,256*sizeof(unsigned long),&dwTimestamp);
[/code]

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

[code]
[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);
[/code]


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 ;^) :

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

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

Cheers
dasda
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group