1

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#.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Jayaraj.K
  • 928
  • 9
  • 30
  • See this link http://stackoverflow.com/questions/991383/call-a-c-function-from-c-sharp – husnain_sys Feb 03 '15 at 06:57
  • Presumably `iNbDevices` contains the length of the supplied array `aszDeviceName` on entry, and the number of items written to the array on exit? With `FALSE` returned if the supplied array is not long enough. Unless you also explain the semantics of the function it is hard to help. – David Heffernan Feb 03 '15 at 12:52

1 Answers1

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

Community
  • 1
  • 1
a_pradhan
  • 3,285
  • 1
  • 18
  • 23