从外部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)