Question

Why does the following program 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();
}

EDIT:

if (!(*ChangeWindowMessageFilter)(WM_COPYDATA, MSGFLT_ADD)){ //if i disable this everything works
    printf("Failed to add exception for WM_COPYDATA");
}
Était-ce utile?

La solution

   BOOL (*ChangeWindowMessageFilter)(UINT,DWORD) = NULL;

Your function pointer declaration is wrong. Winapi functions are always __stdcall. Your compiler no doubt uses the default, __cdecl. The stack imbalance you get when you make the call through the function pointer can have many side effects. If you use MSVC++ and run the Debug build then you'll always get an immediate diagnostic. Fix:

   BOOL (WINAPI * ChangeWindowMessageFilter)(UINT,DWORD) = NULL;

Fwiw, if this is meant to enable drag+drop into an elevated program then just remove all this, it won't work. D+D is COM based, it doesn't use messages.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top