HANDLE OpenDevice(LPGUID pGuid, char *outNameBuf)
{
HANDLE hOut = INVALID_HANDLE_VALUE;
HDEVINFO hardwareDeviceInfo;
SP_INTERFACE_DEVICE_DATA deviceInfoData;
hardwareDeviceInfo = SetupDiGetClassDevs (pGuid, NULL, NULL, (DIGCF_PRESENT | DIGCF_INTERFACEDEVICE));
deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);
if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo, 0, pGuid, 0, &deviceInfoData))
{
hOut = OpenOneDevice (hardwareDeviceInfo, &deviceInfoData, outNameBuf);
if (hOut==INVALID_HANDLE_VALUE)
return INVALID_HANDLE_VALUE;
}
DWORD reErr = GetLastError( );
SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
return hOut;
}
HANDLE OpenOneDevice(HDEVINFO HardwareDeviceInfo, PSP_INTERFACE_DEVICE_DATA DeviceInfoData, char *devName)
{
PSP_INTERFACE_DEVICE_DETAIL_DATA functionClassDeviceData = NULL;
ULONG predictedLength = 0;
ULONG requiredLength = 0;
HANDLE hOut = INVALID_HANDLE_VALUE;
// allocate a function class device data structure to receive the goods about this particular device.
SetupDiGetInterfaceDeviceDetail (HardwareDeviceInfo, DeviceInfoData, NULL, 0, &requiredLength, NULL);
predictedLength = requiredLength;
// sizeof (SP_FNCLASS_DEVICE_DATA) + 512;
functionClassDeviceData = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc (predictedLength);
functionClassDeviceData-> cbSize = sizeof (SP_INTERFACE_DEVICE_DETAIL_DATA);
// Retrieve the information from Plug and Play.
if (!SetupDiGetInterfaceDeviceDetail(
HardwareDeviceInfo,
DeviceInfoData,
functionClassDeviceData,
predictedLength,
&requiredLength,
NULL))
{
free(functionClassDeviceData);
return INVALID_HANDLE_VALUE;
}
strcpy(devName,functionClassDeviceData-> DevicePath);
printf( "Attempting to open %s\n ", devName);
hOut = CreateFile (functionClassDeviceData-> DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (INVALID_HANDLE_VALUE == hOut)
printf( "FAILED to open %s\n ", devName);
free(functionClassDeviceData);
return hOut;
}
HANDLE hDEV = OpenDevice((LPGUID)&GUID_BUS_UMSS, completeDeviceName);
if (hDEV==INVALID_HANDLE_VALUE)
return;
DeviceIoControl (
hDEV,
IOCTL_Vender_VCMD_WRITE,
buf,
31, //CBW
buf,
13, // Data + CSW
(LPDWORD)&nBytes,
NULL
); |