简体   繁体   中英

RegisterClassEx invalid parameter on 64bit (but working on 32 bit)

I've build a DLL that is compiled for 32 and 64 bit. The following snippet works for 32 bit, but doesn't for 64 bit (Error Code 87 - Invalid parameter)

const CHAR g_szClassName[] = "MyFancyDll";
...
WNDCLASSEX wndClass;
    wndClass.cbSize = sizeof(WNDCLASSEX);
    wndClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc = (WNDPROC) WinProcCallback;
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;
    wndClass.hIcon = NULL;
    wndClass.hbrBackground = NULL;
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = g_szClassName;
    wndClass.hIconSm = NULL;
    if (!RegisterClassEx(&wndClass)) {
        DWORD error = GetLastError();
        printf("Error registering class: %d", error);
    }

The code is compiled using MinGW.

So why isn't this working for 64 bit?

The command line for 32 bit is (Extracted this from my makefile macros, so sorry for any typos):

...
gcc -m32 -g3 -c -static-libgcc -static-libstdc++ file.c -o file32.o
gcc -m64 -g3 -c -static-libgcc -static-libstdc++ file.c -o file64.o

g++ -m32 -shared --enable-stdcall-fixup -static-libstdc++ file32.o -o mydll32.dll
g++ -m64 -shared --enable-stdcall-fixup -static-libstdc++ file64.o -o mydll64.dll

Your code is leaving uninitialized memory and that might cause undefined behavior , make sure that every pointer/data is zero'ed out before proceeding setting your flags/pointers:

WNDCLASSEX wndClass;
memset(&wndClass, 0, sizeof(WNDCLASSEX));

or even better:

WNDCLASSEX wndClass = { 0 };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM