我正在尝试调用C++ dll中的方法。
HRESULT WINAPI TestMethod(
_Out_ BOOL *isSuccess,
_In_opt_ DWORD UsernmaeLength,
_Out_opt_ LPWSTR userName );
我编写的C#包装方法如下
[DllImport("Test.dll", CharSet = CharSet.Unicode, SetLastError = true ,CallingConvention = CallingConvention.StdCall)]
public static extern int TestMethod (
IntPtr isSuccess,
[In, Optional] int UsernmaeLength,
out string userName
);
我在程序中调用了这个方法
Wrapper. TestMethod (isSuccess, 200, out userName);
我遇到了System.AccessViolationException错误。
尝试更改C#包装器方法:
[DllImport("Test.dll", CharSet = CharSet.Unicode, SetLastError = true ,CallingConvention = CallingConvention.StdCall)]
public static extern int TestMethod (
bool isSuccess,
[In, Optional] int UsernmaeLength,
out string userName
);
//Caller
bool isSuccess = false;
Wrapper. TestMethod (isSuccess, 200, out userName);
请问您能否帮我理解我在这里做错了什么?
uint UsernmaeLength
。 - FaizanHussainRabbaniout IntPtr userName
- FaizanHussainRabbanipublic static extern int TestMethod ( [Out] ref bool isSuccess, [In, Optional] uint UsernmaeLength, [Out, Optional] IntPtr userName );
- FaizanHussainRabbani