I have been trying recently to create a window class using the Windows API in C++. However, whenever I attempt to call ShowWindow, the function sets the last error to 1400 (ERROR_INVALID_WINDOW_HANDLE). After trying for a while, I stumbled across the following example: http://blogs.msdn.com/b/oldnewthing/archive/2005/04/22/410773.aspx#comments
Even creating a new project (I use MSVC Express 2008) and copying the code exactly (which I hate to do), I discovered that, while the code successfully created a window, the ShowWindow function still reported error 1400. Here is an excerpt from the code found at the above link:
int PASCAL
WinMain(HINSTANCE hinst, HINSTANCE, LPSTR, int nShowCmd)
{
g_hinst = hinst;
if (SUCCEEDED(CoInitialize(NULL))) {
InitCommonControls();
RootWindow *prw = RootWindow::Create();
if (prw) {
ShowWindow(prw->GetHWND(), nShowCmd);
int error = GetLastError(); //Line added by me, error gets set to 1400.
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
CoUninitialize();
}
return 0;
}
(The full code can be found at the above link)
If anyone has any ideas on how to have the window handle as a member variable of a class without receiving error 1400 on ShowWindow, I would greatly appreciate some help.
ShowWindow(prw->GetHWND(), nShowCmd);
int error = GetLastError();
This is not correct code. The only time it is valid to call GetLastError() is when a winapi function failed. If you use GetLastError() when they didn't fail then you'll get a completely random number. ShowWindow() is a bit special in that it doesn't produce an error code at all so using GetLastError() is never correct.
The generic pattern is roughly:
if (!SomeWinapiFunction(...)) {
int error = GetLastError();
CrashAndBurn(error);
}
But do check the MSDN documentation to see what return value indicates an error and whether GetLastError() is appropriate. It is generally not on GDI functions for example. Be sure to correct this in other parts of your code as well. Getting error handling right is very important when you use the raw api. In particular note how your RootWindow::Create() method has no good way to indicate failure to create the window. That needs to be fixed. Exceptions are of course a very good way to do so.