I'm working on converting a legacy project that was in managed c++, to c++ CLI, and I have the following code that is giving me error E0167 - argument of type "const cli::array<CHAR, 1> ^" is incompatible with parameter of type "LPCTSTR *"
deviceNumber = ListDevices(deviceList);
ListDevices is an API function that I don't have the ability to change, but it takes an LPCSTR* as a parameter and it returns an unsigned int. I'm not very familiar with C++ CLI, so I apologize in advance if there's a simple solution to this. The full context of how the line giving me error is used is given below. There's likely some things that don't make sense, but I'm just trying to get us compiling at this moment, and will go around cleaning/modernizing things as needed later. Any help will be greatly appreciated.
bool FindUsbDevice(void)
{
const array<char>^ deviceList = { 0, 0, 0 , 0, 0 ,0 ,0 ,0 ,0 ,0 };
UINT deviceNumber = 0;
DWORD error;
CString deviceName[1];
if (hUSB != INVALID_HANDLE_VALUE)
CloseUSBdevice();
//switchInterface = true;
SwitchtoUsb();
WaitingLoop(8000);
deviceNumber = ListDevices(deviceList);
if (deviceNumber > 0) {
deviceName[0] = deviceList[0];
hUSB = DeviceOpen(deviceName[0].GetBuffer(deviceName[0].GetLength()), &error);
deviceName[0].ReleaseBuffer(-1);
if (hUSB != INVALID_HANDLE_VALUE)
return true;
else
return false;
}
else
return false;
}
LPCSTRis already a pointer type: According to Microsoft, it's aconst char*. Therefore,LPCSTR*is a pointer to a pointer.A pointer to a pointer is usually one of two things: Either the calling method is allowed to change the second pointer to point at something else, or it's a pointer to a list (in which case
LPCSTR[]is the same to the compiler, but a better way for us to think about it).You haven't told us where this
ListDevicesAPI is from, but since you've named your variabledeviceList, I assume the documentation forListDevicessays that it expects a list as a parameter. Since there's no length parameter, it's probably expecting the list to be terminated with some sentinel value, probably a null pointer.In that case, you would need something like this:
As an array of strings, there's no logical conversion from your managed array of characters. If you have a list of managed strings, you can use
marshal_contextandmarshal_asto convert the managed strings to plainconst char*to put into this array.