0
我正在导入我的C#代码中的非托管dll。 .h文件描述如图所示波纹管c#中的管理非托管变量
DLL_API int __stdcall SetConfiguration(IN char* configuration);
DLL_API int __stdcall GetErrorMessage_UTF16(
INOUT int* errorGroup,
INOUT char* errorCode, /*It must be allocated by the caller and its length must be at least 10 bytes, otherwise the function will crash*/
INOUT wchar_t *messageText,
IN int bufferLength);
我已经成功地调用SetConfiguration方法
[DllImport(DllPath, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int SetConfiguration([In] MarshalAs(UnmanagedType.LPStr)] string configuration);
供应一个简单的C#字符串做了工作方法。
现在我正在尝试GetErrorMessage_UTF16方法没有成功。那么,我该如何分配errorCode的长度,以及如何声明wchar_t * messageText变量?
我已经试过这样的事情,但它似乎没有工作
[DllImport(DllPath, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int GetErrorMessage_UTF16(
[In, Out] int errorGroup,
[In, Out] [MarshalAs(UnmanagedType.LPStr)] string errorCode,
[In, Out] [MarshalAs(UnmanagedType.LPStr)] StringBuilder messageText,
[In] int bufferLength);
不管我试过我得到
Exception thrown at 0x0f5d1235 (Mydll.dll) in Mydll.exe: 0xc0000005: Access violation writing location 0x067d1000
If there is a handler for this exception, the program may be safely continued.
第一个arg应该是'out int',第二个arg看起来应该是StringBuilder,第三个arg是LPWStr –