C# – PInvoke for C function that returns char *

cpinvoke

I'm trying to write some C# code that calls a method from an unmanaged DLL. The prototype for the function in the dll is:

extern "C" __declspec(dllexport) char *foo(void);

In C#, I first used:

[DllImport(_dllLocation)]
public static extern string foo();

It seems to work on the surface, but I'm getting memory corruption errors during runtime. I think I'm pointing to memory that happens to be correct, but has already been freed.

I tried using a PInvoke code gen utility called "P/Invoke Interop Assistant". It gave me the output:

[System.Runtime.InteropServices.DLLImportAttribute(_dllLocation, EntryPoint = "foo")]
public static extern System.IntPtr foo();

Is this correct? If so, how do I convert this IntPtr to a string in C#?

Best Answer

You must return this as an IntPtr. Returning a System.String type from a PInvoke function requires great care. The CLR must transfer the memory from the native representation into the managed one. This is an easy and predictable operation.

The problem though comes with what to do with the native memory that was returned from foo(). The CLR assumes the following two items about a PInvoke function which directly returns the string type

  1. The native memory needs to be freed
  2. The native memory was allocated with CoTaskMemAlloc

Therefore it will marshal the string and then call CoTaskMemFree(...) on the native memory blob. Unless you actually allocated this memory with CoTaskMemAlloc this will at best cause a crash in your application.

In order to get the correct semantics here you must return an IntPtr directly. Then use Marshal.PtrToString* in order to get to a managed String value. You may still need to free the native memory but that will dependent upon the implementation of foo.

Related Topic