| 1 |
#include "stdafx.h" |
| 2 |
#include "Restart.h" |
| 3 |
|
| 4 |
////////////////////////////////////////////////////////////////////////// |
| 5 |
|
| 6 |
#define MAX_LOADSTRING 100 |
| 7 |
#define LOOP_COUNT 2 |
| 8 |
#define SLEEP_SPAN 1500 // ms |
| 9 |
#define APP_NAME TEXT("WinCS") |
| 10 |
|
| 11 |
HINSTANCE hInst; |
| 12 |
TCHAR szTitle[MAX_LOADSTRING]; |
| 13 |
TCHAR szWindowClass[MAX_LOADSTRING]; |
| 14 |
|
| 15 |
ATOM MyRegisterClass(HINSTANCE hInstance); |
| 16 |
BOOL InitInstance(HINSTANCE, int); |
| 17 |
BOOL ApplicationRestart(); |
| 18 |
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); |
| 19 |
|
| 20 |
////////////////////////////////////////////////////////////////////////// |
| 21 |
|
| 22 |
int APIENTRY _tWinMain(HINSTANCE hInstance, |
| 23 |
HINSTANCE hPrevInstance, |
| 24 |
LPTSTR lpCmdLine, |
| 25 |
int nCmdShow) |
| 26 |
{ |
| 27 |
UNREFERENCED_PARAMETER(hPrevInstance); |
| 28 |
UNREFERENCED_PARAMETER(lpCmdLine); |
| 29 |
|
| 30 |
MSG msg; |
| 31 |
HACCEL hAccelTable; |
| 32 |
|
| 33 |
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); |
| 34 |
LoadString(hInstance, IDC_RESTART, szWindowClass, MAX_LOADSTRING); |
| 35 |
MyRegisterClass(hInstance); |
| 36 |
|
| 37 |
if (!InitInstance (hInstance, nCmdShow)) |
| 38 |
{ |
| 39 |
return FALSE; |
| 40 |
} |
| 41 |
|
| 42 |
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_RESTART)); |
| 43 |
|
| 44 |
while (GetMessage(&msg, NULL, 0, 0)) |
| 45 |
{ |
| 46 |
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) |
| 47 |
{ |
| 48 |
TranslateMessage(&msg); |
| 49 |
DispatchMessage(&msg); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return (int) msg.wParam; |
| 54 |
} |
| 55 |
|
| 56 |
ATOM MyRegisterClass(HINSTANCE hInstance) |
| 57 |
{ |
| 58 |
WNDCLASSEX wcex; |
| 59 |
|
| 60 |
wcex.cbSize = sizeof(WNDCLASSEX); |
| 61 |
wcex.style = CS_HREDRAW | CS_VREDRAW; |
| 62 |
wcex.lpfnWndProc = WndProc; |
| 63 |
wcex.cbClsExtra = 0; |
| 64 |
wcex.cbWndExtra = 0; |
| 65 |
wcex.hInstance = hInstance; |
| 66 |
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RESTART)); |
| 67 |
wcex.hCursor = LoadCursor(NULL, IDC_ARROW); |
| 68 |
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); |
| 69 |
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_RESTART); |
| 70 |
wcex.lpszClassName = szWindowClass; |
| 71 |
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); |
| 72 |
|
| 73 |
return RegisterClassEx(&wcex); |
| 74 |
} |
| 75 |
|
| 76 |
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) |
| 77 |
{ |
| 78 |
HWND hWnd; |
| 79 |
|
| 80 |
hInst = hInstance; |
| 81 |
|
| 82 |
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, |
| 83 |
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, |
| 84 |
NULL, NULL, hInstance, NULL); |
| 85 |
|
| 86 |
if (!hWnd) |
| 87 |
{ |
| 88 |
return FALSE; |
| 89 |
} |
| 90 |
|
| 91 |
UpdateWindow(hWnd); |
| 92 |
|
| 93 |
return TRUE; |
| 94 |
} |
| 95 |
|
| 96 |
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) |
| 97 |
{ |
| 98 |
switch (message) |
| 99 |
{ |
| 100 |
case WM_CREATE: |
| 101 |
ApplicationRestart(); |
| 102 |
PostQuitMessage(0); |
| 103 |
break; |
| 104 |
|
| 105 |
default: |
| 106 |
return DefWindowProc(hWnd, message, wParam, lParam); |
| 107 |
|
| 108 |
} |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
BOOL ApplicationRestart() |
| 113 |
{ |
| 114 |
PROCESS_INFORMATION pi; |
| 115 |
STARTUPINFO si; |
| 116 |
BOOL fRet; |
| 117 |
int nCount = 0; |
| 118 |
TCHAR szCmdLine[] = APP_NAME; |
| 119 |
|
| 120 |
ZeroMemory(&si, sizeof(si)); |
| 121 |
si.cb = sizeof(si); |
| 122 |
|
| 123 |
do |
| 124 |
{ |
| 125 |
nCount++; |
| 126 |
Sleep(SLEEP_SPAN); |
| 127 |
|
| 128 |
fRet = CreateProcess(NULL, |
| 129 |
szCmdLine, |
| 130 |
NULL, |
| 131 |
NULL, |
| 132 |
FALSE, |
| 133 |
NORMAL_PRIORITY_CLASS, |
| 134 |
NULL, |
| 135 |
NULL, |
| 136 |
&si, |
| 137 |
&pi); |
| 138 |
|
| 139 |
if (fRet) |
| 140 |
{ |
| 141 |
CloseHandle(pi.hThread); |
| 142 |
CloseHandle(pi.hProcess); |
| 143 |
break; |
| 144 |
} |
| 145 |
} |
| 146 |
while (nCount <= LOOP_COUNT); |
| 147 |
|
| 148 |
return fRet; |
| 149 |
} |
| 150 |
|
| 151 |
////////////////////////////////////////////////////////////////////////// |