【发布时间】:2017-04-05 12:07:33
【问题描述】:
我有一个取自 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 设置操作输入目标 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::开始 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 内核32 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)【问题讨论】:
-
请发布标准
-
@Idgorman 不知道该怎么做,但发布了。
-
这有一种独特的感觉,
QObjectDerivedClass实例被它自己触发的其他事件破坏,并在从MessagePump返回时崩溃,第一次碰巧尝试访问任何它的领域。这是在将代码模式逻辑与非 UI 模式对话框混合时一直出现的一个经典问题,我希望这会是这个主题的一些变化。