How to detect USB device insert.
Hello
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