![](/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.