![](/img/trans.png)
[英]Why does the unexecuted code in my Qt program cause the program to crash?
[英]Why does ChangeWindowMessageFilter cause Qt to crash?
為什么以下程序崩潰?
#include <QApplication>
#include <windows.h>
#include <QFrame>
uint MSGFLT_ADD = 1;
uint WM_COPYGLOBALDATA = 0x0049;
int main(int argc, char *argv[])
{
BOOL (*ChangeWindowMessageFilter)(UINT,DWORD) = NULL;
HINSTANCE hDLL = LoadLibraryA("User32.dll"); // Handle to DLL
if (hDLL != NULL){
ChangeWindowMessageFilter = (BOOL (*)(UINT,DWORD))GetProcAddress(hDLL, "ChangeWindowMessageFilter");
}
if (ChangeWindowMessageFilter != NULL){
if (!(*ChangeWindowMessageFilter)(WM_DROPFILES, MSGFLT_ADD)){
printf("Failed to add exception for WM_DROPFILES\n");
}
if (!(*ChangeWindowMessageFilter)(WM_COPYDATA, MSGFLT_ADD)){
printf("Failed to add exception for WM_COPYDATA");
}
if (!(*ChangeWindowMessageFilter)(WM_COPYGLOBALDATA, MSGFLT_ADD)){
printf("Failed to add exception for WM_COPYGLOBALDATA");
}
printf("Added filters\n");
fflush(0);
}
if (hDLL != NULL){
FreeLibrary(hDLL);
}
QApplication a(argc, argv);
QFrame w; //debug crashes here
w.show();
return a.exec();
}
QFrame::QFrame(QWidget* parent, Qt::WindowFlags f)
: QWidget(*new QFramePrivate, parent, f) //on this line in particular
{
Q_D(QFrame);
d->init();
}
編輯:
if (!(*ChangeWindowMessageFilter)(WM_COPYDATA, MSGFLT_ADD)){ //if i disable this everything works
printf("Failed to add exception for WM_COPYDATA");
}
BOOL (*ChangeWindowMessageFilter)(UINT,DWORD) = NULL;
您的函數指針聲明是錯誤的。 Winapi函數始終為__stdcall。 您的編譯器無疑會使用默認的__cdecl。 通過函數指針進行調用時,堆棧不平衡會產生很多副作用。 如果您使用MSVC ++並運行Debug版本,則將始終獲得立即診斷。 固定:
BOOL (WINAPI * ChangeWindowMessageFilter)(UINT,DWORD) = NULL;
首先,如果這是要使拖放到提升的程序中,則只需刪除所有這些內容,它將不起作用。 D + D是基於COM的,它不使用消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.