DisplayHID.frm
Private Sub Form_Load()
'   Look for HID Devices.
'
'   Declare the data structures used
Dim HidGuid As Guid
Dim DeviceInterfaceData As Device_Interface_Data
Dim FunctionClassDeviceData As Device_Interface_Detail
Dim ThisHIDdevice As HidD_Attributes
Dim Buffer(100) As Byte
Dim Success As Boolean

'   First, get my class identifier
Call HidD_GetHidGuid(HidGuid.Data(0))
'
'   Get a handle for the Plug and Play node and request currently active HID devices
PnPHandle& = SetupDiGetClassDevs(HidGuid.Data(0), 0, 0, &H12)
If (PnPHandle& = -1) Then ErrorExit ("Could not attach to PnP node")
'
'   Lets look for a maximum of 20 HID devices
For HIDdevice& = 0 To 19
    DeviceInterfaceData.cbsize = 28 'Length of data structure in bytes
'   Is there a HID device at this table entry
    If SetupDiEnumDeviceInterfaces(PnPHandle&, 0, HidGuid.Data(0), HIDdevice&, DeviceInterfaceData.cbsize) Then
'   There is a device here, get it's name
        FunctionClassDeviceData.cbsize = 5
        Success = SetupDiGetDeviceInterfaceDetail(PnPHandle&, DeviceInterfaceData.cbsize, _
           FunctionClassDeviceData.cbsize, UBound(FunctionClassDeviceData.DataPath), BytesReturned&, 0)
        If (Success = 0) Then ErrorExit ("Could not find the system name for this HID device")
' Convert 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
        HidHandle& = CreateFile(HidName$, &HC0000000, 3, 0, 3, 0, 0)
        If (HidHandle& = -1) Then ErrorExit ("Could not open HID device")
' Get VID and PID for display
        Success = HidD_GetAttributes(HidHandle&, ThisHIDdevice.cbsize)
        temp$ = " VID = " & TwoHexCharacters$(ThisHIDdevice.VendorID(1)) & TwoHexCharacters$(ThisHIDdevice.VendorID(0))
' Get Manufacturers name if present
        If HidD_GetManufacturerString(HidHandle&, AddressFor(Buffer(0)), UBound(Buffer)) Then
            temp$ = temp$ & " '": i& = 0
            Do While Buffer(i&) <> 0: temp$ = temp$ & Chr(Buffer(i&)): i& = i& + 2: Loop
            temp$ = temp$ & "'"
            End If
        temp$ = temp$ & ", PID = " & TwoHexCharacters$(ThisHIDdevice.ProductID(1)) & TwoHexCharacters$(ThisHIDdevice.ProductID(0))
' Get Product name if present
        If HidD_GetProductString(HidHandle&, AddressFor(Buffer(0)), UBound(Buffer)) Then
            temp$ = temp$ & " '": i& = 0
            Do While Buffer(i&) <> 0: temp$ = temp$ & Chr(Buffer(i&)): i& = i& + 2: Loop
            temp$ = temp$ & "'"
            End If
' Add entry to Listbox
        Display.AddItem temp$
        Call CloseHandle(HidHandle&)
        End If 'SetupDiEnumDeviceInterfaces
    Next HIDdevice&
Call CloseHandle(PnPHandle&)
End Sub