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;
int my_error;
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.
//就是这里,老是报1784,怎么回事啊??????
if (! SetupDiGetInterfaceDeviceDetail (
HardwareDeviceInfo,
DeviceInfoData,
functionClassDeviceData,
predictedLength,
&requiredLength,
NULL))
{
my_error = GetLastError();
return INVALID_HANDLE_VALUE;
}
strcpy( devName,functionClassDeviceData->DevicePath) ;
hOut = CreateFile (
functionClassDeviceData->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (INVALID_HANDLE_VALUE == hOut)
{
}
free(functionClassDeviceData);
return hOut;
}
--------------------next---------------------
HANDLE OpenUsbDevice( LPGUID pGuid, char *outNameBuf)
{
ULONG NumberDevices;
HANDLE hOut = INVALID_HANDLE_VALUE;
HDEVINFO hardwareDeviceInfo;
SP_INTERFACE_DEVICE_DATA deviceInfoData;
ULONG i;
BOOLEAN done;
PUSB_DEVICE_DESCRIPTOR usbDeviceInst;
PUSB_DEVICE_DESCRIPTOR *UsbDevices = &usbDeviceInst;
*UsbDevices = NULL;
NumberDevices = 0;
hardwareDeviceInfo = SetupDiGetClassDevs (pGuid,NULL,NULL,(DIGCF_PRESENT | DIGCF_INTERFACEDEVICE));
NumberDevices = 4;
done = FALSE;
deviceInfoData.cbSize = sizeof (SP_INTERFACE_DEVICE_DATA);
i=0;
while (!done)
{
NumberDevices *= 2;
if (*UsbDevices)
{
// *UsbDevices =
// (PUSB_DEVICE_DESCRIPTOR)realloc (*UsbDevices, (NumberDevices * sizeof (USB_DEVICE_DESCRIPTOR)));
*UsbDevices =
(struct _USB_DEVICE_DESCRIPTOR *)realloc (*UsbDevices, (NumberDevices *
sizeof (USB_DEVICE_DESCRIPTOR)));
}
else
{
// *UsbDevices = (PUSB_DEVICE_DESCRIPTOR)calloc (NumberDevices, sizeof (USB_DEVICE_DESCRIPTOR));
*UsbDevices = (struct _USB_DEVICE_DESCRIPTOR *)calloc (NumberDevices,
sizeof (USB_DEVICE_DESCRIPTOR));
}
if (NULL == *UsbDevices)
{
SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
return INVALID_HANDLE_VALUE;
}
usbDeviceInst = *UsbDevices + i;
for (i=0; i < NumberDevices; i++)
{
if (SetupDiEnumDeviceInterfaces (hardwareDeviceInfo,0,pGuid,i,&deviceInfoData))
{
int my_error = GetLastError();
//打开设备
hOut = OpenOneDevice (hardwareDeviceInfo, &deviceInfoData, outNameBuf);
if ( hOut != INVALID_HANDLE_VALUE )
{
done = TRUE;
break;
}
} else
{
if (ERROR_NO_MORE_ITEMS == GetLastError())
{
done = TRUE;
break;
}
}
}
}
NumberDevices = i;
SetupDiDestroyDeviceInfoList (hardwareDeviceInfo);
if (*UsbDevices)
free(*UsbDevices);
return hOut;
}
BOOL GetUsbDeviceFileName( LPGUID pGuid, char *outNameBuf)
{
HANDLE hDev = OpenUsbDevice( pGuid, outNameBuf );
if ( hDev != INVALID_HANDLE_VALUE )
{
CloseHandle( hDev );
return TRUE;
}
return FALSE;
}
HANDLE open_dev()
{
HANDLE hDEV = OpenUsbDevice( (LPGUID)&GUID_CLASS_D12_BULK, completeDeviceName);
return hDEV;
}
HANDLE open_file( char *filename)
{
HANDLE h;
//打开设备
if ( !GetUsbDeviceFileName((LPGUID) &GUID_CLASS_D12_BULK,completeDeviceName) )
{
return INVALID_HANDLE_VALUE;
}
strcat (completeDeviceName,"\\");
strcat (completeDeviceName,filename);
h = CreateFile(completeDeviceName,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
return h;
}
--------------------next---------------------
阅读(1152) | 评论(0) | 转发(0) |