从外部 C DLL 调用以下命令时,我不断收到 AccessViolationException:
short get_device_list(char ***device_list, int *number_of_devices);
我这样设置了一个 DLLImport 声明:
[DLLImport("mydll.dll")]
static public extern short get_device_list([MarshalAs(UnmanagedType.LPArray)] ref string[] devices, ref int number_of_devices);
我的 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.
}
虽然我收到了异常,但设备数组被连接的设备的 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);
和
{
IntPtr devices = new IntPtr();
int i = 0;
short ret = 0;
ret = get_device_list(ref devices, ref i); // No AccessViolation Exception here
string b = Marshal.PtrToStringAuto(devices); // b = "歀ׄ", which is incorrect
}
但这对我没有帮助。
提前致谢!