Calling C++ method in C#

2019-08-08 07:54发布

问题:

I need to call a c++ function from c#.

c++ function is

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

I tried

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

I got error

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

Please help me in converting this c++ function to C#.

回答1:

You can just flatten the 2D array to a single dimensional one and then pass it.

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

Refer to this answer



标签: c# c++ pinvoke