EDN Admin
Well-known member
Hi guys,
I need to read the disk drive letter when a USB drive is inserted in a windows service.
The code I used ina sample MFC program raises three events for DBT_DEVICEARRIVAL, two for DBT_DEVTYP_DEVICEINTERFACE and one for DBT_DEVTYP_VOLUME.
But the I get only two events in a windows service for DBT_DEVTYP_DEVICEINTERFACE. I do not get the drive letter because DBT_DEVTYP_VOLUME event is never raised. Here is the Registration code and event handler code.
Is there any reason for this behaviour???
<font color="#0000ff" size=2>
bool</font><font size=2> CeTCaptureControlService::RegisterForUsbMessages(</font><font color="#0000ff" size=2>void</font><font size=2>)
{ </font><font size=2>
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
HDEVNOTIFY hDeviceNotify = NULL;
</font><font color="#0000ff" size=2>static</font> <font color="#0000ff" size=2>const</font><font size=2> GUID GuidDevInterfaceList[] =
{
{ 0xa5dcbf10, 0x6530, 0x11d2, { 0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed } },
{ 0x53f56307, 0xb6bf, 0x11d0, { 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },
{ 0x4d1e55b2, 0xf16f, 0x11Cf, { 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } },
{ 0xad498944, 0x762f, 0x11d0, { 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } }
};
ZeroMemory(&NotificationFilter, </font><font color="#0000ff" size=2>sizeof</font><font size=2>(NotificationFilter));
NotificationFilter.dbcc_size = </font><font color="#0000ff" size=2>sizeof</font><font size=2>(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
</font><font color="#0000ff" size=2>for</font><font size=2> (</font><font color="#0000ff" size=2>int</font><font size=2> i = 0; i < </font><font color="#0000ff" size=2>sizeof</font><font size=2>(GuidDevInterfaceList); i++)
{
NotificationFilter.dbcc_classguid = GuidDevInterfaceList;
hDeviceNotify = RegisterDeviceNotification(</font><font color="#008000" size=2>/*m_hWnd*/</font><font size=2>m_hServiceStatus, &NotificationFilter,
DEVICE_NOTIFY_SERVICE_HANDLE</font><font color="#008000" size=2>/*DEVICE_NOTIFY_WINDOW_HANDLE*/</font><font size=2>);
</font><font color="#0000ff" size=2>if</font><font size=2> (hDeviceNotify == NULL)
{
ATLTRACE2(</font><font color="#a31515" size=2>"Device Notify registration failed with error %d.n"</font><font size=2>, GetLastError());
</font><font color="#0000ff" size=2>this</font><font size=2>->LogMessage(</font><font color="#a31515" size=2>"Device Notify registration failed."</font><font size=2>, </font><font color="#0000ff" size=2>true</font><font size=2>, 0);
</font><font color="#0000ff" size=2>return</font> <font color="#0000ff" size=2>false</font><font size=2>;
}
}
</font><font color="#008000" size=2>//END Register Code </font><font size=2>
</font><font color="#0000ff" size=2>return</font> <font color="#0000ff" size=2>true</font><font size=2>;
} </font>///HANDLER code
<font size=2>
DWORD CService::Handler(DWORD dwOpcode, DWORD evtype, PVOID evdata, PVOID Context)
{
</font><font color="#0000ff" size=2>switch</font><font size=2> (dwOpcode)
{
</font><font color="#0000ff" size=2>case</font><font size=2> SERVICE_CONTROL_DEVICEEVENT:
{
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR) evdata</font><font color="#008000" size=2>/*lParam*/</font><font size=2>;
</font><font color="#0000ff" size=2>char</font><font size=2> szMessage[80];
</font><font color="#0000ff" size=2>char</font><font size=2> cDriveLetter;
</font><font color="#0000ff" size=2>if</font><font size=2>(pHdr)
{
wsprintf(szMessage, </font><font color="#a31515" size=2>"Device type is %d.n"</font><font size=2>, pHdr->dbch_devicetype);
MessageBox(NULL, szMessage, </font><font color="#a31515" size=2>""</font><font size=2>, 0);
}
</font><font color="#0000ff" size=2>switch</font><font size=2> (evtype)
{
</font><font color="#0000ff" size=2>case</font><font size=2> DBT_DEVICEARRIVAL:
</font><font color="#008000" size=2>// Handle device arrival </font><font size=2>
</font><font color="#0000ff" size=2>if</font><font size=2>(pHdr)
{
</font><font color="#008000" size=2>/*wsprintf(szMessage, "Device type is %d.n", pHdr->dbch_devicetype);
MessageBox(NULL, szMessage, "", 0);*/ </font><font size=2>
</font><font color="#0000ff" size=2>if</font><font size=2> (pHdr->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME pVol = (PDEV_BROADCAST_VOLUME) pHdr;
cDriveLetter = GetDriveLetter(pVol->dbcv_unitmask);
sprintf_s(szMessage, </font><font color="#a31515" size=2>"Device %c: has been inserted."</font><font size=2>, cDriveLetter);
MessageBox(NULL, szMessage, </font><font color="#a31515" size=2>"USB Notice"</font><font size=2>, MB_OK);
}
}
MessageBox(NULL, </font><font color="#a31515" size=2>"Device Inserted."</font><font size=2>, </font><font color="#a31515" size=2>""</font><font size=2>, 0);
</font><font color="#0000ff" size=2>break</font><font size=2>;
</font><font color="#0000ff" size=2>case</font><font size=2> DBT_DEVICEREMOVECOMPLETE:
</font><font color="#008000" size=2>// Handle device removal </font><font size=2>
MessageBox(NULL, </font><font color="#a31515" size=2>"Device Removed."</font><font size=2>, </font><font color="#a31515" size=2>""</font><font size=2>, 0);
</font><font color="#0000ff" size=2>break</font><font size=2>;
}
</font><font color="#0000ff" size=2>break</font><font size=2>;
}
return NO_ERROR;
} </font>
View the full article
I need to read the disk drive letter when a USB drive is inserted in a windows service.
The code I used ina sample MFC program raises three events for DBT_DEVICEARRIVAL, two for DBT_DEVTYP_DEVICEINTERFACE and one for DBT_DEVTYP_VOLUME.
But the I get only two events in a windows service for DBT_DEVTYP_DEVICEINTERFACE. I do not get the drive letter because DBT_DEVTYP_VOLUME event is never raised. Here is the Registration code and event handler code.
Is there any reason for this behaviour???
<font color="#0000ff" size=2>
bool</font><font size=2> CeTCaptureControlService::RegisterForUsbMessages(</font><font color="#0000ff" size=2>void</font><font size=2>)
{ </font><font size=2>
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
HDEVNOTIFY hDeviceNotify = NULL;
</font><font color="#0000ff" size=2>static</font> <font color="#0000ff" size=2>const</font><font size=2> GUID GuidDevInterfaceList[] =
{
{ 0xa5dcbf10, 0x6530, 0x11d2, { 0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed } },
{ 0x53f56307, 0xb6bf, 0x11d0, { 0x94, 0xf2, 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b } },
{ 0x4d1e55b2, 0xf16f, 0x11Cf, { 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } },
{ 0xad498944, 0x762f, 0x11d0, { 0x8d, 0xcb, 0x00, 0xc0, 0x4f, 0xc3, 0x35, 0x8c } }
};
ZeroMemory(&NotificationFilter, </font><font color="#0000ff" size=2>sizeof</font><font size=2>(NotificationFilter));
NotificationFilter.dbcc_size = </font><font color="#0000ff" size=2>sizeof</font><font size=2>(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
</font><font color="#0000ff" size=2>for</font><font size=2> (</font><font color="#0000ff" size=2>int</font><font size=2> i = 0; i < </font><font color="#0000ff" size=2>sizeof</font><font size=2>(GuidDevInterfaceList); i++)
{
NotificationFilter.dbcc_classguid = GuidDevInterfaceList;
hDeviceNotify = RegisterDeviceNotification(</font><font color="#008000" size=2>/*m_hWnd*/</font><font size=2>m_hServiceStatus, &NotificationFilter,
DEVICE_NOTIFY_SERVICE_HANDLE</font><font color="#008000" size=2>/*DEVICE_NOTIFY_WINDOW_HANDLE*/</font><font size=2>);
</font><font color="#0000ff" size=2>if</font><font size=2> (hDeviceNotify == NULL)
{
ATLTRACE2(</font><font color="#a31515" size=2>"Device Notify registration failed with error %d.n"</font><font size=2>, GetLastError());
</font><font color="#0000ff" size=2>this</font><font size=2>->LogMessage(</font><font color="#a31515" size=2>"Device Notify registration failed."</font><font size=2>, </font><font color="#0000ff" size=2>true</font><font size=2>, 0);
</font><font color="#0000ff" size=2>return</font> <font color="#0000ff" size=2>false</font><font size=2>;
}
}
</font><font color="#008000" size=2>//END Register Code </font><font size=2>
</font><font color="#0000ff" size=2>return</font> <font color="#0000ff" size=2>true</font><font size=2>;
} </font>///HANDLER code
<font size=2>
DWORD CService::Handler(DWORD dwOpcode, DWORD evtype, PVOID evdata, PVOID Context)
{
</font><font color="#0000ff" size=2>switch</font><font size=2> (dwOpcode)
{
</font><font color="#0000ff" size=2>case</font><font size=2> SERVICE_CONTROL_DEVICEEVENT:
{
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR) evdata</font><font color="#008000" size=2>/*lParam*/</font><font size=2>;
</font><font color="#0000ff" size=2>char</font><font size=2> szMessage[80];
</font><font color="#0000ff" size=2>char</font><font size=2> cDriveLetter;
</font><font color="#0000ff" size=2>if</font><font size=2>(pHdr)
{
wsprintf(szMessage, </font><font color="#a31515" size=2>"Device type is %d.n"</font><font size=2>, pHdr->dbch_devicetype);
MessageBox(NULL, szMessage, </font><font color="#a31515" size=2>""</font><font size=2>, 0);
}
</font><font color="#0000ff" size=2>switch</font><font size=2> (evtype)
{
</font><font color="#0000ff" size=2>case</font><font size=2> DBT_DEVICEARRIVAL:
</font><font color="#008000" size=2>// Handle device arrival </font><font size=2>
</font><font color="#0000ff" size=2>if</font><font size=2>(pHdr)
{
</font><font color="#008000" size=2>/*wsprintf(szMessage, "Device type is %d.n", pHdr->dbch_devicetype);
MessageBox(NULL, szMessage, "", 0);*/ </font><font size=2>
</font><font color="#0000ff" size=2>if</font><font size=2> (pHdr->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
PDEV_BROADCAST_VOLUME pVol = (PDEV_BROADCAST_VOLUME) pHdr;
cDriveLetter = GetDriveLetter(pVol->dbcv_unitmask);
sprintf_s(szMessage, </font><font color="#a31515" size=2>"Device %c: has been inserted."</font><font size=2>, cDriveLetter);
MessageBox(NULL, szMessage, </font><font color="#a31515" size=2>"USB Notice"</font><font size=2>, MB_OK);
}
}
MessageBox(NULL, </font><font color="#a31515" size=2>"Device Inserted."</font><font size=2>, </font><font color="#a31515" size=2>""</font><font size=2>, 0);
</font><font color="#0000ff" size=2>break</font><font size=2>;
</font><font color="#0000ff" size=2>case</font><font size=2> DBT_DEVICEREMOVECOMPLETE:
</font><font color="#008000" size=2>// Handle device removal </font><font size=2>
MessageBox(NULL, </font><font color="#a31515" size=2>"Device Removed."</font><font size=2>, </font><font color="#a31515" size=2>""</font><font size=2>, 0);
</font><font color="#0000ff" size=2>break</font><font size=2>;
}
</font><font color="#0000ff" size=2>break</font><font size=2>;
}
return NO_ERROR;
} </font>
View the full article