The code:
ATOM WindowAtom = NULL;
HWND Window = NULL;
int main()
{
WNDCLASSEX wndclassex;
// open primary window
ZeroMemory(&wndclassex, sizeof(WNDCLASSEX));
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_CLASSDC;
wndclassex.lpfnWndProc = (WNDPROC)WindowProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = (HINSTANCE)GetModuleHandle(NULL);
wndclassex.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MAINICON));
wndclassex.hCursor = LoadCursor((HINSTANCE)GetModuleHandle(NULL), MAKEINTRESOURCE(IDC_POINTER));
wndclassex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = CLASS_NAME;
wndclassex.hIconSm = LoadIcon((HINSTANCE)GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MAINICON));
WindowAtom = RegisterClassEx(&wndclassex);
if (!WindowAtom)
{
printf("ERROR: !RegisterClassEx(...)\n");
printf("ERROR: GetLastError() == %lu\n", GetLastError());
goto exit;
}
Window = CreateWindow(CLASS_NAME, TITLE,
WS_CAPTION | WS_SYSMENU,
0, 0, 640, 480,
NULL, NULL,
(HINSTANCE)GetModuleHandle(NULL), NULL);
if (!Window)
{
printf("ERROR: !CreateWindow(...)\n");
printf("ERROR: GetLastError() == %lu\n", GetLastError());
goto exit;
}
ShowWindow(Window, SW_NORMAL);
UpdateWindow(Window);
// message pump here
// exit
return 0;
exit:
// wait for key press
printf("\nHit any key to continue...");
getch();
return 0;
}
// window handler
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
// not handled
return DefWindowProc(hWnd, uMsg, lParam, wParam);
}
The output:
ERROR: !CreateWindow(...)
ERROR: GetLastError() == 0
I've done some testing and have found that RegisterClassEx doesn't fail, but GetLastError() after the call does return 2 (which has something to do with file not found). To see if GetLastError() was returning a previous value (not that it should) I had done a SetLastError(0), but GetLastError() still returned 2. I've tried comparing the code with other successful attempts, but I can't figure out what I'm doing wrong here.
Thanks.
[Edited by - bcome on July 29, 2004 4:24:03 PM]