USB设备CreateFile总是返回Invalid Handle
extern "C" {
#include "hidsdi.h"
#include <setupapi.h>
}
#include "stdio.h"
void main()
{
GUID HidGuid;
//1. HidD_GetHidGuid
HidD_GetHidGuid(&HidGuid);
//2. SetupDiGetClassDevs
//The "SetupDiGetClassDevs" function returns the address of an array of structures
//containing information about all attached and enumerated HIDs.
HDEVINFO hDevInfo=SetupDiGetClassDevs (&HidGuid,
NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
SP_DEVICE_INTERFACE_DATA devInfoData;
devInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for(int i=0;;i++){
//3. SetupDiEnumDeviceInterfaces
//The next call is to SetupDiEnumDeviceInterfaces, which retrieves a pointer
//to a structure that identifies an interface in the previously retrieved Device-
//InfoSet array.
BOOLEAN hasMore=SetupDiEnumDeviceInterfaces(hDevInfo,0,&HidGuid,
i,&devInfoData);
if(!hasMore){
printf("No more interfaces can be found!\n");
break;
}
DWORD length;
//4. SetupDiGetDevicelnterfaceDetail
if(!SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,NULL,0,&length,NULL)){
printf("GetDeviceInterfaceDetail DataSize failed!This is normal!Error Code is 0x%x\n",GetLastError());
}
PSP_DEVICE_INTERFACE_DETAIL_DATA detailData;
detailData =(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(length);
detailData->cbSize =sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
//5. SetupDiGetDevicelnterfaceDetail
if(!SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,detailData,
length,NULL,NULL)){
printf("GetDeviceInterfaceDetail failed!\n");
continue;
}
//6. CreateFile
HANDLE dHwnd = CreateFile(detailData->DevicePath,
GENERIC_WRITE|GENERIC_READ,
FILE_SHARE_WRITE|FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING, 0, NULL);
if(dHwnd==INVALID_HANDLE_VALUE)
printf("dHwnd is invalid!\n");
// Get Device Attributes
HIDD_ATTRIBUTES dAttrib;
dAttrib.Size = sizeof(dAttrib);
//7. HidD_GetAttributes
if(!HidD_GetAttributes(dHwnd,&dAttrib)){
printf("Get Device Attribute failed!Error Code is 0x%x\n",GetLastError());
}
else
printf("Device vendor ID is 0x%x\n",dAttrib.VendorID);
// Get device PreparsedData
PHIDP_PREPARSED_DATA preparsedData;
if(!HidD_GetPreparsedData(dHwnd,&preparsedData)){
printf("Get PreparsedData failed!\n");
}
CloseHandle(dHwnd);
//HidD_FreePreparsedData(preparsedData);
}
SetupDiDestroyDeviceInfoList(hDevInfo);
}
源代码如上,为什么会这样呢?请前辈们指教???
#include "hidsdi.h"
#include <setupapi.h>
}
#include "stdio.h"
void main()
{
GUID HidGuid;
//1. HidD_GetHidGuid
HidD_GetHidGuid(&HidGuid);
//2. SetupDiGetClassDevs
//The "SetupDiGetClassDevs" function returns the address of an array of structures
//containing information about all attached and enumerated HIDs.
HDEVINFO hDevInfo=SetupDiGetClassDevs (&HidGuid,
NULL, NULL, DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
SP_DEVICE_INTERFACE_DATA devInfoData;
devInfoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
for(int i=0;;i++){
//3. SetupDiEnumDeviceInterfaces
//The next call is to SetupDiEnumDeviceInterfaces, which retrieves a pointer
//to a structure that identifies an interface in the previously retrieved Device-
//InfoSet array.
BOOLEAN hasMore=SetupDiEnumDeviceInterfaces(hDevInfo,0,&HidGuid,
i,&devInfoData);
if(!hasMore){
printf("No more interfaces can be found!\n");
break;
}
DWORD length;
//4. SetupDiGetDevicelnterfaceDetail
if(!SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,NULL,0,&length,NULL)){
printf("GetDeviceInterfaceDetail DataSize failed!This is normal!Error Code is 0x%x\n",GetLastError());
}
PSP_DEVICE_INTERFACE_DETAIL_DATA detailData;
detailData =(PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(length);
detailData->cbSize =sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
//5. SetupDiGetDevicelnterfaceDetail
if(!SetupDiGetDeviceInterfaceDetail(hDevInfo,&devInfoData,detailData,
length,NULL,NULL)){
printf("GetDeviceInterfaceDetail failed!\n");
continue;
}
//6. CreateFile
HANDLE dHwnd = CreateFile(detailData->DevicePath,
GENERIC_WRITE|GENERIC_READ,
FILE_SHARE_WRITE|FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING, 0, NULL);
if(dHwnd==INVALID_HANDLE_VALUE)
printf("dHwnd is invalid!\n");
// Get Device Attributes
HIDD_ATTRIBUTES dAttrib;
dAttrib.Size = sizeof(dAttrib);
//7. HidD_GetAttributes
if(!HidD_GetAttributes(dHwnd,&dAttrib)){
printf("Get Device Attribute failed!Error Code is 0x%x\n",GetLastError());
}
else
printf("Device vendor ID is 0x%x\n",dAttrib.VendorID);
// Get device PreparsedData
PHIDP_PREPARSED_DATA preparsedData;
if(!HidD_GetPreparsedData(dHwnd,&preparsedData)){
printf("Get PreparsedData failed!\n");
}
CloseHandle(dHwnd);
//HidD_FreePreparsedData(preparsedData);
}
SetupDiDestroyDeviceInfoList(hDevInfo);
}
源代码如上,为什么会这样呢?请前辈们指教???
...全文
相关推荐
usb.rar_USB C程序_USB接口通信_c++ usb通信_usb 接口_用VC++编写USB接口通信程序
用VC++编写USB接口通信程序?肰C++编写USB接口通信程序?肰C++编写USB接口通信程序
delphi开发usb的方法
delphi开发usb接口的方法,需要借助setupapi才行,csdn中有下载
C#usb代码学习
C#usb代码学习a
访问USB端口的程序.pdf
访问USB端口的程序及示例(C++) 如果你想打开一个USB管道, 你首先要知道这种USB设备的GUID和管道名称, 获取句柄以后就可以使用ReadFile/WriteFile 进行读写了! 以下是代码,请参考!!
MFC读取USB接口硬盘固件、序列号、型号
在VS2015环境用的C++编程,实现了读取USB接口的固态硬盘的型号、固件、序列号信息,主要通过盘符,代码中的盘符要按实际显示修改,我这里面是设置的G盘。


驱动开发/核心开发
