在 C# 中调用C++方法

Calling C++ method in C#

本文关键字:C++ 方法 调用      更新时间:2023-10-16

我需要从 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);

我收到错误

无法封送"参数 #2":没有封送处理支持 嵌套数组。

请帮助我将这个 c++ 函数转换为 C#。

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

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

参考此答案