1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
Public Function FindHIDInterface(VID As Integer, PID As Integer, UsagePage As Integer) As String
' This function searches the system HID tables for a device matching VID, PID and UsagePage
' If found then it returns the system name for the device (for a later open)
Dim HidGuid As GUID
Dim PnPHandle, HidHandle, HidEntry, BytesReturned, i As Long
Dim HidName, NameMatch As String
Dim Success, Found As Boolean
Dim DeviceInterfaceData As Device_Interface_Data
Dim FunctionClassDeviceData As Device_Interface_Detail
Dim Attributes As HIDD_ATTRIBUTES
Dim DataPointer As Long
Dim HidCapabilities As Hid_Capabilities
Dim temp As Integer
'
' Clear the name, return this empty string if the device is not found
NameMatch = ""
' First, get the HID class identifier
Call HidD_GetHidGuid(HidGuid)
' Get a handle for the Plug and Play node, request currently active HID devices
PnPHandle = SetupDiGetClassDevs(HidGuid, 0, 0, DIGCF_PRESENT Or DIGCF_DEVICEINTERFACE)
If (PnPHandle = INVALID_DEVICE_HANDLE) Then ErrorExit ("Could not attach to PnP node")
'
HidEntry = 0
Found = False
DeviceInterfaceData.cbSize = Len(DeviceInterfaceData)
' Step through the table of HID devices
Do While (SetupDiEnumDeviceInterfaces(PnPHandle, 0, HidGuid, HidEntry, DeviceInterfaceData) And Not (Found))
' There is a device here, get it's name
FunctionClassDeviceData.cbSize = 5 ' Minimum size, my structure can hold maximum size
Success = SetupDiGetDeviceInterfaceDetail(PnPHandle, DeviceInterfaceData, FunctionClassDeviceData, _
UBound(FunctionClassDeviceData.DataPath), BytesReturned, 0)
If (Success = 0) Then
ErrorExit ("Could not get the name of this HID device")
' Convert returned C string to Visual Basic String
HidName = ""
i = 0
Do While FunctionClassDeviceData.DataPath(i) <> 0
HidName = HidName & Chr$(FunctionClassDeviceData.DataPath(i))
i = i + 1
Loop
' Can now open this HID device for inspection
HidHandle = CreateFile(HidName, 0, FILE_SHARE_READWRITE, SA, OPEN_EXISTING, 0, 0)
If (HidHandle = INVALID_DEVICE_HANDLE) Then
ErrorExit ("Could not open HID device")
' Is it OUR HID device?
If HidD_GetAttributes(HidHandle, Attributes) Then
If ((Attributes.VendorID = VID) And (Attributes.ProductID = PID)) Then
' Is it the correct interface?
Success = HidD_GetPreparsedData(HidHandle, DataPointer)
If (Success) Then
End If
End If
End If
Call CloseHandle(HidHandle)
End If 'HidD_GetAttributes
HidEntry = HidEntry + 1 ' Check next entry
Loop 'SetupDiEnumDeviceInterfaces returns FALSE when there are no more entries
SetupDiDestroyDeviceInfoList (PnPHandle)
FindHIDInterface = NameMatch
End Function |
Partager