遍历USB设备获取USB序列号Word文件下载.docx
- 文档编号:18195168
- 上传时间:2022-12-14
- 格式:DOCX
- 页数:26
- 大小:36.36KB
遍历USB设备获取USB序列号Word文件下载.docx
《遍历USB设备获取USB序列号Word文件下载.docx》由会员分享,可在线阅读,更多相关《遍历USB设备获取USB序列号Word文件下载.docx(26页珍藏版)》请在冰豆网上搜索。
{
ULONGActualLength;
WCHARDriverKeyName[1];
}USB_HCD_DRIVERKEY_NAME,*PUSB_HCD_DRIVERKEY_NAME;
2:
IOCTL_USB_GET_ROOT_HUB_NAME,用于获取roothub键名。
使用的结构体,跟上面一样。
typedefstruct_USB_ROOT_HUB_NAME
WCHARRootHubName[1];
}USB_ROOT_HUB_NAME,*PUSB_ROOT_HUB_NAME;
3:
IOCTL_USB_GET_NODE_INFORMATION,用于获取连接在roothub上的节点设
备信息。
也就是我们接在usb口上的所有usb设备的信息,对应的结构体:
typedefstruct_USB_NODE_INFORMATION
USB_HUB_NODENodeType;
union{
USB_HUB_INFORMATIONHubInformation;
USB_MI_PARENT_INFORMATIONMiParentInformation;
}u;
}USB_NODE_INFORMATION,*PUSB_NODE_INFORMATION;
typedefstruct_USB_MI_PARENT_INFORMATION
ULONGNumberOfInterfaces;
}USB_MI_PARENT_INFORMATION,*PUSB_MI_PARENT_INFORMATION;
typedefstruct_USB_HUB_INFORMATION
USB_HUB_DESCRIPTORHubDescriptor;
BOOLEANHubIsBusPowered;
}USB_HUB_INFORMATION,*PUSB_HUB_INFORMATION;
typedefstruct_USB_HUB_DESCRIPTOR
UCHARbDescriptorLength;
//Lengthofthisdescriptor
UCHARbDescriptorType;
//Hubconfigurationtype
UCHARbNumberOfPorts;
//numberofportsonthishub
USHORTwHubCharacteristics;
//HubCharateristics
UCHARbPowerOnToPowerGood;
//portpowerontillpower
goodin2ms
UCHARbHubControlCurrent;
//maxcurrentinmA
//
//roomfor255portspowercontrolandremovablebitmask
UCHARbRemoveAndPowerMask[64];
}USB_HUB_DESCRIPTOR,*PUSB_HUB_DESCRIPTOR;
4)IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,用于获取接在usb口上的
单个usb设备的信息,对应的结构体:
typedefstruct_USB_NODE_CONNECTION_INFORMATION{
ULONGConnectionIndex;
USB_DEVICE_DESCRIPTORDeviceDescriptor;
UCHARCurrentConfigurationValue;
BOOLEANLowSpeed;
BOOLEANDeviceIsHub;
USHORTDeviceAddress;
ULONGNumberOfOpenPipes;
USB_CONNECTION_STATUSConnectionStatus;
USB_PIPE_INFOPipeList[0];
}USB_NODE_CONNECTION_INFORMATION,*PUSB_NODE_CONNECTION_INFORMATION;
IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,用于获取usb设备的
描述信息。
typedefstruct_USB_DEVICE_DESCRIPTOR{
UCHARbLength;
USHORTbcdUSB;
UCHARbDeviceClass;
UCHARbDeviceSubClass;
UCHARbDeviceProtocol;
UCHARbMaxPacketSize0;
USHORTidVendor;
USHORTidProduct;
USHORTbcdDevice;
UCHARiManufacturer;
UCHARiProduct;
UCHARiSerialNumber;
UCHARbNumConfigurations;
}USB_DEVICE_DESCRIPTOR,*PUSB_DEVICE_DESCRIPTOR;
typedefenum_USB_CONNECTION_STATUS{
NoDeviceConnected,
DeviceConnected,
/*failurecodes,thesemaptofailreasons*/
DeviceFailedEnumeration,
DeviceGeneralFailure,
DeviceCausedOvercurrent,
DeviceNotEnoughPower,
DeviceNotEnoughBandwidth,
DeviceHubNestedTooDeeply,
DeviceInLegacyHub
}USB_CONNECTION_STATUS,*PUSB_CONNECTION_STATUS;
typedefstruct_USB_PIPE_INFO
USB_ENDPOINT_DESCRIPTOREndpointDescriptor;
ULONGScheduleOffset;
}USB_PIPE_INFO,*PUSB_PIPE_INFO;
typedefstruct_USB_ENDPOINT_DESCRIPTOR
UCHARbEndpointAddress;
UCHARbmAttributes;
USHORTwMaxPacketSize;
UCHARbInterval;
}USB_ENDPOINT_DESCRIPTOR,*PUSB_ENDPOINT_DESCRIPTOR;
需要注意一点,如果要得到pid,vid,则直接从USB_DEVICE_DESCRIPTOR结构中取出idVendor,idProduct这两项的值就行了。
如果要得到序列号,则不是取出iSerialNumber就可以的。
这里的iSerialNumber仅仅是一个索引值。
如果想得到序列号,就需要定义一个结构,然后给设备发送个请求。
代码参照GetStringDescriptor函数。
可以根据iSerialNumber偏移,取出其对应的字符串,存放在上图USB_STRING_DESCRIPTOR结构中。
【转帖】windowssdk编程系列文章----遍历USB设备,获取扫描仪序列号之二网上的好文章2009-08-1318:
36阅读33评论0
字号:
大中小
#include"
windows.h"
PlkUsbIo.h"
#include<
malloc.h>
#defineNUM_HCS_TO_CHECK10
/******************************************************************/
boolEnumUsbDevice();
PCHARGetDriverKeyName(HANDLEHub,ULONGConnectionIndex);
PCHARGetHCDDriverKeyName(HANDLEHCD);
PCHARGetRootHubName(HANDLEHostController);
PCHARWideStrToMultiStr(PWCHARWideStr);
boolGetStringDescriptor(
HANDLEhHubDevice,
ULONGConnectionIndex,
UCHARDescriptorIndex,
CHAR*outBuff);
intmain(intargc,char*argv[])
EnumUsbDevice();
return0;
}
boolEnumUsbDevice()
charHCName[16];
intHCNum;
HANDLEhHCDev;
PCHARrootHubName;
ULONGindex;
BOOLsuccess;
PUSB_NODE_CONNECTION_INFORMATIONconnectionInfo;
HANDLEhHubDevice;
for(HCNum=0;
HCNum<
NUM_HCS_TO_CHECK;
HCNum++)
wsprintf(HCName,"
\\\\.\\HCD%d"
HCNum);
hHCDev=CreateFile(HCName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if(hHCDev==INVALID_HANDLE_VALUE)
break;
PCHARdriverKeyName,deviceDesc;
driverKeyName=GetHCDDriverKeyName(hHCDev);
if(driverKeyName==NULL)
gotoend;
ULONGnBytes;
rootHubName=(char*)GetRootHubName(hHCDev);
if(rootHubName==NULL)
PUSB_NODE_INFORMATIONHubInfo;
HubInfo=(PUSB_NODE_INFORMATION)malloc(sizeof(USB_NODE_INFORMATION));
PCHARdeviceName;
deviceName=(PCHAR)malloc(strlen(rootHubName)+sizeof("
\\\\.\\"
));
if(rootHubName!
=NULL)
strcpy(deviceName,"
);
strcpy(deviceName+sizeof("
)-1,rootHubName);
hHubDevice=CreateFile(deviceName,
free(deviceName);
if(hHubDevice==INVALID_HANDLE_VALUE)
success=DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_INFORMATION,
HubInfo,
sizeof(USB_NODE_INFORMATION),
&
nBytes,
if(!
success)
intport;
port=HubInfo->
u.HubInformation.HubDescriptor.bNumberOfPorts;
for(index=1;
index<
=port;
index++)
nBytes=sizeof(USB_NODE_CONNECTION_INFORMATION)+
sizeof(USB_PIPE_INFO)*30;
connectionInfo=(PUSB_NODE_CONNECTION_INFORMATION)malloc(nBytes);
if(connectionInfo==NULL)
connectionInfo->
ConnectionIndex=index;
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
connectionInfo,
free(connectionInfo);
deviceDesc=NULL;
if(connectionInfo->
ConnectionStatus!
=NoDeviceConnected)
driverKeyName=GetDriverKeyName(hHubDevice,
index);
if(driverKeyName)
free(driverKeyName);
ConnectionStatus==DeviceConnected)
//取出序列号索引
UCHARnSerialno=connectionInfo->
DeviceDescriptor.iSerialNumber;
CHAROutBuff[20]={0};
GetStringDescriptor(hHubDevice,connectionInfo->
ConnectionIndex,nSerialno,OutBuff);
//判断序列号是否有效
if(序列号是否有效)
CloseHandle(hHubDevice);
CloseHandle(hHCDev);
returntrue;
end:
;
returnfalse;
PCHARGetDriverKeyName(HANDLEHub,ULONGConnectionIndex){
USB_NODE_CONNECTION_DRIVERKEY_NAMEdriverKeyName;
PUSB_NODE_CONNECTION_DRIVERKEY_NAMEdriverKeyNameW;
PCHARdriverKeyNameA;
driverKeyNameW=NULL;
driverKeyNameA=NULL;
driverKeyName.ConnectionIndex=ConnectionIndex;
success=DeviceIoControl(Hub,
IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
driverKeyName,
sizeof(driverKeyName),
gotoGetDriverKeyNameError;
nBytes=driverKeyName.ActualLength;
if(nBytes<
=sizeof(driverKeyName))
driverKeyNameW=(PUSB_NODE_CONNECTION_DRIVERKEY_NAME)malloc(nBytes);
if(driverKeyNameW==NULL)
driverKeyNameW->
ConnectionIndex=ConnectionIndex;
driverKeyNameW,
driverKeyNameA=WideStrToMultiStr(driverKeyNameW->
DriverKeyName);
free(driverKeyNameW);
returndriverKeyNameA;
GetDriverKeyNameError:
if(driverKeyNameW!
returnNULL;
PCHARGetRootHubName(HANDLEHostController){
USB_ROOT_HUB_NAMErootHubName;
PUSB_ROOT_HUB_NAMErootHubNameW;
PCHARrootHubNameA;
rootHubNameW=NULL;
rootHubNameA=NULL;
success=DeviceIoControl(HostController,
IOCTL_USB_GET_ROOT_HUB_NAME,
rootHubName,
sizeof(rootHubName),
gotoGetRootHubNameError;
nBytes=rootHubName.ActualLength;
rootHubNameW=(PUSB_ROOT_HUB_NAME)malloc(nBytes);
if(rootHubNameW==NULL)
rootHubNameW,
rootHubNameA=WideStrToMultiStr(rootHubNameW->
RootHubName);
free(rootHubNameW);
returnrootHubNameA;
GetRootHubNameError:
if(rootHubNameW!
f
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 遍历 USB 设备 获取 序列号
![提示](https://static.bdocx.com/images/bang_tan.gif)