问题描述:

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 int.

Try passing a out IntPtr szOutput, then use the Marshal.PtrToStringUni() with the returned IntPtr.

See for example PInvoke and char** for a similar problem.

相关阅读:
Top