Marshal.PtrToStringAnsi()
is what you need to convert your IntPtr
to a string. The IntPtr
holds a char*
, and Marshal.PtrToStringAnsi()
is your guy.
However, you've already tried that without success. So I suspect that your problem is more fundamental. Perhaps you have a binary mismatch in the way the two sides of the interop boundary treat the large struct. This is one part of interop where different tools behave differently. Structs should always be returned using out
parameters. Change the native code to be like this:
__declspec(dllexport) int compute_calc(int opt, const char* file_1,
const char* file_2, struct_name* result)
And the managed side to be:
[StructLayout(LayoutKind.Sequential)]
public struct struct_name{
int a;
IntPtr s;
}
[DllImport("dll_name.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int compute_calc(int opt, string file1, string file2,
out struct_name result);
Note that I also removed the Pack = 1
from your struct declaration. Unless the C code uses #pragma pack
, and it should not do so, then this would cause a mismatch under 64 bit code.