www.pudn.com > the PC Host examples .zip > DisplayHID.frm


VERSION 5.00 
Begin VB.Form DisplayWindow  
   BackColor       =   &H00FF8080& 
   Caption         =   "USB Design By Example: Display HID Devices" 
   ClientHeight    =   5595 
   ClientLeft      =   3000 
   ClientTop       =   2145 
   ClientWidth     =   8475 
   FillStyle       =   0  'Solid 
   ForeColor       =   &H8000000E& 
   LinkTopic       =   "Form1" 
   NegotiateMenus  =   0   'False 
   OLEDropMode     =   1  'Manual 
   ScaleHeight     =   5874.016 
   ScaleMode       =   0  'User 
   ScaleWidth      =   8475 
   ShowInTaskbar   =   0   'False 
   Begin VB.ListBox Display  
      BeginProperty Font  
         Name            =   "Courier New" 
         Size            =   8.25 
         Charset         =   0 
         Weight          =   400 
         Underline       =   0   'False 
         Italic          =   0   'False 
         Strikethrough   =   0   'False 
      EndProperty 
      Height          =   5310 
      Left            =   240 
      TabIndex        =   0 
      Top             =   120 
      Width           =   7935 
   End 
End 
Attribute VB_Name = "DisplayWindow" 
Attribute VB_GlobalNameSpace = False 
Attribute VB_Creatable = False 
Attribute VB_PredeclaredId = True 
Attribute VB_Exposed = False 
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