AccessViolation

man*_*l w 5 c# string pinvoke dllimport access-violation

从外部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();
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
}
Run Code Online (Sandbox Code Playgroud)

但这对我没有帮助.

提前致谢!

Dav*_*nan 3

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

是解决这个问题的最好方法。内存在接口的本机端分配和拥有。诀窍在于如何获得它。像这样的东西应该有效。

static public string[] getDevices()
{
    IntPtr devices;
    int deviceCount;
    short ret = get_device_list(out devices, out deviceCount);
    //need to test ret in case of error

    string[] result = new string[deviceCount];
    for (int i=0; i<deviceCount; i++)
    {
        IntPtr ptr = (IntPtr)Marshal.PtrToStructure(devices, typeof(IntPtr));
        result[i] = Marshal.PtrToStringAnsi(ptr);
        devices += IntPtr.Size;//move to next element of array
    }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

您的代码正在使用PtrToStringAuto,但这会将数据解释为UTF-16编码的。但你的 C++ 代码使用的char*是 8 位 ANSI。所以你需要PtrToStringAnsi. 好的,这里假设编码不是 UTF-8,但这是我无法提供的细节。使其适应 UTF-8 很容易。

您还应该仔细检查本机代码是否使用stdcall调用约定并且不使用cdecl.