Reputation: 822
i'm having an issue PInvoking a native function from c++... i have PInvoked all the other native functions without problems (params were very simple conversions). below is the c++ prototype and its usage in c++:
DECL_FOOAPIDLL DWORD WINAPI FOO_Command(
VOID *Par, //pointer to the parameter(s) needed by the command
DWORD Len, //length of Parameter in bytes
DWORD Id //identification number of the command
);
usage:
static DWORD DoSomething(WCHAR *str)
{
DWORD len = (wcslen(str)+1)*sizeof(WCHAR);
DWORD ret = FOO_Command((VOID *)str,len,FOOAPI_COMMAND_CLOSE);
return(ret);
}
my C# PInvoke:
[DllImport("FOO.dll")]
static public extern uint FOO_Command(IntPtr par, uint len, uint id);
private void DoSomething()
{
string text = "this is a test";
IntPtr ptr = Marshal.StringToHGlobalUni(text);
uint hr = FOO_Command(ptr,255, FOOAPI_COMMAND_CLOSE);
Marshal.FreeHGlobal(ptr);
}
1) is this the correct way to PInvoke this API? 2) how can i get the size of ptr where i have 255 as the parm?
the above does work, but i'm new to PInvoke and the "Native" world...
Thank you
Upvotes: 0
Views: 514
Reputation: 16981
private void DoSomething()
{
string text = "this is a test";
IntPtr ptr = Marshal.StringToHGlobalUni(text);
uint hr = FOO_Command(ptr, (text.Length + 1) * 2, FOOAPI_COMMAND_CLOSE);
Marshal.FreeHGlobal(ptr);
}
Upvotes: 1