'补充:相关代码如下: DeviceInfoSet = SetupDiGetClassDevs(HidGuid, vbNullString, 0, DIGCF_PRESENT Or DIGCF_INTERFACRDEVICE) If DeviceInfoSet = INVALID_HANDLE_VALUE Then Text1.Text = Text1.Text & "查找USB设备类失败!" & vbCrLf Exit Sub End If
MyDeviceInterfaceData.cbSize = LenB(MyDeviceInterfaceData) For MemberIndex = 0 To 9 Result = SetupDiEnumDeviceInterfaces(DeviceInfoSet, 0, HidGuid, MemberIndex, MyDeviceInterfaceData) If Result = 0 Then Text1.Text = Text1.Text & "查找USB设备数据结构失败!" & vbCrLf Exit Sub End If Result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, MyDeviceInterfaceData, 0, 0, Needed, 0) DetailData = Needed MyDeviceInterfaceDetailData.cbSize = Len(MyDeviceInterfaceDetailData) ReDim DetailDataBuffer(Needed) Call RtlMoveMemory(DetailDataBuffer(0), MyDeviceInterfaceDetailData, 4) Result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, MyDeviceInterfaceData, VarPtr(DetailDataBuffer(0)), DetailData, Needed, 0) If Result = 0 Then Text1.Text = Text1.Text & "查找设备路径时出错!" & vbCrLf Else DevicePathName = CStr(DetailDataBuffer()) DevicePathName = StrConv(DevicePathName, vbUnicode) DevicePathName = Right(DevicePathName, Len(DevicePathName) - 4) 'Text1.Text = Text1.Text & DevicePathName & vbCrLf ' HidDevice = CreateFile(DevicePathName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0) HidDevice = CreateFile(DevicePathName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0) If HidDevice <> INVALID_HANDLE_VALUE Then DeviceAttributes.Size = LenB(DeviceAttributes) Result = HidD_GetAttributes(HidDevice, DeviceAttributes) If DeviceAttributes.ProductID = MyProductID And DeviceAttributes.VendorID = MyVendorID Then Text1.Text = Text1.Text & "经过识别PID与VID,,找到了正确的设备!成功了!" & vbCrLf End If Result = HidD_GetPreparsedData(HidDevice, PreparsedData) Result = HidP_GetCaps(PreparsedData, Capabilities) InputReportByteLength = Capabilities.InputReportByteLength OutputReportByteLength = Capabilities.OutputReportByteLength ReDim ReadBuffer(InputReportByteLength - 1) ReDim SendBuffer(OutputReportByteLength - 1) Timer1.Enabled = True Exit For Else Text1.Text = Text1.Text & "第" & (MemberIndex + 1) & "个设备无法打开!" & vbCrLf End If End If Next MemberIndex
Exit Sub errordeal: Text1.Text = Text1.Text & "未找到相应的USB设备或设备打开失败,请检查!" & vbCrLf
'补充:相关代码如下: DeviceInfoSet = SetupDiGetClassDevs(HidGuid, vbNullString, 0, DIGCF_PRESENT Or DIGCF_INTERFACRDEVICE)
If DeviceInfoSet = INVALID_HANDLE_VALUE Then
Text1.Text = Text1.Text & "查找USB设备类失败!" & vbCrLf
Exit Sub
End If
MyDeviceInterfaceData.cbSize = LenB(MyDeviceInterfaceData)
For MemberIndex = 0 To 9
Result = SetupDiEnumDeviceInterfaces(DeviceInfoSet, 0, HidGuid, MemberIndex, MyDeviceInterfaceData)
If Result = 0 Then
Text1.Text = Text1.Text & "查找USB设备数据结构失败!" & vbCrLf
Exit Sub
End If
Result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, MyDeviceInterfaceData, 0, 0, Needed, 0)
DetailData = Needed
MyDeviceInterfaceDetailData.cbSize = Len(MyDeviceInterfaceDetailData)
ReDim DetailDataBuffer(Needed)
Call RtlMoveMemory(DetailDataBuffer(0), MyDeviceInterfaceDetailData, 4)
Result = SetupDiGetDeviceInterfaceDetail(DeviceInfoSet, MyDeviceInterfaceData, VarPtr(DetailDataBuffer(0)), DetailData, Needed, 0)
If Result = 0 Then
Text1.Text = Text1.Text & "查找设备路径时出错!" & vbCrLf
Else
DevicePathName = CStr(DetailDataBuffer())
DevicePathName = StrConv(DevicePathName, vbUnicode)
DevicePathName = Right(DevicePathName, Len(DevicePathName) - 4)
'Text1.Text = Text1.Text & DevicePathName & vbCrLf
' HidDevice = CreateFile(DevicePathName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
HidDevice = CreateFile(DevicePathName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, CREATE_ALWAYS, 0, 0)
If HidDevice <> INVALID_HANDLE_VALUE Then
DeviceAttributes.Size = LenB(DeviceAttributes)
Result = HidD_GetAttributes(HidDevice, DeviceAttributes)
If DeviceAttributes.ProductID = MyProductID And DeviceAttributes.VendorID = MyVendorID Then
Text1.Text = Text1.Text & "经过识别PID与VID,,找到了正确的设备!成功了!" & vbCrLf
End If
Result = HidD_GetPreparsedData(HidDevice, PreparsedData)
Result = HidP_GetCaps(PreparsedData, Capabilities)
InputReportByteLength = Capabilities.InputReportByteLength
OutputReportByteLength = Capabilities.OutputReportByteLength
ReDim ReadBuffer(InputReportByteLength - 1)
ReDim SendBuffer(OutputReportByteLength - 1)
Timer1.Enabled = True
Exit For
Else
Text1.Text = Text1.Text & "第" & (MemberIndex + 1) & "个设备无法打开!" & vbCrLf
End If
End If
Next MemberIndex
Exit Sub
errordeal:
Text1.Text = Text1.Text & "未找到相应的USB设备或设备打开失败,请检查!" & vbCrLf