G
Guest
I am trying to return strings from a P/invoke call to a C based DLL possibly
with a direct interface from C#.
I can get all basic types across, up to pointers to byte[] and then do the
Ascii to Unicode conversion on .netcf.
However I would like to just prepare the strings in the DLL and return them
"ready oto go".
Here's the code - ehant am I missing?
Thanks ahead
tb
C:
DllExport wchar_t* getString()
//a char* can be picked up via IntPtr for conversion
// but this (char* or wchar_t*) always gets me to an exception
{
wchar_t* result;
printf ("wcslen(L\"123abc\")=%d\n",wcslen(L"123abc"));
result = (wchar_t*)malloc((wcslen(L"123abc")+1) * sizeof(wchar_t));//??
somewhere the example said CoTaskMemAlloc - don't have that symbol so far...
wcscpy(result, L"123abc");
//those 2 confirm the string is ok
printf ("returning string to managed side - object reference = %d\n",
result);
wprintf(L"Result = :%s:\n",result);
return (wchar_t*) result;
}
c#:
[DllImport("my.dll")]
public static extern void passString (String str); //this works!!
[DllImport("my.dll")]
public static extern String getString (); //this not
//and the call:
Console.WriteLine("String returned " + getString());
with a direct interface from C#.
I can get all basic types across, up to pointers to byte[] and then do the
Ascii to Unicode conversion on .netcf.
However I would like to just prepare the strings in the DLL and return them
"ready oto go".
Here's the code - ehant am I missing?
Thanks ahead
tb
C:
DllExport wchar_t* getString()
//a char* can be picked up via IntPtr for conversion
// but this (char* or wchar_t*) always gets me to an exception
{
wchar_t* result;
printf ("wcslen(L\"123abc\")=%d\n",wcslen(L"123abc"));
result = (wchar_t*)malloc((wcslen(L"123abc")+1) * sizeof(wchar_t));//??
somewhere the example said CoTaskMemAlloc - don't have that symbol so far...
wcscpy(result, L"123abc");
//those 2 confirm the string is ok
printf ("returning string to managed side - object reference = %d\n",
result);
wprintf(L"Result = :%s:\n",result);
return (wchar_t*) result;
}
c#:
[DllImport("my.dll")]
public static extern void passString (String str); //this works!!
[DllImport("my.dll")]
public static extern String getString (); //this not
//and the call:
Console.WriteLine("String returned " + getString());