Is it possible to return a LPWSTR from C++ DLL to C# app

后端 未结 2 1407
被撕碎了的回忆
被撕碎了的回忆 2021-01-15 04:12

The C++ function definition is this

__declspec(dllexport) LPWSTR __stdcall GetErrorString(int errCode);

And I call it in C# like this

2条回答
  •  星月不相逢
    2021-01-15 04:20

    See the accepted answer here: PInvoke for C function that returns char *

    The short version: you must marshal the return value as an IntPtr or the .NET runtime makes some assumptions and tries to delete the memory pointed to by the char pointer. This can cause crashes if the assumptions that the runtime makes are wrong.

提交回复
热议问题