?
Solved

VB6 Detecting a removal of a hid device

Posted on 2014-12-18
8
Medium Priority
?
244 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 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 1000 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 58
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 49

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

801 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