In my C/C++ Library, I've a function with such a prototype:
long myFunction( const wchar_t*& pszOutput)
The Dll allocates the memory for pszOutput and deallocates it too.
How do I call this function from C# ?
I've tried a lot of variation of the following line (with out, with MarshalAs, ...), but I always get a crash when exiting myFunction:
[DllImport("mydll.dll", EntryPoint = "myFunction", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
private static extern long myFunction(ref string szOutput);
Does anyone knows, how to do that?
If you are on Windows, C++
long isn't C#
long, it is normally C#
int. Check the
sizeof(long) in C++. If it is 4 then it is
Try passing a
out IntPtr szOutput, then use the
Marshal.PtrToStringUni() with the returned
See for example PInvoke and char** for a similar problem.