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#?