bool GetDeviceHandle (GUID guidDeviceInterface, PHANDLE deviceHandle)
{
if (guidDeviceInterface == GUID_NULL)
{
return false;
}
bool res = true;
HDEVINFO deviceInfo;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
PSP_DEVICE_INTERFACE_DETAIL_DATA interfaceDetailData = NULL;
unsigned long requiredLength=0;
char devicePath[1024];
unsigned long index = 0;
// Get information about all the installed devices for the specified
// device interface class.
deviceInfo = SetupDiGetClassDevs(
&guidDeviceInterface,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (deviceInfo == INVALID_HANDLE_VALUE)
{
// ERROR
printf("Error SetupDiGetClassDevs: %d.\n", GetLastError());
return false;
}
for (index = 0; index < 8; index++)
{
deviceInterfaceData.cbSize = sizeof(SP_INTERFACE_DEVICE_DATA);
//Get information about the device interface.
res = SetupDiEnumDeviceInterfaces(
deviceInfo,
NULL,
&guidDeviceInterface,
index,
&deviceInterfaceData);
// Check if last item
if (GetLastError () == ERROR_NO_MORE_ITEMS)
{
break;
}
//Check for some other error
if (!res)
{
printf("Error SetupDiEnumDeviceInterfaces: %d.\n", GetLastError());
return false;
}
//Interface data is returned in SP_DEVICE_INTERFACE_DETAIL_DATA
//which we need to allocate, so we have to call this function twice.
//First to get the size so that we know how much to allocate
//Second, the actual call with the allocated buffer
res = SetupDiGetDeviceInterfaceDetail(
deviceInfo,
&deviceInterfaceData,
NULL, // probing so no output buffer yet
0, // probing so output buffer length of zero
&requiredLength,
NULL); // not interested in the specific dev-node
//Check for some other error
if (!res)
{
if ((ERROR_INSUFFICIENT_BUFFER == GetLastError()) && (requiredLength>0))
{
//we got the size, allocate buffer
interfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, requiredLength);
if (!interfaceDetailData)
{
// ERROR
printf("Error allocating memory for the device detail buffer.\n");
return false;
}
}
else
{
printf("Error SetupDiEnumDeviceInterfaces: %d.\n", GetLastError());
return false;
}
}
//get the interface detailed data
interfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
//Now call it with the correct size and allocated buffer
res = SetupDiGetDeviceInterfaceDetail(
deviceInfo,
&deviceInterfaceData,
interfaceDetailData,
requiredLength,
NULL,
&deviceInfoData);
//Check for some other error
if (!res)
{
printf("Error SetupDiGetDeviceInterfaceDetail: %d.\n", GetLastError());
return false;
}
//copy device path
strcpy(devicePath, interfaceDetailData->DevicePath);
printf("Device path: %s\n", devicePath);
if (!devicePath)
{
//Error.
printf("Error %d.", GetLastError());
return false;
}
//Open the device
*deviceHandle = CreateFile (
devicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if (*deviceHandle == INVALID_HANDLE_VALUE)
{
//Error.
printf("Error %d.", GetLastError());
goto done;
}
else
{
return true;
}
}
return false;
}
bool GetWinUSBHandle(HANDLE deviceHandle, PWINUSB_INTERFACE_HANDLE winUSBHandle)
{
if (deviceHandle == INVALID_HANDLE_VALUE)
{
return false;
}
bool res = WinUsb_Initialize(deviceHandle, winUSBHandle);
if (!res)
{
//Error.
printf("WinUsb_Initialize Error %d.", GetLastError());
return false;
}
return res;
}
int main(int argc, char* argv[])
{
GUID guidDeviceInterface = OMAPFLASH_USB; //in the INF file
bool res = true;
PIPE_ID pipeId;
HANDLE deviceHandle = INVALID_HANDLE_VALUE;
WINUSB_INTERFACE_HANDLE winUSBHandle = INVALID_HANDLE_VALUE;
unsigned char deviceSpeed;
unsigned int size = 0;
res = false;
do
{
res = GetDeviceHandle(guidDeviceInterface, &deviceHandle);
}while(!res);
res = GetWinUSBHandle(deviceHandle, &winUSBHandle);
}
Output:
WinUsb_Initialize Error 22.