c#中的Marshal非托管变量

时间:2016-11-18 18:24:00

标签: c# dllimport unmanaged wchar-t interopservices

我在C#代码上导入了一个非托管dll。 .h文件描述了如下所示的方法

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.

1 个答案:

答案 0 :(得分:1)

好的,我找到了答案

[DllImport(DllName, EntryPoint = "GetErrorMessage_UTF16", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int GetErrorMessage_UTF16(out int errorGroup, IntPtr errorCode, IntPtr messageText, int bufferLength);

对于errorCode和messageText,创建一个指针,然后分配大小。

IntPtr errorCode = Marshal.AllocHGlobal(10);
IntPtr messageText = Marshal.AllocHGlobal(someBufferLength * sizeof(char));

当不需要变量时,不要忘记释放分配的空间。