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 );
}
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.
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With