Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERROR_INVALID_WINDOW_HANDLE from CreateWindowEx()

I am getting the error ERROR_INVALID_WINDOW_HANDLE when CreateWindowEx() fails in my program. I am using C++ with the native Win32 API.

I have no idea why, and I have tried toying with some parameters in CreateWindowEx but it still produces the exact same error every time.

I have also tried adding W to random functions and data types hoping that the UNICODE version somehow works...nothing changed, of course.

This is the function that calls CreateWindowEx():

int InitMainWindow( HWND *hwnd, WNDCLASSEXW *wnd, WNDPROC wndproc )
{
    memset( wnd, NULL, sizeof( WNDCLASSEXW ) );
    wnd->cbSize = sizeof( WNDCLASSEXW );
    wnd->lpszClassName = L"MainWClass";
    wnd->lpfnWndProc = wndproc;
    wnd->hInstance = GetModuleHandle( NULL );

    if( NULL == RegisterClassExW(wnd) )
    {
        printf( "InitMainWindow::RegisterClassexW() error: %d\r\n", GetLastError() );
        return GetLastError();
    }

    *hwnd = CreateWindowExW
        (
        WS_EX_ACCEPTFILES | WS_EX_APPWINDOW, //extended styles
        wnd->lpszClassName, //class name
        L"MainWindow", //window name
        WS_OVERLAPPEDWINDOW | WS_VISIBLE, //style tags
        CW_USEDEFAULT, //horizontal position
        CW_USEDEFAULT, //vertical position
        900, //width
        600, //height
        GetDesktopWindow(), //parent window
        NULL, //class menu
        GetModuleHandle(NULL), //some HINSTANCE pointer
        NULL //Create Window Data?
        );

    if( NULL == *hwnd )
    {
        printf( "InitMainWindow::CreateWindowEx() error: %d\r\n", GetLastError() );
        return GetLastError();
    }

    return 0;
}

This is the main method:

static HWND mainhwnd;
void main()
{
    DWORD time;
    time = GetTickCount();

    MSG msg;
    WNDCLASSEXW wnd = { 0 };
    NOTIFYICONDATA nid;

    InitMainWindow( &mainhwnd, &wnd, MainWndProc );
    InitNotifyIcon( &mainhwnd, &nid );
    ShowWindow( mainhwnd, true );
    UpdateWindow( mainhwnd );

    time = ( GetTickCount() - time );
    std::cout << "Time: " << time << "\r\n" << std::endl;

    for( ; ; ) //message loop
    {
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            if( WM_CLOSE == msg.message ) //reassign close button to minimize to tray
            {
                printf("close\r\n");
                break;
            }

            TranslateMessage(&msg);
            DispatchMessage(&msg);

            /*if( !IsDialogMessage( hwndListDialog, &msg ) )
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }*/
        }
        Sleep( 5 );
    }
}

This is my Windows Procedure:

LRESULT CALLBACK MainWndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    switch(uMsg)
    {
    case WM_CREATE:
        printf("Main Window Create.......SUCCESS");
        break;
    default:
        break;
    }

    return DefWindowProc( mainhwnd, uMsg, wParam, lParam );
}
like image 850
Joshua Avatar asked Dec 12 '22 04:12

Joshua


2 Answers

I think the problem is with your call to DefWindowProc.

Try changing the line:

    return DefWindowProc( mainhwnd, uMsg, wParam, lParam );

To this:

    return DefWindowProc( hwnd, uMsg, wParam, lParam );

I've just compiled your code here with that change and it works OK.

like image 108
Bob Moss Avatar answered Jan 01 '23 21:01

Bob Moss


I'd say your MainWndProc IS called, and the problem is that you pass mainhwnd as a DefWindowProc argument, while it is still not initialized. You should be doing instead:

return DefWindowProc( hwnd, uMsg, wParam, lParam );
like image 25
Roman R. Avatar answered Jan 01 '23 20:01

Roman R.