B
Berhack
I am not too familiar with C# interop so please help me out. I need
to call the following C function (in a DLL):
// this creates an array of strings
// LPTSTR is just char *
void C_Func(LPTSTR **pszStrings)
{
(*pszStrings) = reinterpret_cast<LPTSTR *>(malloc(2 *
sizeof(LPTSTR)));
(*pszStrings)[0] = _T("A test");
(*pszStrings)[1] = _T("Anorther String");
}
This is what I have so far in C# (please be gentle, i'm a newbie at
it):
[DllImport(...)]
public static extern void recvReports(out IntPtr ptr);
// later on...
IntPtr ptr;
IntPtr i = C_Func(out ptr);
int offset = 0;
for (;
{
IntPtr p = Marshal.ReadIntPtr(ptr, offset);
if (p == IntPtr.Zero) break;
Console.Writelin("Found String: " +
Marshal.PtrToStringAnsi(p));
Marshal.FreeHGlobal(p); // trying to free the malloc here...
offset += IntPtr.Size;
}
Can someone correct it?? Extra marks for explaining why my code is
wrong. Thanks a lot.
to call the following C function (in a DLL):
// this creates an array of strings
// LPTSTR is just char *
void C_Func(LPTSTR **pszStrings)
{
(*pszStrings) = reinterpret_cast<LPTSTR *>(malloc(2 *
sizeof(LPTSTR)));
(*pszStrings)[0] = _T("A test");
(*pszStrings)[1] = _T("Anorther String");
}
This is what I have so far in C# (please be gentle, i'm a newbie at
it):
[DllImport(...)]
public static extern void recvReports(out IntPtr ptr);
// later on...
IntPtr ptr;
IntPtr i = C_Func(out ptr);
int offset = 0;
for (;
{
IntPtr p = Marshal.ReadIntPtr(ptr, offset);
if (p == IntPtr.Zero) break;
Console.Writelin("Found String: " +
Marshal.PtrToStringAnsi(p));
Marshal.FreeHGlobal(p); // trying to free the malloc here...
offset += IntPtr.Size;
}
Can someone correct it?? Extra marks for explaining why my code is
wrong. Thanks a lot.