我在编码检测USB设备的时候遇到一个问题,一直解决不了。待检测的USB设备是自行设计的,可以正常工作。
在程序中,我用OnUSBChange(WPARAM wParam, LPARAM lParam)函数响应WM_DEVICECHANGE消息。为了简单起见,我将代码简化如下:
LRESULT CMainFrame::OnUSBChange(WPARAM wParam, LPARAM lParam)
{
DEV_BROADCAST_HDR *pDEV = (DEV_BROADCAST_HDR*)lParam;
if (wParam == DBT_DEVICEARRIVAL)
{
AfxMessageBox("OK");
}
if (wParam == DBT_DEVICEREMOVECOMPLETE)
{
AfxMessageBox("NO");
}
return CWnd::OnDeviceChange(wParam, lParam);
}
由于担心没有注册消息,我又添加了函数RegisterForDevChange(HWND hDlg, PVOID *hNotifyDevNode),代码如下
void CMainFrame::RegisterForDevChange(HWND hDlg, PVOID *hNotifyDevNode)
{
DEV_BROADCAST_DEVICEINTERFACE *pFilterData =
(DEV_BROADCAST_DEVICEINTERFACE*)
_alloca(sizeof(DEV_BROADCAST_DEVICEINTERFACE));
ASSERT (pFilterData);
UUID GUID_CLASS_INPUT;
UuidFromString((unsigned char*)"36FC9E60-C465-11CF-8056-444553540000", &GUID_CLASS_INPUT);
ZeroMemory(pFilterData, sizeof(DEV_BROADCAST_DEVICEINTERFACE));
pFilterData->dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
pFilterData->dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
pFilterData->dbcc_classguid = GUID_CLASS_INPUT;
*hNotifyDevNode = RegisterDeviceNotification(hDlg, pFilterData, DEVICE_NOTIFY_WINDOW_HANDLE);
} |