小编man*_*l w的帖子

AccessViolation

从外部C DLL调用以下内容时,我不断收到AccessViolationException:

short get_device_list(char ***device_list, int *number_of_devices);
Run Code Online (Sandbox Code Playgroud)

我设置了一个DLLImport声明:

[DLLImport("mydll.dll")]
static public extern short get_device_list([MarshalAs(UnmanagedType.LPArray)] ref string[] devices, ref int number_of_devices);
Run Code Online (Sandbox Code Playgroud)

我的C#应用​​程序代码:

{
string[] devices = new string[20];
int i = 0;
short ret = 0;
ret = get_device_list(ref devices, ref i); // I receive the AccessViolation Exception here
// devices[0] = "2255f796e958f7f31a7d2e6b833d2d426c634621" which is correct.
}
Run Code Online (Sandbox Code Playgroud)

虽然我收到异常,但是设备阵列正确填充了所连接设备的2个UUID(并且还调整为size = 2; i也是2;).

怎么了?

PS:经过长时间的研究,我也尝试过:

[DLLImport("mydll.dll")]
static public extern short get_device_list(ref IntPtr devices, ref int number_of_devices);
Run Code Online (Sandbox Code Playgroud)

{
IntPtr devices = new IntPtr(); …
Run Code Online (Sandbox Code Playgroud)

c# string pinvoke dllimport access-violation

5
推荐指数
1
解决办法
1410
查看次数

标签 统计

access-violation ×1

c# ×1

dllimport ×1

pinvoke ×1

string ×1