I am writing a console app that needs to detect USB storage keys being
inserted and removed.
I have looked through the MS Help and decided I can use
RegisterDeviceNotification.
I lifted this snippet from the help files.
#include <windows.h>
#include <dbt.h>
HWND hWnd;
BOOL DoRegisterDeviceInterface(
GUID InterfaceClassGuid,
HDEVNOTIFY *hDevNotify
)
{
GUID DeviceGUID;
DWORD dRet = 0;
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
char szMsg[80];
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size =
sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = InterfaceClassGuid;
*hDevNotify = RegisterDeviceNotification( hWnd, &NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);
if(!*hDevNotify)
{
wsprintf(szMsg, "RegisterDeviceNotification failed: %d\n",
GetLastError());
MessageBox(hWnd, szMsg, "Registration", MB_OK);
return FALSE;
}
}
However I am stuck trying to fill out the DEV_BROADCAST_VOLUME
structure.
The variable InterfaceClassGuid in this snippet was passed in to the
function. I do not know how to find the device class GUID. Any help
would be appreciated.
Rhodri
You don't need RegisterDeviceNotification for storage devices. Just catch
WM_DEVICECHANGE. If you need any information besides the drive letter, you
do need to register, the GUIDs are in devguid.h
These are probably the most helpful to you:
DEFINE_GUID( GUID_DEVCLASS_DISKDRIVE, 0x4d36e967L, 0xe325, 0x11ce, 0xbf,
0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 );
DEFINE_GUID( GUID_DEVCLASS_USB, 0x36fc9e60L, 0xc465, 0x11cf, 0x80, 0x56,
0x44, 0x45, 0x53, 0x54, 0x00, 0x00 );
DEFINE_GUID( GUID_DEVCLASS_VOLUME, 0x71a27cddL, 0x812a, 0x11d0, 0xbe, 0xc7,
0x08, 0x00, 0x2b, 0xe2, 0x09, 0x2f );