关于USB设备路径的获取

sych11 2018-03-01 10:50:37
下面是两段网上找到遍历USB设备的经典代码,,每段代码获取的USB设备的路径不同,但好像关联性很大,我想能不能这两段代码分别从自己的代码中获取到另一段代码所获取到的USB设备路径,比如:
一段代码获取到的是
\\.\USB#ROOT_HUB#4&2661de7&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}
\\.\USB#ROOT_HUB#4&fde6d7e&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}
\\.\USB#ROOT_HUB#4&46b5628&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}
\\.\USB#ROOT_HUB#4&3b4bfc2c&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}
\\.\USB#ROOT_HUB20#4&314164ad&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}
另一段代码获取的是
\\.\usb#vid_046d&pid_c52b#5&27a80390&0&2#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
\\.\usb#vid_0cf3&pid_9271#12345#{a5dcbf10-6530-11d2-901f-00c04fb951ed}
怎么实现两段代码获取的设备路径一致?
...全文
2169 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sych11 2018-03-14
  • 打赏
  • 举报
回复
谢谢十豆三老师的关注,我的目的是要直接操控USB打印机,并且知道操作的打印机的信息(VID、PID、制造商名称等) 第一段代码获取的USB路径可以打开并由此得到USB设备的VID、PID、制造商名称等信息,但不能控制输入输出,第二段代码获取的USB路径也可以打开,但得不到USB设备的VID、PID、制造商名称等信息,不过却可以控制输入输出,所以我想能不能通过一段代码或者将这两段代码综合一下,一次性的就可以得到USB设备的VID、PID、制造商名称等信息而且可以进行控制输入输出操作 我不清楚现做一个猜测,第一段代码获取的USB设备路径应该是第二段代码获取的USB设备路径的父路径,如果能够知道怎么由子获取父或者由父获取子就好办了 另外还有一个问题,第一段代码在有的WIN7下 szHCDPath = "\\.\HCD" + TRANSFORM(i) hHCD = CreateFile(@szHCDPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0) 这句出错,返回错误值2,不知道该怎么处理,不过有的WIN7正常
十豆三 2018-03-11
  • 打赏
  • 举报
回复
这是我程序中的代码: Thisform.Combo1.Clear m.loWMI = Getobject("winmgmts:\\.\root\cimv2") m.loDisks = m.loWMI.ExecQuery([Select * From Win32_DiskDrive Where InterfaceType="USB"]) m.loParts = m.loWMI.ExecQuery([Select * From Win32_LogicalDiskToPartition]) For Each m.loDisk In m.loDisks m.lcDevID = 'Disk #' + Transform(m.loDisk.Index) For Each m.loPart In m.loParts If At(m.lcDevID, m.loPart.Antecedent) > 0 lnUsable=Diskspace(Strextract(m.loPart.Dependent, [DeviceID="], ["], 1),2)/1024 lnTimes=1 Do While lnUsable>1024 lnUsable=lnUsable/1024 lnTimes=lnTimes+1 Enddo lcUsableSize='' Do Case Case lnTimes=1 lcUsableSize=Transform(lnUsable)+' KB' Case lnTimes=2 lcUsableSize=Transform(lnUsable)+' MB' Case lnTimes=3 lcUsableSize=Transform(lnUsable)+' GB' Case lnTimes=4 lcUsableSize=Transform(lnUsable)+' TB' Endcase Thisform.Combo1.AddItem(Strextract(m.loPart.Dependent, [DeviceID="], ["], 1)+' 容量:'+Transform(Diskspace(Strextract(m.loPart.Dependent, [DeviceID="], ["], 1),1)/1024/1024/1024)+'GB 可用:'+lcUsableSize) Endif Endfor Endfor Thisform.Combo1.ListItemId=1 Release m.loWMI
sych11 2018-03-01
  • 打赏
  • 举报
回复
第一段代码: ** ** 枚举USB设备获取USB序列号 ** CLEAR #DEFINE GENERIC_WRITE 0x40000000 #DEFINE FILE_SHARE_WRITE 0x00000002 #DEFINE OPEN_EXISTING 3 #DEFINE INVALID_HANDLE_VALUE -1 #DEFINE BUFFER_SIZE 2048 #DEFINE FILE_DEVICE_USB 0x00000022 #DEFINE HCD_GET_DRIVERKEY_NAME 265 #DEFINE HCD_GET_ROOT_HUB_NAME 258 #DEFINE USB_GET_NODE_INFORMATION 258 #DEFINE USB_GET_NODE_CONNECTION_INFORMATION 259 #DEFINE USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION 260 #DEFINE USB_GET_NODE_CONNECTION_NAME 261 #DEFINE METHOD_BUFFERED 0 #DEFINE FILE_ANY_ACCESS 0 #DEFINE IOCTL_GET_HCD_DRIVERKEY_NAME CTL_CODE(FILE_DEVICE_USB,; HCD_GET_DRIVERKEY_NAME,; METHOD_BUFFERED,; FILE_ANY_ACCESS) #DEFINE IOCTL_USB_GET_ROOT_HUB_NAME CTL_CODE(FILE_DEVICE_USB,; HCD_GET_ROOT_HUB_NAME,; METHOD_BUFFERED,; FILE_ANY_ACCESS) #DEFINE IOCTL_USB_GET_NODE_INFORMATION CTL_CODE(FILE_DEVICE_USB,; USB_GET_NODE_INFORMATION,; METHOD_BUFFERED,; FILE_ANY_ACCESS) #DEFINE IOCTL_USB_GET_NODE_CONNECTION_INFORMATION CTL_CODE(FILE_DEVICE_USB,; USB_GET_NODE_CONNECTION_INFORMATION,; METHOD_BUFFERED,; FILE_ANY_ACCESS) #DEFINE IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION CTL_CODE(FILE_DEVICE_USB,; USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,; METHOD_BUFFERED,; FILE_ANY_ACCESS) #DEFINE IOCTL_USB_GET_NODE_CONNECTION_NAME CTL_CODE(FILE_DEVICE_USB,; USB_GET_NODE_CONNECTION_NAME,; METHOD_BUFFERED,; FILE_ANY_ACCESS) #DEFINE USB_STRING_DESCRIPTOR_TYPE 0x03 DECLARE LONG CreateFile IN Kernel32 STRING@, LONG, LONG, STRING@, LONG, LONG, LONG DECLARE LONG CloseHandle IN Kernel32 LONG DECLARE LONG DeviceIoControl IN Kernel32 LONG, LONG, STRING@, LONG, STRING@, LONG, LONG@, LONG DECLARE LONG GetSystemDefaultLangID IN Kernel32 CREATE CURSOR dt (Property C(10), Value C(50)) EnumUsbDevice() GO TOP BROWSE CLEAR DLLS RETURN FUNCTION CTL_CODE(DeviceType, Function, Method, Access) * #define CTL_CODE(DeviceType, Function, Method, Access) ( * ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) * ) RETURN BITOR(BITLSHIFT(DeviceType, 16), BITLSHIFT(Access, 14), BITLSHIFT(Function, 2), Method) ENDFUNC FUNCTION EnumUsbDevice() LOCAL i, hHCD, szHCDPath, szHUBName FOR i = 0 TO 9 && 假定最多有10个主控 szHCDPath = "\\.\HCD" + TRANSFORM(i) hHCD = CreateFile(@szHCDPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0) IF hHCD == INVALID_HANDLE_VALUE EXIT ENDIF szHUBName = GetHUBName(hHCD) IF !ISNULL(szHUBName) GetHUB(szHUBName) && 取集线器数据 ENDIF CloseHandle(hHCD) ENDFOR ENDFUNC FUNCTION GetHUB(szHUBName) LOCAL i, hHUB, szHUBPath, szBuffer, dwReturnedSize, dbPorts szHUBPath = "\\.\" + szHUBName hHUB = CreateFile(@szHUBPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0) ?szHUBPath IF hHUB != INVALID_HANDLE_VALUE szBuffer = REPLICATE(0h00, BUFFER_SIZE) dwReturnedSize = 0 IF DeviceIoControl(hHUB,; IOCTL_USB_GET_NODE_INFORMATION,; @szBuffer, BUFFER_SIZE,; @szBuffer, BUFFER_SIZE,; @dwReturnedSize,; 0) != 0 * USB_NODE_INFORMATION 结构 * 端口数,dbPports = szBuffer->u.HubInformation.HubDescriptor.bNumberOfPorts dbPorts = CTOBIN(SUBSTR(szBuffer, 7, 1), "1RS") FOR i = 1 TO dbPorts GetHUBPort(hHUB, i) && 取各端口数据 ENDFOR ENDIF CloseHandle(hHUB) ENDIF ENDFUNC FUNCTION GetHUBPort(hHUB, dbPortIndex) LOCAL szBuffer, dwReturnedSize, ddConnectionIndex LOCAL iManufacturer, iProduct, iSerialNumber, idVendor, idProduct, bcdDevice LOCAL DeviceIsHub, ConnectionStatus * USB_NODE_CONNECTION_INFORMATION 结构 * 端口号,szBuffer->ConnectionIndex = dbPortIndex szBuffer = BINTOC(dbPortIndex, "4RS") + REPLICATE(0h00, BUFFER_SIZE - 4) dwReturnedSize = 0 IF DeviceIoControl(hHUB,; IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,; @szBuffer, BUFFER_SIZE,; @szBuffer, BUFFER_SIZE,; @dwReturnedSize,; 0) != 0 ddConnectionIndex = CTOBIN(LEFT(szBuffer, 4), "4RS") IF ddConnectionIndex > 0 * 集线器标志,szBuffer->DeviceIsHub DeviceIsHub = CTOBIN(SUBSTR(szBuffer, 25, 1), "1RS") IF DeviceIsHub == 1 && 是集线器 szBuffer = BINTOC(dbPortIndex, "4RS") + REPLICATE(0h00, BUFFER_SIZE - 4) dwReturnedSize = 0 IF DeviceIoControl(hHUB,; IOCTL_USB_GET_NODE_CONNECTION_NAME,; @szBuffer, BUFFER_SIZE,; @szBuffer, BUFFER_SIZE,; @dwReturnedSize,; 0) != 0 szBuffer = RTRIM(STRCONV(SUBSTR(szBuffer, 9, dwReturnedSize - 8), 6), 0h00, " ") GetHUB(szBuffer) && 遍历集线器 ENDIF ELSE * szBuffer->ConnectionStatus ConnectionStatus = CTOBIN(SUBSTR(szBuffer, 32, 1), "1RS") IF ConnectionStatus == 1 * USB_NODE_CONNECTION_INFORMATION 结构 idVendor = CTOBIN(SUBSTR(szBuffer, 13, 2) + 0h0000, "4RS") idProduct = CTOBIN(SUBSTR(szBuffer, 15, 2) + 0h0000, "4RS") bcdDevice = CTOBIN(SUBSTR(szBuffer, 17, 2) + 0h0000, "4RS") INSERT INTO dt VALUES ("供应商ID", IIF(idVendor > 0, TRANSFORM(idVendor), "NULL")) INSERT INTO dt VALUES (" 产品ID", IIF(idProduct > 0, TRANSFORM(idProduct), "NULL")) INSERT INTO dt VALUES ("设备编码", IIF(bcdDevice > 0, TRANSFORM(bcdDevice), "NULL")) * iManufacturer、iProduct 和 iSerialNumber 域指向一个串描述符, * 该串描述符用人类可读的语言描述设备生产厂商、产品和序列号。 iManufacturer = CTOBIN(SUBSTR(szBuffer, 19, 1), "1RS") iProduct = CTOBIN(SUBSTR(szBuffer, 20, 1), "1RS") iSerialNumber = CTOBIN(SUBSTR(szBuffer, 21, 1), "1RS") INSERT INTO dt VALUES ("厂商描述", GetStringDescriptor(hHUB, ddConnectionIndex, iManufacturer)) INSERT INTO dt VALUES ("产品描述", GetStringDescriptor(hHUB, ddConnectionIndex, iProduct)) INSERT INTO dt VALUES (" 序列号", GetStringDescriptor(hHUB, ddConnectionIndex, iSerialNumber)) INSERT INTO dt VALUES ("", "") ENDIF ENDIF ENDIF ENDIF ENDFUNC FUNCTION GetHCDDriverKeyName(hHCD) LOCAL szBuffer, dwReturnedSize szBuffer = REPLICATE(0h00, BUFFER_SIZE) dwReturnedSize = 0 IF DeviceIoControl(hHCD,; IOCTL_GET_HCD_DRIVERKEY_NAME,; @szBuffer, BUFFER_SIZE,; @szBuffer, BUFFER_SIZE,; @dwReturnedSize,; 0) != 0 szBuffer = STRCONV(SUBSTR(szBuffer, 5, dwReturnedSize - 4), 6) RETURN RTRIM(szBuffer, 0h00, " ") ENDIF RETURN NULL ENDFUNC FUNCTION GetHUBName(hHostController) LOCAL szBuffer, dwReturnedSize szBuffer = REPLICATE(0h00, BUFFER_SIZE) dwReturnedSize = 0 IF DeviceIoControl(hHostController,; IOCTL_USB_GET_ROOT_HUB_NAME,; @szBuffer, BUFFER_SIZE,; @szBuffer, BUFFER_SIZE,; @dwReturnedSize,; 0) != 0 szBuffer = STRCONV(SUBSTR(szBuffer, 5, dwReturnedSize - 4), 6) RETURN RTRIM(szBuffer, 0h00, " ") ENDIF RETURN NULL ENDFUNC
sych11 2018-03-01
  • 打赏
  • 举报
回复
第二段代码 Clear Local ans, DeviceNumber, DiskGUID, hSetup, Success, i, dia, da, didd #Define GUID_DEVINTERFACE_USB_DEVICE "A5DCBF10-6530-11D2-901F-00C04FB951ED" #Define INVALID_HANDLE_VALUE -1 #Define BUFFER_SIZE 2048 #Define REG_SZ 1 #Define GENERIC_WRITE 0x40000000 #Define FILE_SHARE_WRITE 0x2 #Define OPEN_EXISTING 0x3 #Define FILE_ATTRIBUTE_NORMAL 128 #Define DIGCF_PRESENT 0x2 #Define DIGCF_DEVICEINTERFACE 0x10 Declare Integer SetupDiGetClassDevs In "setupapi.dll" ; String @ClassGuid, ; Integer Enumerator, ; Integer hwndParent, ; Integer Flags Declare Integer SetupDiEnumDeviceInterfaces In "setupapi.dll" ; Integer DeviceInfoSet, ; Integer DeviceInfoData, ; String InterfaceClassGuid, ; Integer MemberIndex, ; String @DeviceInterfaceData Declare Integer SetupDiDestroyDeviceInfoList In "setupapi.dll" ; Integer DeviceInfoSet Declare Integer SetupDiGetDeviceInterfaceDetail In "setupapi.dll" ; Integer DeviceInfoSet, ; String DeviceInterfaceData, ; String @DeviceInterfaceDetailData, ; Integer DeviceInterfaceDetailDataSize, ; Integer @RequiredSize, ; String @DeviceInfoData Declare INTEGER CreateFile IN kernel32; STRING lpFileName,INTEGER dwDesiredAccess,INTEGER dwShareMode,; INTEGER lpSecurityAttr,INTEGER dwCreationDisp,INTEGER dwFlagsAndAttrs,; INTEGER hTemplateFile DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject DECLARE INTEGER DeviceIoControl IN kernel32; INTEGER hDevice,; INTEGER dwIoControlCode,; STRING @ lpInBuffer,; INTEGER nInBufferSize,; STRING @ lpOutBuffer,; INTEGER nOutBufferSize,; INTEGER @ lpBytesReturned,; INTEGER lpOverlapped DiskGUID = guid(GUID_DEVINTERFACE_USB_DEVICE) hSetup = SetupDiGetClassDevs(DiskGUID, 0, 0, Bitor(DIGCF_PRESENT, DIGCF_DEVICEINTERFACE)) If hSetup <> INVALID_HANDLE_VALUE i = 0 Success=1 Do While Success<>0 dia = dword2str(28)+Replicate(Chr(0),16)+Replicate(Chr(0),4)+Replicate(Chr(0),4) Success = SetupDiEnumDeviceInterfaces(hSetup, 0, DiskGUID, i, @dia) If Success <> 0 da = dword2str(28)+Replicate(Chr(0),16)+Replicate(Chr(0),4)+Replicate(Chr(0),4) didd = dword2str(5)+Replicate(Chr(0),BUFFER_SIZE) nBytes = BUFFER_SIZE nRequiredSize = 0 m.err1 = SetupDiGetDeviceInterfaceDetail(hSetup, dia, @didd, nBytes, @nRequiredSize, @da) didd="\\.\"+SUBSTR(didd,9) didd=LEFT(didd,AT(CHR(0),didd)-1) ?didd Endif i = i+1 Enddo Endif SetupDiDestroyDeviceInfoList(hSetup) Return Function Guid Lparameter m.guid m.guid=Chrtran(m.guid,"{}-","") Return ; word2str(Eval("0x"+Substr(m.guid,5,4)))+ ; word2str(Eval("0x"+Substr(m.guid,1,4)))+ ; word2str(Eval("0x"+Substr(m.guid,9,4)))+ ; word2str(Eval("0x"+Substr(m.guid,13,4)))+ ; chr(Eval("0x"+Substr(m.guid,17,2)))+ ; chr(Eval("0x"+Substr(m.guid,19,2)))+ ; chr(Eval("0x"+Substr(m.guid,21,2)))+ ; chr(Eval("0x"+Substr(m.guid,23,2)))+ ; chr(Eval("0x"+Substr(m.guid,25,2)))+ ; chr(Eval("0x"+Substr(m.guid,27,2)))+ ; chr(Eval("0x"+Substr(m.guid,29,2)))+ ; chr(Eval("0x"+Substr(m.guid,31,2))) Function dword2str Lparameter m.wrd Local m.wrd1, m.st1 m.wrd1=m.wrd%4294967296 Declare RtlMoveMemory In Win32API String @st1, Integer @wrd, Integer Cnt m.st1=Chr(0)+Chr(0)+Chr(0)+Chr(0) RtlMoveMemory(@m.st1, @m.wrd, 4) Return m.st1 Function word2str Lparameter m.wrd Local m.wrd1, m.st1 m.wrd1=m.wrd%65536 Declare RtlMoveMemory In Win32API String @st1, Integer @wrd, Integer Cnt m.st1=Chr(0)+Chr(0) RtlMoveMemory(@m.st1, @m.wrd, 2) Return m.st1
sych11 2018-03-01
  • 打赏
  • 举报
回复
FUNCTION GetStringDescriptor(hHUB, dbConnectionIndex, dbDescriptorIndex) LOCAL szBuffer, dwReturnedSize, dbLength, dbDescriptorType IF (dbConnectionIndex < 1) OR (dbDescriptorIndex < 1) RETURN "NULL" ENDIF * szBuffer->ConnectionIndex = ConnectionIndex * szBuffer->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | DescriptorIndex * szBuffer->SetupPacket.wIndex = GetSystemDefaultLangID() * szBuffer->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_DESCRIPTOR_REQUEST)) * sizeof(USB_DESCRIPTOR_REQUEST) = 12 szBuffer = BINTOC(dbConnectionIndex, "4RS") + 0h0000; + BINTOC(BITOR(BITLSHIFT(USB_STRING_DESCRIPTOR_TYPE, 8), dbDescriptorIndex), "2RS"); + BINTOC(0, "2RS"); + BINTOC(BUFFER_SIZE - 12, "2RS") + REPLICATE(0h00, BUFFER_SIZE - 12) dwReturnedSize = 0 IF DeviceIoControl(hHUB,; IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,; @szBuffer, BUFFER_SIZE,; @szBuffer, BUFFER_SIZE,; @dwReturnedSize,; 0) != 0 * USB_STRING_DESCRIPTOR 结构 dbLength = CTOBIN(SUBSTR(szBuffer, 13, 1), "1RS") dbDescriptorType = CTOBIN(SUBSTR(szBuffer, 14, 1), "1RS") IF (dwReturnedSize > 1) AND (dbLength == dwReturnedSize - 12); AND (dbDescriptorType == USB_STRING_DESCRIPTOR_TYPE); AND (dbLength % 2 == 0) RETURN RTRIM(STRCONV(SUBSTR(szBuffer, 15, dbLength), 6), 0h00, " ") ELSE RETURN "NULL" ENDIF ENDIF RETURN "NULL" ENDFUNC

2,741

社区成员

发帖
与我相关
我的任务
社区描述
VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
社区管理员
  • VFP社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧