I'm not sure in which kernel version this was implemented, but the /sys/block/*
entries are symlinks to the devices.
In other words, /sys/block/sdb
symlinks to a different directory, and its name contains the USB device ID.
$ file /sys/block/sdb
/sys/block/sdb: symbolic link to `../devices/pci0000:00/0000:00:02.1/usb1/1-1/1-1.1/1-1.1:1.0/host31/target31:0:0/31:0:0:0/block/sdb'
USB version and port here---^^^^^
The 1-1.1
is the interesting part, denoting usb1-port 1.device 1
. When plugged into a hub, another level is added: 1-2.3.1
, denoting usb1-port 2.port 3.device 1
.
Pseudocode:
get partition name # e.g. /dev/sdb1
get disk name # that would be /dev/sdb
get your basename # sdb
see where /sys/block/$your_basename points to # e.g. ../devices/blah/blah/1-2.1/blah
get the longest substring matching "\d-\d+(.\d+)*" # e.g. 1-2.1
that is the device id you want
/sys/bus/usb/devices/$device_id/ has all kinds of information about it
the ID corresponds to hardware USB ports
Working example script in bash.
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
Best Answer
Ok, so I was able to answer one of my own questions: Is there a way to make Windows notify me of both volume removals?
Yes - even though windows sends only one
DBT_DEVTYP_VOLUME
WM_DEVICECHANGE
message, you actually do get notified of both volume removals - but, as always, the answer lies deep down buried in MSDN:So, all I had to do was ignore the example function that Microsoft gives in one of their samples,
And replace it with a function that interprets the mask for all drives affected. So the one message I was getting was indeed for both volumes, and both volume drive letters were available in the mask.
I still have the other question to answer though - how can the two messages (
DBT_DEVTYP_DEVICEINTERFACE
andDBT_DEVTYP_VOLUME
) be correlated?