我有一个从Registering for Device Notification获取的代码示例,用于检测USB设备是否已被删除。此代码使用Win32 API,我已经成功构建并测试它。
当我尝试将此功能集成到QObject
派生类中时,我在MessagePump()
方法中遇到了崩溃:
void QObjectDerivedClass::MessagePump() {
MSG message;
int retVal;
if (!m_hWnd) {
return;
}
qDebug() << Q_FUNC_INFO;
// Get all messages for any window that belongs to this thread,
// without any filtering. Potential optimization could be
// obtained via use of filter values if desired.
while ((retVal = GetMessage(&message, m_hWnd,
MSG_FILTER_MIN, MSG_FILTER_MAX)) != 0) {
if (retVal == -1) {
break;
} else {
TranslateMessage(&message);
DispatchMessage(&message);
}
}
}
您可以想象,我必须修改示例中的WndProc()
回调,以便为此类使用静态成员并满足WNDCLASS
对象,如下所示:
BOOL QObjectDerivedClass::InitWindowClass() {
WNDCLASSEX wndClass;/* = {0};*/
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = 0;
wndClass.hInstance = reinterpret_cast<HINSTANCE>(GetModuleHandle(0));
// WndProcThunk is an static member of QObjectDerivedClass
wndClass.lpfnWndProc = reinterpret_cast<WNDPROC>(WndProcThunk);
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hIcon = NULL;
wndClass.hIconSm = NULL;
wndClass.hbrBackground = NULL;
wndClass.hCursor = NULL;
wndClass.lpszClassName = WND_CLASS_NAME;
wndClass.lpszMenuName = NULL;
if (!RegisterClassEx(&wndClass)) {
qDebug() << Q_FUNC_INFO << "Unable to register window class. Error:"
<< QString::number(GetLastError());
return FALSE;
}
m_instance = wndClass.hInstance;
return TRUE;
}
应用程序崩溃时的调用堆栈是:
1 0xd26128 2 SetManipulationInputTarget USER32 0x7709d2b3 3 DispatchMessageW USER32 0x7707e88a 4 DispatchMessageW USER32 0x7707e4c0 5 RealGetWindowClassW USER32 0x7708a64f 6 KiUserCallbackDispatcher ntdll 0x772e08c6 7 QObjectDerivedClass::MessagePump qobjectderived.cpp 165 0xa52e88 8 QObjectDerivedClass::Start qobjectderived.cpp 346 0xa52bc2 9 wWinMain main.cpp 259 0xa525ff 10 invoke_main exe_common.inl 118 0xa5516e 11 __scrt_common_main_seh exe_common.inl 253 0xa54fd0 12 __scrt_common_main exe_common.inl 296 0xa54e6d 13 wWinMainCRTStartup exe_wwinmain.cpp 17 0xa55188 14 BaseThreadInitThunk KERNEL32 0x73d862c4 15 RtlSubscribeWnfStateChangeNotification ntdll 0x772d0fd9 16 RtlSubscribeWnfStateChangeNotification ntdll 0x772d0fa4
知道如何解决这次崩溃吗?
编辑:发布标准输出:
int __stdcall wWinMain(struct HINSTANCE__ *,struct HINSTANCE__ *,wchar_t *,int) int __stdcall QObjectDerivedClass::WndProcThunk(struct HWND__ *,unsigned int,unsigned int,long) int __stdcall QObjectDerivedClass::WndProcThunk(struct HWND__ *,unsigned int,unsigned int,long) int __stdcall QObjectDerivedClass::WndProcThunk(struct HWND__ *,unsigned int,unsigned int,long) int __stdcall QObjectDerivedClass::WndProcThunk(struct HWND__ *,unsigned int,unsigned int,long) void __thiscall QObjectDerivedClass::MessagePump(void)
答案 0 :(得分:2)
你不需要重新实现任何本机消息:Qt已经为你做了。
相反,在具体QAbstractNativeEventFilter中重新实现nativeEventFilter
时对Windows消息作出反应。重新实现也可以继承QObject
- 确保QObject
是第一个基类,因为这是继承QObject
唯一受支持的方式。
要使用过滤器,请使用qApp->installNativeEventFilter
安装。
答案 1 :(得分:-1)
WinApi本质上不支持C ++。 QT将抽象出将正确的消息传递到正确窗口所涉及的所有艰苦工作。您似乎试图绕过所有这些事情,并自己创建窗口和消息,这会导致您的问题。
为什么不跟随simple QT example并将更改插入其中,而不是重新发明轮子。