Using the SysInfo control in VB6 to detect when a USB device has been plugged/unplugged

usbvb6

I have added a SysInfo control to my form and, to test, am trying to produce a text message when a USB card reader is plugged or unplugged.

Private Sub SysInfo1_ConfigChanged(ByVal OldConfigNum As Long, ByVal NewConfigNum As Long)

    ShowText "The system configuration has changed"

End Sub

I have tried a similar test message with the DisplayChanged event and successfully receive that message when the monitor resolution is altered, but I can't for the life of me get the above to work. I was under the impression that the ConfigChanged event should occur when a USB device is added/removed. I am including the SysInfo.ocx with the application and testing on a Vista machine. Thanks

Best Answer

Use the SysInfo_DeviceArrival event to detect insertion of a USB drive. SysInfo_DeviceRemoveComplete fires when it's removed.

There are a lot of events for the SysInfo control, so I wrote a test program to see what events happen. I do this often when working with a control that I haven't used before. The program that exercises the SysInfoControl is included below.

I made this by using the dropdown lists at the top of the VB6 code editing window: Select the SysInfo control in the left one, the select each of the events displayed in the right one. Add a Debug.Print statement to each, run the project, and plug in your USB device.

Also, if you select the SysInfo control in the form designer, then press F1, the MSDN library help should display which includes descriptions of all of the events and how to interpret their parameters. At least, it does on my machine. (I hate it when people say that, but it applies here because the MSDN docs have to be separately installed.)

If that doesn't work, I found the docs online here.

Option Explicit

Private Sub SysInfo_ConfigChangeCancelled()
    Debug.Print Now() & ": " & "SysInfo_ConfigChangeCancelled"
End Sub

Private Sub SysInfo_ConfigChanged(ByVal OldConfigNum As Long, ByVal NewConfigNum As Long)
    Debug.Print Now() & ": " & "SysInfo_ConfigChanged"
End Sub

Private Sub SysInfo_DeviceArrival(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
    Debug.Print Now() & ": " & "SysInfo_DeviceArrival"
End Sub

Private Sub SysInfo_DeviceOtherEvent(ByVal DeviceType As Long, ByVal EventName As String, ByVal DataPointer As Long)
    Debug.Print Now() & ": " & "SysInfo_DeviceOtherEvent"
End Sub

Private Sub SysInfo_DeviceQueryRemove(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long, Cancel As Boolean)
    Debug.Print Now() & ": " & "SysInfo_DeviceQueryRemove"
End Sub

Private Sub SysInfo_DeviceQueryRemoveFailed(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
    Debug.Print Now() & ": " & "SysInfo_DeviceQueryRemoveFailed"
End Sub

Private Sub SysInfo_DeviceRemoveComplete(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
    Debug.Print Now() & ": " & "SysInfo_DeviceRemoveComplete"
End Sub

Private Sub SysInfo_DeviceRemovePending(ByVal DeviceType As Long, ByVal DeviceID As Long, ByVal DeviceName As String, ByVal DeviceData As Long)
    Debug.Print Now() & ": " & "SysInfo_DeviceRemovePending"
End Sub

Private Sub SysInfo_DevModeChanged()
    Debug.Print Now() & ": " & "SysInfo_DevModeChanged"
End Sub

Private Sub SysInfo_DisplayChanged()
    Debug.Print Now() & ": " & "SysInfo_DisplayChanged"
End Sub

Private Sub SysInfo_PowerQuerySuspend(Cancel As Boolean)
    Debug.Print Now() & ": " & "SysInfo_PowerQuerySuspend"
End Sub

Private Sub SysInfo_PowerResume()
    Debug.Print Now() & ": " & "SysInfo_PowerResume"
End Sub

Private Sub SysInfo_PowerStatusChanged()
    Debug.Print Now() & ": " & "SysInfo_PowerStatusChanged"
End Sub

Private Sub SysInfo_PowerSuspend()
    Debug.Print Now() & ": " & "SysInfo_PowerSuspend"
End Sub

Private Sub SysInfo_QueryChangeConfig(Cancel As Boolean)
    Debug.Print Now() & ": " & "SysInfo_QueryChangeConfig"
End Sub

Private Sub SysInfo_SettingChanged(ByVal Item As Integer)
    Debug.Print Now() & ": " & "SysInfo_SettingChanged"
End Sub

Private Sub SysInfo_SysColorsChanged()
    Debug.Print Now() & ": " & "SysInfo_SysColorsChanged"
End Sub

Private Sub SysInfo_TimeChanged()
    Debug.Print Now() & ": " & "SysInfo_TimeChanged"
End Sub
Related Topic