Solved

VB6 Detecting a removal of a hid device

Posted on 2014-12-18
8
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 40509514
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
ID: 40509588
Ah...I thought this was for VBA...the code I posted should drop right in.

Jim.
0
 

Author Comment

by:MKadric
ID: 40513905
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:MKadric
ID: 40516027
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 47

Expert Comment

by:Martin Liss
ID: 40551897
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
ID: 40551898
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 132
Spell Check in VB6 13 146
Problem to open text file 11 144
Set email body to html using vbscript 6 89
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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.

738 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