Thread: CreateWindowEx - GetLastError() = 6

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    CreateWindowEx - GetLastError() = 6

    Ok if I've done this once I've done it a million bloody times. The only difference here is I'm calling CreateWindowEx() passing parameters which are parameters of a function in a DLL:

    Code:
    HWND GFXBase::NewWindow(HINSTANCE hInst, WNDPROC wndProc, DWORD windowStyle,
                            char *className, char *windowTitle, int x, int y, int width, int height)
    {
        this->mWindowInstance = hInst;
        DWORD lastError = NULL;
        char buf[120] = {0};
    
        mWC.cbClsExtra = 0;
        mWC.cbSize = sizeof(mWC);
        mWC.cbWndExtra = 0;
        mWC.hbrBackground = reinterpret_cast<HBRUSH> (COLOR_WINDOW);
        mWC.hCursor = LoadCursor(NULL, IDC_ARROW);
        mWC.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        mWC.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        mWC.hInstance = hInst;
        mWC.lpfnWndProc = wndProc;
        mWC.lpszClassName = className;
        mWC.lpszMenuName = NULL;
        mWC.style = CS_HREDRAW | CS_VREDRAW;
    
        if (! RegisterClassEx(&mWC))
        {
            lastError = GetLastError();
            sprintf(buf, "Unable to register window class [%u]", lastError);
            MessageBox(NULL, buf, "GFXBase::NewWindow", 
                       MB_OK | MB_ICONERROR | MB_TASKMODAL);
            return NULL;
        }
    
        this->mWindowHandle = CreateWindowEx(NULL, className, windowTitle, windowStyle, 
                                           x, y, width, height, NULL, NULL, NULL, NULL);
        if (! this->mWindowHandle)
        {
            lastError = GetLastError();
            sprintf(buf, "Unable to create window [%u]", lastError);
            MessageBox(NULL, buf, "GFXBase::NewWindow",
                       MB_OK | MB_ICONERROR | MB_TASKMODAL);
            return NULL;
        }
    
        return this->mWindowHandle;
    }
    The above is in the DLL. This calls it:

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        GFXBase *gfxBase = GFXBase::CreateInstance();
        HWND hWnd = NULL;
        MSG msg;
    
        hWnd = gfxBase->NewWindow(hInstance, WndProc, WS_OVERLAPPEDWINDOW, "GFXBaseClient", "GFXBase Sandbox",
                                  0, 0, 320, 240);
    
        if (! hWnd)
        {
            gfxBase->DestroyWindow();
            GFXBase::FreeInstance();
            return 1;
        }
    
        while (GetMessage(&msg, hWnd, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        gfxBase->DestroyWindow();
        GFXBase::FreeInstance();
        return 0;
    }
    Now I've debugged it to death and the parameters are coming through to NewWindow fine and when I step down to the CreateWindowEx call they remain unchanged. Still, it fails with GetLastError() returning 6 ("The Handle is invalid"). Which handle? It takes 3 and none of them apply here so they should be NULL. Right?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Well the first param to CreateWindowEx, dwExStyle, shouldn't be NULL (it's asking for a value dude, not a pointer), try 0.

    Then there's the little matter of hInstance, the second-to-last param. That can't be NULL otherwise it can't attribute the window to any process. Try GetModuleHandle(NULL).
    Last edited by SMurf; 04-19-2006 at 02:29 PM.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Well my compiler #define's NULL as 0 and according to MSDN, under XP that hInstance parameter is ignored.

    I'll try them though.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Nope same problem. Blasted 6.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Try:
    Code:
        mWC.hbrBackground = reinterpret_cast<HBRUSH> (COLOR_WINDOW + 1);
        mWC.hCursor = LoadCursor(NULL, IDC_ARROW);
        mWC.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        mWC.hIconSm = NULL

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Nope still the same error code. I don't know what in the ........ I'm doing wrong. I just knocked up a small windows program w/o using a DLL and had the same problem.

    I bet it's something glaringly obvious too.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Okay then, what does wndProc return after handling WM_CREATE?

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    It returns 0.

    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    
        case WM_CREATE:
            break;
        }
    
        return 0;
    }
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    It's not a dialog box, so you need to have:
    Code:
    default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    As a case in your WindowProc, otherwise WM_NCCREATE isn't handled properly and CreateWindowEx assumes the worst.

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I can't believe I forgot about DefWindowProc(). Cheers SMurf-o!
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateWindowEx() failing with 64-bit
    By domokato in forum Windows Programming
    Replies: 7
    Last Post: 07-02-2009, 07:30 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Having Trouble with CreateWindowEx
    By Eber Kain in forum Windows Programming
    Replies: 10
    Last Post: 06-05-2004, 07:45 AM
  4. Beginner Question: CreateWindowEx Fails.
    By dicky in forum Windows Programming
    Replies: 4
    Last Post: 05-30-2004, 08:56 AM
  5. Creating a toolbar using CreateWindowEX()
    By bc17 in forum Windows Programming
    Replies: 3
    Last Post: 08-02-2003, 11:21 AM