ERROR_INVALID_WINDOW_HANDLE from CreateWindowEx ()

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

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

I also tried adding W

to random functions and datatypes, 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 );
}

      

+3


source to share


2 answers


I think the problem is with your call to DefWindowProc.

Try changing the line:

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

      



For this:

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

      

I just compiled your code with this change and it works fine.

+8


source


I would say that your MainWndProc

IS has been called and the problem is that you are passing mainhwnd

as an argument to DefWindowProc while it hasn't been initialized yet. You should be doing instead:



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

      

+1


source







All Articles