I'm trying to call a unmanaged function from a DLL written in C++ from a C# console application. I have managed to do so for a couple of simple method calls however one of the functions takes a parameter of void*
and I'm not sure what to pass it to get it to work.
C++ method signature
BOOL, SetData, (INT iDevice, BYTE iCmd, VOID* pData, DWORD nBytes)
C# method signature
[DllImport("NvUSB.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static unsafe extern bool SetData(int iDevice, byte iCmd, void* pData, uint nBytes);
Working C++ Call
DWORD pDatatoHi[] = { 'R','E','B','O','O','T' };
SetData(0, 0, pDatatoHi, sizeof(pDatatoHi))
Not working C#
uint* cmd = stackalloc uint[6];
cmd[0] = 'R';
cmd[1] = 'E';
cmd[2] = 'B';
cmd[3] = 'O';
cmd[4] = 'O';
cmd[5] = 'T';
SetData(address, 0, cmd, 6);
The method I am trying to call from the unmanaged DLL should reboot a USB device, when the function is called as per the C++ example above the device reboots correctly. When I call the C# version above the code executes and returns true, however the device does not reboot. I suspect this is due to the way in which I am passing the pData
parameter?
When you call SetData(address, 0, cmd, 6)
the number of bytes is 24, not 6. 6 is the number of items, not the size of the array, which is what you're passing in the C++ example you've given