在 C# 中调用 C++ 方法

Calling C++ method in C#

我需要从 c# 调用一个 c++ 函数。

c++函数是

BOOL Usb_GetDevicesList(int &iNbDevices, char aszDeviceName[][128]);

我试过了

  [DllImport("UsbComm.dll", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
        public static extern int Usb_GetDevicesList(int iNbDevices, out byte[][] aszDeviceName);

我遇到错误

Cannot marshal 'parameter #2': There is no marshaling support for nested arrays.

请帮助我将此 C++ 函数转换为 C#。

您可以将二维数组展平为一维数组然后传递它。

 flattened_array[(y * width) + x] = source[x][y];

参考this answer