获取USB设备信息,这个是可运行的代码,但有个先天缺陷

sych11 2018-05-08 08:54:58
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
DECLARE Long GetLastError IN WIN32API

**这种获取的GUID不行
DiskGUID = guid(GUID_DEVINTERFACE_USB_DEVICE)
**这种获取的GUID可以,但只能取到键盘的信息,可是怎么获得其他USB设备的信息呢?
**和以前发的代码只有这里不同
DECLARE HidD_GetHidGuid IN HID STRING @ HidGuid
DiskGUID=REPLICATE(CHR(0),16)
HidD_GetHidGuid(@DiskGUID)
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,5)
didd=LEFT(didd,AT(CHR(0),didd)-1)
hhcd=CreateFile(didd, GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)

Declare LONG HidD_GetProductString IN "HID.dll" LONG Handle,STRING @BufferPtr, LONG Length
Declare LONG HidD_GetSerialNumberString IN "HID.dll" LONG Handle , STRING @BufferPtr, LONG Length
Declare LONG HidD_GetManufacturerString IN "HID.dll" LONG Handle , STRING @BufferPtr, LONG Length
Declare LONG HidD_GetPhysicalDescriptor IN "HID.dll" LONG Handle , STRING @BufferPtr, LONG Length

STU=REPLICATE(CHR(0),256)
HidD_GetManufacturerString( hHcd,@STU,256)
STU=STRCONV(STU,6)
?LEFT(STU,AT(CHR(0),STU)-1)

STU=REPLICATE(CHR(0),256)
HidD_GetProductString( hHcd,@STU,256)
STU=STRCONV(STU,6)
?LEFT(STU,AT(CHR(0),STU)-1)

STU=REPLICATE(CHR(0),256)
HidD_GetSerialNumberString( hHcd,@STU,256)
?LEFT(STU,AT(CHR(0),STU)-1)

IF hhcd <>INVALID_HANDLE_VALUE
CloseHandle(hHcd)
endif
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
...全文
767 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

2,717

社区成员

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

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