I am trying to detect a USB disk drive being inserted within a Windows Service, I have done this as a normal Windows application. The problem is the following code doesn't work for volumes.
Registering the device notification:
DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
HDEVNOTIFY hDeviceNotify = NULL;
::ZeroMemory(¬ificationFilter, sizeof(notificationFilter));
notificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
notificationFilter.dbcc_classguid = ::GUID_DEVINTERFACE_VOLUME;
hDeviceNotify = ::RegisterDeviceNotification(g_serviceStatusHandle, ¬ificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE);
The code from the ServiceControlHandlerEx function:
case SERVICE_CONTROL_DEVICEEVENT:
PDEV_BROADCAST_HDR pBroadcastHdr = (PDEV_BROADCAST_HDR)lpEventData;
switch (dwEventType)
{
case DBT_DEVICEARRIVAL:
::MessageBox(NULL, "A Device has been plugged in.", "Pounce", MB_OK | MB_ICONINFORMATION);
switch (pBroadcastHdr->dbch_devicetype)
{
case DBT_DEVTYP_DEVICEINTERFACE:
PDEV_BROADCAST_DEVICEINTERFACE pDevInt = (PDEV_BROADCAST_DEVICEINTERFACE)pBroadcastHdr;
if (::IsEqualGUID(pDevInt->dbcc_classguid, GUID_DEVINTERFACE_VOLUME))
{
PDEV_BROADCAST_VOLUME pVol = (PDEV_BROADCAST_VOLUME)pDevInt;
char szMsg[80];
char cDriveLetter = ::GetDriveLetter(pVol->dbcv_unitmask);
::wsprintfA(szMsg, "USB disk drive with the drive letter '%c:' has been inserted.", cDriveLetter);
::MessageBoxA(NULL, szMsg, "Pounce", MB_OK | MB_ICONINFORMATION);
}
}
return NO_ERROR;
}
In a Windows application I am able to get the DBT_DEVTYP_VOLUME in dbch_devicetype, however this isn't present in a Windows Service implementation. Has anyone seen or heard of a solution to this problem, without the obvious, rewrite as a Windows application?