gpt4 book ai didi

c++ - 在我的 Qt 应用程序中收到 WM_DEVICECHANGE 但没有收到 DBT_DEVICEARRIVAL

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:44 28 4
gpt4 key购买 nike

我正在按照一个示例来检测 Windows 7 中的 USB 闪存驱动器插件和拔出检测。我确实收到了通知 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;
}

最佳答案

我解决了这个问题,如果其他人遇到类似问题,这里是解决方案。

问题出在以下行中的 InterfaceClassGuid

GUID InterfaceClassGuid = HID_CLASSGUID;

HID_CLASSGUID 在我的代码中设置为以下内容:

#define HID_CLASSGUID {0x4d1e55b2, 0xf16f, 0x11cf,{ 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}}

这是错误的,我从示例中学到了这一点,但从未意识到我需要更改它。有不同的值可以注册不同类型的通知,帮助系统在这种情况下没有太大帮助,但这里是有效 GUID 的列表 values .

我将其更改为以下内容,现在我收到了所需的通知。

#define HID_CLASSGUID  {0x745a17a0,0x74d3, 0x11d0, 0xb6fe, 0x00a0c90f57da}

关于c++ - 在我的 Qt 应用程序中收到 WM_DEVICECHANGE 但没有收到 DBT_DEVICEARRIVAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30399992/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com