I’m trying to learn win32 API by following some tutorial.
(Though, I did very minor tweaking to create a borderless fixed window.)
However, my simplest window application is exiting with some random code.
I have no idea why it is not exiting with code ‘0’.
For extra information, I’m using Visual Studio 2012 Pro.
Source code’s file extension is .c and the compiler setting is probably default.
I created the project as an empty win32 application (not console).
Please, some help will be appreciated.
Thank you.
#include <Windows.h>
#include <windowsx.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int __stdcall WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
INT nCmdShow) {
HWND hWnd;
WNDCLASSEX wcex;
MSG msg;
ZeroMemory(&wcex, sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"WindowClass1";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wcex))
{
MessageBox(
NULL,
L"Failed to register window!",
L"ERROR",
MB_OK | MB_ICONEXCLAMATION);
return EXIT_FAILURE;
}
hWnd = CreateWindowEx(
0,
L"WindowClass1",
L"Application",
WS_POPUP,
0, 0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL, NULL, hInstance, NULL);
if (hWnd == NULL)
{
MessageBox(
NULL,
L"Failed to create window!",
L"ERROR",
MB_OK | MB_ICONEXCLAMATION);
return EXIT_FAILURE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, hWnd, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
In your program,
GetMessageis in fact returning-1which is an error condition. Your message loop terminates whenGetMessagereturns a value <=0, and so it terminates whenGetMessagereturns -1.Now, because the final call to
GetMessagefails with an error, the value ofmsg.wParamis not well-defined. You should not return it as an exit code. You should only returnmsg.wParamas an exit code when the final call toGetMessagereturned 0. This is all made clear in the documentation.You can see all this if you change your message loop to look like this:
On my machine, the
bRet == -1route is selected and the error code is 1400. Which isERROR_INVALID_WINDOW_HANDLE. I’m not quite sure why your app is behaving in this way, but I’m content that I’ve answered the question you asked about exit codes!