Solved

VB6 Detecting a removal of a hid device

Posted on 2014-12-18
8
184 Views
Last Modified: 2015-01-15
I am looking for an example code in Visul Basic 6 to detect a inseration and removal of a Hid device. NEED ASAP
0
Comment
Question by:MKadric
  • 3
  • 2
8 Comments
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
Comment Utility
Since you have no takers yet, I thought I might take a stab at this.  While most likely not a direct answer, this should get you headed in the right direction.

You need to use the RegisterDeviceNotification() WinAPI call to detect hardware changes.   I found this project on CodeProject:

http://www.codeproject.com/Articles/14500/Detecting-Hardware-Insertion-and-or-Removal

  Which has some code detail.   Then checking further, I found the VB Code below.   may or may not drop into VBA...I haven't gone through it in detail.  Time is very short at the moment, but this looked like an interesting question to check out while I was eating lunch quick and I was pretty sure you were not going to get many takers on this<g>.

Jim.

 'In a module
Option Explicit

Public Type GUID
    Data1(1 To 4) As Byte
    Data2(1 To 2) As Byte
    Data3(1 To 2) As Byte
    Data4(1 To 8) As Byte
End Type

Public Type DEV_BROADCAST_DEVICEINTERFACE
  dbcc_size As Long
  dbcc_devicetype As Long
  dbcc_reserved As Long
  dbcc_classguid As GUID
  dbcc_name As Long
End Type

Public Type DEV_BROADCAST_DEVICEINTERFACE2
  dbcc_size As Long
  dbcc_devicetype As Long
  dbcc_reserved As Long
  dbcc_classguid As GUID
  dbcc_name As String * 1024
End Type

Public Declare Function RegisterDeviceNotification Lib "user32.dll" _
            Alias "RegisterDeviceNotificationA" ( _
            ByVal hRecipient As Long, _
            NotificationFilter As Any, _
            ByVal Flags As Long) As Long
            
Public Declare Function UnregisterDeviceNotification Lib "user32.dll" _
             (ByVal hRecipient As Long) As Long
            
Public Const DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = &H4
Public Const DEVICE_NOTIFY_WINDOW_HANDLE = 0


 
Public Declare Sub CopyMemory Lib "kernel32" Alias _
    "RtlMoveMemory" (Destination As Any, Source As Any, _
    ByVal Length As Long)

Public Declare Function CallWindowProc Lib "user32" _
  Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
                           ByVal hWnd As Long, _
                           ByVal Msg As Long, _
                           ByVal wParam As Long, _
                           ByVal lParam As Long) As Long

Public Declare Function GetWindowLong Lib "user32" _
  Alias "GetWindowLongA" (ByVal hWnd As Long, _
                          ByVal nIndex As Long) As Long

Public Declare Function SetWindowLong Lib "user32" _
  Alias "SetWindowLongA" (ByVal hWnd As Long, _
                          ByVal nIndex As Long, _
                          ByVal dwNewLong As Long) As Long

Public Const GWL_WNDPROC = (-4)
Public Const WM_DEVICECHANGE = &H219
Public Const UNSAFE_REMOVE = &H1C

Public glngPrevWndProc As Long

Public Type DEV_BROADCAST_HDR
  dbch_size As Long
  dbch_devicetype As Long
  dbch_reserved As Long
End Type

Public Const DBT_CONFIGCHANGECANCELED As Long = 25
Public Const DBT_CONFIGCHANGED As Long = 24
Public Const DBT_CUSTOMEVENT As Long = 32744
Public Const DBT_DEVICEARRIVAL As Long = 32768
Public Const DBT_DEVICEQUERYREMOVE As Long = 32769
Public Const DBT_DEVICEQUERYREMOVEFAILED As Long = 32770
Public Const DBT_DEVICEREMOVECOMPLETE As Long = 32772
Public Const DBT_DEVICEREMOVEPENDING As Long = 32771
Public Const DBT_DEVICETYPESPECIFIC As Long = 32773
Public Const DBT_DEVNODES_CHANGED As Long = 7
Public Const DBT_QUERYCHANGECONFIG As Long = 23
Public Const DBT_USERDEFINED As Long = 65535

Public Const DBT_DEVTYP_OEM                 As Long = 0
Public Const DBT_DEVTYP_DEVNODE             As Long = 1
Public Const DBT_DEVTYP_VOLUME              As Long = 2
Public Const DBT_DEVTYP_PORT                As Long = 3
Public Const DBT_DEVTYP_NET                 As Long = 4
Public Const DBT_DEVTYP_DEVICEINTERFACE     As Long = 5
Public Const DBT_DEVTYP_HANDLE              As Long = 6

 
Public Function MyWindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Dim dbHdr As DEV_BROADCAST_HDR
    
    Dim dbDdb As DEV_BROADCAST_DEVICEINTERFACE2
    
    Select Case Msg
        Case WM_DEVICECHANGE
        
            Form1.List1.AddItem "WM_DEVICECHANGE " & Msg
   
            Select Case wParam
            
                Case DBT_CONFIGCHANGECANCELED
                    Form1.List1.AddItem "wParam = DBT_CONFIGCHANGECANCELED"
                    
                Case DBT_CONFIGCHANGED
                    Form1.List1.AddItem "wParam = DBT_CONFIGCHANGED"
                    
                Case DBT_CUSTOMEVENT
                    Form1.List1.AddItem "wParam = DBT_CUSTOMEVENT"
                    
                Case DBT_DEVICEARRIVAL
                    Form1.List1.AddItem "wParam = DBT_DEVICEARRIVAL"
                    
                     CopyMemory dbHdr, ByVal (lParam), Len(dbHdr)
                 
                        Select Case dbHdr.dbch_devicetype
                            Case DBT_DEVTYP_OEM
                                Form1.List1.AddItem "Device Type: DBT_DEVTYP_OEM"
                                
                            Case DBT_DEVTYP_DEVNODE
                                Form1.List1.AddItem "Device Type: DBT_DEVTYP_DEVNODE"
                                
                            Case DBT_DEVTYP_VOLUME
                                Form1.List1.AddItem "Device Type: DBT_DEVTYP_VOLUME"
                                
                            Case DBT_DEVTYP_PORT
                                Form1.List1.AddItem "Device Type: DBT_DEVTYP_PORT"
                                
                            Case DBT_DEVTYP_NET
                                Form1.List1.AddItem "Device Type: DBT_DEVTYP_NET"
                                
                            Case DBT_DEVTYP_DEVICEINTERFACE
                                Form1.List1.AddItem "Device Type: DBT_DEVTYP_DEVICEINTERFACE"
                                
                                CopyMemory dbDdb, ByVal (lParam), ByVal (dbHdr.dbch_size)
                                
                                Form1.List1.AddItem "Device Name: " & Mid(dbDdb.dbcc_name, 1, InStr(dbDdb.dbcc_name, Chr(0)))
                            
                            Case DBT_DEVTYP_HANDLE
                                Form1.List1.AddItem "Device Type: DBT_DEVTYP_HANDLE"
                                
                            Case Else
                                Form1.List1.AddItem "Device Type unknown: " & CStr(dbHdr.dbch_devicetype)
                                
                        End Select
                        
                 
                Case DBT_DEVICEQUERYREMOVE
                    Form1.List1.AddItem "wParam = DBT_DEVICEQUERYREMOVE"
                    
                Case DBT_DEVICEQUERYREMOVEFAILED
                    Form1.List1.AddItem "wParam = DBT_DEVICEQUERYREMOVEFAILED"
                    
                Case DBT_DEVICEREMOVECOMPLETE
                    Form1.List1.AddItem "wParam = DBT_DEVICEREMOVECOMPLETE"
                    
                Case DBT_DEVICEREMOVEPENDING
                    Form1.List1.AddItem "wParam = DBT_DEVICEREMOVEPENDING"
                    
                Case DBT_DEVICETYPESPECIFIC
                    Form1.List1.AddItem "wParam = DBT_DEVICETYPESPECIFIC"
                    
                Case DBT_DEVNODES_CHANGED
                    Form1.List1.AddItem "wParam = DBT_DEVNODES_CHANGED"
                    
                Case DBT_QUERYCHANGECONFIG
                    Form1.List1.AddItem "wParam = DBT_QUERYCHANGECONFIG"
                    
                Case DBT_USERDEFINED
                    Form1.List1.AddItem "wParam = DBT_USERDEFINED"
                    
                Case Else
                    Form1.List1.AddItem "wParam = unknown = " & wParam
            
            End Select
        
    End Select
  
 ' pass the rest messages onto VB's own Window Procedure
  MyWindowProc = CallWindowProc(glngPrevWndProc, hWnd, Msg, wParam, lParam)
End Function


Public Function DoRegisterDeviceInterface(hWnd As Long, ByRef hDevNotify As Long) As Boolean

    Dim NotificationFilter As DEV_BROADCAST_DEVICEINTERFACE
        
    NotificationFilter.dbcc_size = Len(NotificationFilter)
    NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE
    
    hDevNotify = RegisterDeviceNotification(hWnd, NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE Or DEVICE_NOTIFY_ALL_INTERFACE_CLASSES)
    
    If hDevNotify = 0 Then
        MsgBox "RegisterDeviceNotification failed: " & CStr(Err.LastDllError), vbOKOnly
        DoRegisterDeviceInterface = False
        Exit Function
    End If
    
    DoRegisterDeviceInterface = True
End Function

Open in new window


 and in a form, you could do this:

'In a form
Option Explicit

Dim hDevNotify As Long

Private Sub Form_Load()
  Call DoRegisterDeviceInterface(Me.hWnd, hDevNotify)
   glngPrevWndProc = GetWindowLong(Me.hWnd, GWL_WNDPROC)
   SetWindowLong Me.hWnd, GWL_WNDPROC, AddressOf MyWindowProc
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If hDevNotify <> 0 Then
        Call UnregisterDeviceNotification(hDevNotify)
    End If
   'pass control back to previous windows
   SetWindowLong Me.hWnd, GWL_WNDPROC, glngPrevWndProc
End Sub

Open in new window

0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
Ah...I thought this was for VBA...the code I posted should drop right in.

Jim.
0
 

Author Comment

by:MKadric
Comment Utility
I found something on the internet waiting for someone to answer this.  I am now ready to try this one and see if it works better than the one I found Thank you I will let you know in the next couple of days how it went.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:MKadric
Comment Utility
I have created a project with the code above, I guess I just don't understand what I am to do with it.  Can you explain a little more? I have attached the project
Vb6-Hid-Notification.zip
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
I've requested that this question be deleted for the following reason:

The question has either no comments or not enough useful information to be called an "answer".
0
 

Author Closing Comment

by:MKadric
Comment Utility
Although this did not give me the answer I was looking for it sent me in the direction I needed to be in.  Thank you
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now