Subclassing USB with MS. Visual Basic

0 comments
Save to del.icio.us 0 hits!

Subclassing is a way to intercept the Windows messages when the usb entry. With subclassing, we can change the information in our arbitrarily, in other words, if we can intercept the messages , we can replace it with a message / other messages in accordance with our wishes .. Subclassing (here) allows us to send an event that occurred or Message, we create a form that would identify or tell us that device has been detect (plugged in)

Step 1
On top Module
Dim OldProc As Long
Dim WHnd As Long 'Window handle

Step 2
Sub Module
Public Sub SubClass(ByVal iWnd As Long)
If (WHnd) Then Call UnSubClass
   OldProc = SetWindowLong(iWnd, GWL_WNDPROC, AddressOf
   WndProc)
   WHnd = iWnd
End Sub

This sub-function to intercept the form of our message, so we need to
called sub form with the form load event as below:

Private Sub Form_Load()
Call SubClass(Me.hWnd)
End Sub

'bout Sub Subclass
  • First, we determine / ascertain whether whether the window "new" has appear? If so, then the procedure will be called UnSubClass.
  • Second, by using the SetWindowLong API, we change the "setting" Write a function (new event) called WndProc, which we will described in Step 3.
  • Third, we do handle setting Window WHnd variable. This variable used to call the sub-subclass, and here we are to set the Form's handle.
Step3
On Function Module
Private Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
 ByVal wParam As Long, ByVal lParam As Long) As Long
 Dim DevBroadcastHead As DEV_BROADCAST_HDR
 Dim UMask As Long, Flags As Integer
If (uMsg = WM_DEVICECHANGE) Then
 Select Case wParam
  Case DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETE
    Call RtlMoveMemory(DevBroadcastHead, ByVal lParam, _
      Len(DevBroadcastHead))
    If (DevBroadcastHead.dbch_devicetype = _
      DBT_DEVTYP_VOLUME) Then
       Call GetDWord(ByVal _
        (lParam + Len(DevBroadcastHead)), UMask)
       Call GetWord(ByVal _
        (lParam + Len(DevBroadcastHead) + 4), Flags)
         MsgBox "Drive - " & UMaskString(UMask) & " " & _
          IIf(wParam = DBT_DEVICEARRIVAL, "Inserted", _
              "Ejected")
    End If
 End Select
End If
 WndProc = CallWindowProc(OldProc, hWnd, uMsg, wParam, lParam)
End Function

This function serves to change the events that occurred on the form. We make DEV_BROADCAST_HDR structure which contains precise information about the event devices reported by WM_DEVICECHANGE message. After we make sure that WM_DEVICECHANGE message sent, we use a Select Case statement to ascertain whether there are reports of (DBT_DEVICEARRIVAL) or (DBT_DEVICEREMOVECOMPLETE).

We use RtlMoveMemory to move data from device to the structure DEV_BROADCAST_HDR. Then, we check whether the disk plugged in / removed that is based on logic values. Based on this, we use the function and GetDWord GetWord (in MSVBVM60.dll) to get the value of the DWORD or WORD DEV_BROADCAST_HDR object and channel it into VB - umask and Flags. After all this process to make sure we can get back and using all the necessary values, we can use the statement Inline If to display the MessageBox containing "inserted" (True), or "ejected" (False).

 
Inserted Notification

Comments
0 comments
Do you have any suggestions? Add your comment. Please don't spam!
Subscribe to my feed

Post a Comment

Dont be shy.. leave your comment, track & review here :)