我跟随一个例子来检测USB驱动器插件和在Windows7中的插件检测。我确实收到通知WM_DEVICECHANGE
,而不是DBT_DEVICEARRIVAL
,即当USB设备被插入时。我的代码如下:
/*******************************************
* WINDOWS EVENTS
********************************************/
/*We use the first WM_PAINT event to get the handle of main window
and pass it to RegisterDeviceNotification function.
It not possible to do this in the contructor because the
main window does not exist yet.
WM_DEVICECHANGE event notify us that a device is attached or detached */
bool USBexample::nativeEvent(const QByteArray & eventType, void * message, long * result)
{
MSG * msg = static_cast< MSG * > (message);
int msgType = msg->message;
if(msgType == WM_PAINT)
{
if(!msgp) //Only the first WM_PAINT
{
GUID InterfaceClassGuid = HID_CLASSGUID;
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = InterfaceClassGuid;
HWND hw = (HWND) this->effectiveWinId(); //Main window handle
hDevNotify = RegisterDeviceNotification(hw,&NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);
msgp = true;
}
}
if(msgType == WM_DEVICECHANGE)
{
qDebug() << "WM_DEVICECHANGE recieved";
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
switch(msg->wParam)
{
case DBT_DEVICEARRIVAL: // never comes here!
if (lpdb -> dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
qDebug() << "DBT_DEVICEARRIVAL case";
PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
int i = 0;
QString s;
//to find a better way for this...
while(lpdbv->dbcc_name[i] != 0)
{
s.append(lpdbv->dbcc_name[i]);
i++;
}
s = s.toUpper();
if(s.contains(MY_DEVICE_VIDPID))
emit USB_Arrived();
}
break;
case DBT_DEVICEREMOVECOMPLETE:
if (lpdb -> dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
qDebug() << "DBT_DEVICEREMOVECOMPLETE case";
PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb;
int i = 0;
QString s;
//to find a better way for this...
while(lpdbv->dbcc_name[i] != 0)
{
s.append(lpdbv->dbcc_name[i]);
i++;
}
s = s.toUpper();
if(s.contains(MY_DEVICE_VIDPID))
emit USB_Removed();
}
break;
case DBT_DEVICEREMOVEPENDING :
{
qDebug() << "DBT_DEVICEREMOVEPENDING case";
}
break;
default:
{
qDebug() << "Went to Default case";
}
}
}
return false;
}
发布于 2015-05-28 16:13:11
我想出了这个问题,如果其他人遇到类似的问题,这里有一个解决方案。
问题是下面一行的InterfaceClassGuid
。
GUID InterfaceClassGuid = HID_CLASSGUID;
在我的代码中,HID_CLASSGUID
设置为以下内容:
#define HID_CLASSGUID {0x4d1e55b2, 0xf16f, 0x11cf,{ 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}}
这是错误的,我从例子中学到了这个,从来没有意识到我需要改变它。对于不同类型的通知,有不同的值要注册,在这种情况下,帮助系统没有多大帮助,但是下面是有效的GUID 值列表。
我将其更改为以下内容,现在我得到了所需的通知。
#define HID_CLASSGUID {0x745a17a0,0x74d3, 0x11d0, 0xb6fe, 0x00a0c90f57da}
发布于 2018-11-09 20:10:43
造成此问题的另一个原因可能是传递给RegisterDeviceNotification(.)的窗口句柄不正确。打电话。
错误代码:
QMainWindow w;
...
HANDLE windowId = w.window()->winId();
RegisterDeviceNotification(&windowId,&NotificationFilter,DEVICE_NOTIFY_WINDOW_HANDLE);
我传递了winId地址的错误值,虽然检测到了设备插入和删除,但wparam仍然得到了7。在将其更改为使用窗口句柄的正确地址后,我将其传递给RegisterDeviceNotification调用并运行。正确代码:
QMainWindow w;
...
HANDLE *windowId = (HANDLE *)w.window()->winId();
RegisterDeviceNotification(windowId,&NotificationFilter,DEVICE_NOTIFY_WINDOW_HANDLE);
https://stackoverflow.com/questions/30399992
复制相似问题