1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
/* ====================================================================
18
* ApacheMonitor.c Simple program to manage and monitor Apache services.
20
* Contributed by Mladen Turk <mturk mappingsoft.com>
23
* ====================================================================
26
#define _WIN32_WINNT 0x0400
34
#if defined(_MSC_VER) && _MSC_VER >= 1400
35
#define _CRT_SECURE_NO_DEPRECATE
36
#pragma warning(disable: 4996)
46
#include "ApacheMonitor.h"
49
#define OS_VERSION_WIN9X 1
50
#define OS_VERSION_WINNT 2
51
#define OS_VERSION_WIN2K 3
52
/* Should be enough */
53
#define MAX_APACHE_SERVICES 128
54
#define MAX_APACHE_COMPUTERS 32
56
#define WM_TRAYMESSAGE (WM_APP+1)
57
#define WM_UPDATEMESSAGE (WM_USER+1)
58
#define WM_MANAGEMESSAGE (WM_USER+2)
59
#define WM_TIMER_REFRESH 10
60
#define WM_TIMER_RESCAN 11
61
#define SERVICE_APACHE_RESTART 128
64
#define MAX_LOADSTRING 100
65
#define REFRESH_TIME 2000 /* service refresh time (ms) */
66
#define RESCAN_TIME 20000 /* registry rescan time (ms) */
68
typedef struct _st_APACHE_SERVICE
78
typedef struct _st_MONITORED_COMPUTERS
84
/* Global variables */
85
HINSTANCE g_hInstance = NULL;
86
CHAR *g_szTitle; /* The title bar text */
87
CHAR *g_szWindowClass; /* Window Class Name */
90
UINT g_bUiTaskbarCreated;
92
BOOL g_bDlgServiceOn = FALSE;
93
BOOL g_bConsoleRun = FALSE;
94
ST_APACHE_SERVICE g_stServices[MAX_APACHE_SERVICES];
95
ST_MONITORED_COMP g_stComputers[MAX_APACHE_COMPUTERS];
97
HBITMAP g_hBmpStart, g_hBmpStop;
98
HBITMAP g_hBmpPicture, g_hBmpOld;
99
BOOL g_bRescanServices;
100
HWND g_hwndServiceDlg;
102
HWND g_hwndStdoutList;
103
HWND g_hwndConnectDlg;
104
HCURSOR g_hCursorHourglass;
105
HCURSOR g_hCursorArrow;
107
HANDLE g_hpipeOutRead;
108
HANDLE g_hpipeOutWrite;
109
HANDLE g_hpipeInRead;
110
HANDLE g_hpipeInWrite;
111
HANDLE g_hpipeStdError;
113
PROCESS_INFORMATION g_lpRedirectProc;
114
CRITICAL_SECTION g_stcSection;
117
/* locale language support */
118
static CHAR *g_lpMsg[IDS_MSG_LAST - IDS_MSG_FIRST + 1];
121
void am_ClearServicesSt()
124
for (i = 0; i < MAX_APACHE_SERVICES; i++)
126
if (g_stServices[i].szServiceName) {
127
free(g_stServices[i].szServiceName);
129
if (g_stServices[i].szDisplayName) {
130
free(g_stServices[i].szDisplayName);
132
if (g_stServices[i].szDescription) {
133
free(g_stServices[i].szDescription);
135
if (g_stServices[i].szImagePath) {
136
free(g_stServices[i].szImagePath);
138
if (g_stServices[i].szComputerName) {
139
free(g_stServices[i].szComputerName);
143
memset(g_stServices, 0, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);
148
void am_ClearComputersSt()
151
for (i = 0; i < MAX_APACHE_COMPUTERS; i++) {
152
if (g_stComputers[i].szComputerName) {
153
free(g_stComputers[i].szComputerName);
154
RegCloseKey(g_stComputers[i].hRegistry);
157
memset(g_stComputers, 0, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);
162
BOOL am_IsComputerConnected(LPSTR szComputerName)
165
while (g_stComputers[i].szComputerName != NULL) {
166
if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
175
void am_DisconnectComputer(LPSTR szComputerName)
178
while (g_stComputers[i].szComputerName != NULL) {
179
if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
184
if (g_stComputers[i].szComputerName != NULL) {
185
free(g_stComputers[i].szComputerName);
186
RegCloseKey(g_stComputers[i].hRegistry);
187
for (j = i; j < MAX_APACHE_COMPUTERS - 1; j++) {
188
g_stComputers[i].szComputerName= g_stComputers[i+1].szComputerName;
189
g_stComputers[i].hRegistry = g_stComputers[i+1].hRegistry;
191
for (i = j; i < MAX_APACHE_COMPUTERS; i++) {
192
g_stComputers[i].szComputerName = NULL;
193
g_stComputers[i].hRegistry = NULL;
200
void ErrorMessage(LPCSTR szError, BOOL bFatal)
202
LPVOID lpMsgBuf = NULL;
204
MessageBox(NULL, szError, g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST],
205
MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION));
208
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
209
FORMAT_MESSAGE_FROM_SYSTEM |
210
FORMAT_MESSAGE_IGNORE_INSERTS,
211
NULL, GetLastError(), g_LangID,
212
(LPSTR) &lpMsgBuf, 0, NULL);
213
MessageBox(NULL, (LPCSTR)lpMsgBuf,
214
g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST],
215
MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION));
224
BOOL am_ConnectComputer(LPSTR szComputerName)
228
char szTmp[MAX_PATH];
230
while (g_stComputers[i].szComputerName != NULL) {
231
if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
236
if (i > MAX_APACHE_COMPUTERS - 1) {
239
if (RegConnectRegistry(szComputerName, HKEY_LOCAL_MACHINE, &hKeyRemote)
241
sprintf(szTmp, g_lpMsg[IDS_MSG_ECONNECT - IDS_MSG_FIRST],
243
ErrorMessage(szTmp, FALSE);
247
g_stComputers[i].szComputerName = strdup(szComputerName);
248
g_stComputers[i].hRegistry = hKeyRemote;
254
LPSTR GetStringRes(int id)
256
static CHAR buffer[MAX_PATH];
259
LoadString(GetModuleHandle(NULL), id, buffer, MAX_PATH);
264
BOOL GetSystemOSVersion(LPDWORD dwVersion)
268
Try calling GetVersionEx using the OSVERSIONINFOEX structure.
269
If that fails, try using the OSVERSIONINFO structure.
271
memset(&osvi, 0, sizeof(OSVERSIONINFO));
272
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
274
if (!GetVersionEx(&osvi)) {
278
switch (osvi.dwPlatformId)
280
case VER_PLATFORM_WIN32_NT:
281
if (osvi.dwMajorVersion <= 4) {
282
*dwVersion = OS_VERSION_WINNT;
284
else if (osvi.dwMajorVersion == 5) {
285
*dwVersion = OS_VERSION_WIN2K;
292
case VER_PLATFORM_WIN32_WINDOWS:
293
*dwVersion = OS_VERSION_WIN9X;
296
case VER_PLATFORM_WIN32s:
305
static VOID ShowNotifyIcon(HWND hWnd, DWORD dwMessage)
310
memset(&nid, 0, sizeof(nid));
311
nid.cbSize = sizeof(NOTIFYICONDATA);
314
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
315
nid.uCallbackMessage = WM_TRAYMESSAGE;
317
while (g_stServices[i].szServiceName != NULL)
319
if (g_stServices[i].dwPid != 0) {
324
if (dwMessage != NIM_DELETE)
327
nid.hIcon = g_icoRun;
330
nid.hIcon = g_icoStop;
336
if (n == i && n > 0) {
337
lstrcpy(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGALL - IDS_MSG_FIRST]);
340
sprintf(nid.szTip, g_lpMsg[IDS_MSG_RUNNING - IDS_MSG_FIRST], n, i);
343
sprintf(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGNONE - IDS_MSG_FIRST], i);
346
lstrcpy(nid.szTip, g_lpMsg[IDS_MSG_NOSERVICES - IDS_MSG_FIRST]);
348
Shell_NotifyIcon(dwMessage, &nid);
352
void appendMenuItem(HMENU hMenu, UINT uMenuId, LPSTR szName,
353
BOOL fDefault, BOOL fEnabled)
357
memset(&mii, 0, sizeof(MENUITEMINFO));
358
mii.cbSize = sizeof(MENUITEMINFO);
359
mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
362
mii.fType = MFT_STRING;
365
mii.fState = MFS_DEFAULT;
368
mii.fState |= MFS_DISABLED;
370
mii.dwTypeData = szName;
373
mii.fType = MFT_SEPARATOR;
375
InsertMenuItem(hMenu, uMenuId, FALSE, &mii);
379
void appendServiceMenu(HMENU hMenu, UINT uMenuId,
380
LPSTR szServiceName, BOOL fRunning)
385
smh = CreatePopupMenu();
387
appendMenuItem(smh, IDM_SM_START + uMenuId,
388
g_lpMsg[IDS_MSG_SSTART - IDS_MSG_FIRST], FALSE, !fRunning);
389
appendMenuItem(smh, IDM_SM_STOP + uMenuId,
390
g_lpMsg[IDS_MSG_SSTOP - IDS_MSG_FIRST], FALSE, fRunning);
391
appendMenuItem(smh, IDM_SM_RESTART + uMenuId,
392
g_lpMsg[IDS_MSG_SRESTART - IDS_MSG_FIRST], FALSE, fRunning);
394
memset(&mii, 0, sizeof(MENUITEMINFO));
395
mii.cbSize = sizeof(MENUITEMINFO);
396
mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_SUBMENU
398
mii.fType = MFT_STRING;
400
mii.hbmpChecked = g_hBmpStart;
401
mii.hbmpUnchecked = g_hBmpStop;
402
mii.dwTypeData = szServiceName;
404
mii.fState = fRunning ? MFS_CHECKED : MFS_UNCHECKED;
405
InsertMenuItem(hMenu, IDM_SM_SERVICE + uMenuId, FALSE, &mii);
409
void ShowTryPopupMenu(HWND hWnd)
411
/* create popup menu */
412
HMENU hMenu = CreatePopupMenu();
417
appendMenuItem(hMenu, IDM_RESTORE,
418
g_lpMsg[IDS_MSG_MNUSHOW - IDS_MSG_FIRST],
420
if (g_dwOSVersion >= OS_VERSION_WINNT) {
421
appendMenuItem(hMenu, IDC_SMANAGER,
422
g_lpMsg[IDS_MSG_MNUSERVICES - IDS_MSG_FIRST],
425
appendMenuItem(hMenu, 0, "", FALSE, TRUE);
426
appendMenuItem(hMenu, IDM_EXIT,
427
g_lpMsg[IDS_MSG_MNUEXIT - IDS_MSG_FIRST],
430
if (!SetForegroundWindow(hWnd)) {
431
SetForegroundWindow(NULL);
434
TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
435
pt.x, pt.y, 0, hWnd, NULL);
441
void ShowTryServicesMenu(HWND hWnd)
443
/* create services list popup menu and submenus */
444
HMENU hMenu = CreatePopupMenu();
450
while (g_stServices[i].szServiceName != NULL)
452
appendServiceMenu(hMenu, i, g_stServices[i].szDisplayName,
453
g_stServices[i].dwPid != 0);
458
if (!SetForegroundWindow(hWnd)) {
459
SetForegroundWindow(NULL);
462
TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
463
pt.x, pt.y, 0, hWnd, NULL);
470
BOOL CenterWindow(HWND hwndChild)
472
RECT rChild, rWorkArea;
477
/* Get the Height and Width of the child window */
478
GetWindowRect(hwndChild, &rChild);
479
wChild = rChild.right - rChild.left;
480
hChild = rChild.bottom - rChild.top;
482
/* Get the limits of the 'workarea' */
483
bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT),
486
rWorkArea.left = rWorkArea.top = 0;
487
rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
488
rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
491
/* Calculate new X and Y position*/
492
xNew = (rWorkArea.right - wChild) / 2;
493
yNew = (rWorkArea.bottom - hChild) / 2;
494
return SetWindowPos(hwndChild, HWND_TOP, xNew, yNew, 0, 0,
495
SWP_NOSIZE | SWP_SHOWWINDOW);
499
static void addListBoxItem(HWND hDlg, LPSTR lpStr, HBITMAP hBmp)
503
nItem = SendMessage(hDlg, LB_ADDSTRING, 0, (LPARAM)lpStr);
504
SendMessage(hDlg, LB_SETITEMDATA, nItem, (LPARAM)hBmp);
508
static void addListBoxString(HWND hListBox, LPSTR lpStr)
510
static int nItems = 0;
511
if (!g_bDlgServiceOn) {
515
if (nItems > MAX_LOADSTRING)
517
SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
520
ListBox_SetCurSel(hListBox,
521
ListBox_AddString(hListBox, lpStr));
526
static DWORD WINAPI ConsoleOutputThread(LPVOID lpThreadParameter)
528
static BYTE lpBuffer[MAX_PATH+1];
533
while (ReadFile(g_hpipeOutRead, &ch, 1, &dwReaded, NULL) == TRUE)
537
if (ch == '\n' || nPtr >= MAX_PATH)
539
lpBuffer[nPtr] = '\0';
540
addListBoxString(g_hwndStdoutList, lpBuffer);
543
else if (ch == '\t' && nPtr < (MAX_PATH - 4))
546
for (i = 0; i < 4; ++i) {
547
lpBuffer[nPtr++] = ' ';
550
else if (ch != '\r') {
551
lpBuffer[nPtr++] = ch;
555
CloseHandle(g_hpipeInWrite);
556
CloseHandle(g_hpipeOutRead);
557
CloseHandle(g_hpipeStdError);
562
DWORD WINAPI ConsoleWaitingThread(LPVOID lpThreadParameter)
564
WaitForSingleObject(g_lpRedirectProc.hThread, INFINITE);
565
CloseHandle(g_lpRedirectProc.hThread);
567
g_bConsoleRun = FALSE;
568
SetCursor(g_hCursorArrow);
573
BOOL RunRedirectedConsole(LPSTR szCmdLine)
580
memset(&stInfo, 0, sizeof(stInfo));
581
stInfo.cb = sizeof(stInfo);
582
stInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
583
stInfo.wShowWindow = SW_HIDE;
585
hProc = GetCurrentProcess();
587
if (!CreatePipe(&g_hpipeInRead, &g_hpipeInWrite, NULL, MAX_PATH)) {
588
ErrorMessage(NULL, TRUE);
590
if (!CreatePipe(&g_hpipeOutRead, &g_hpipeOutWrite, NULL, MAX_PATH*8)) {
591
ErrorMessage(NULL, TRUE);
593
DuplicateHandle(hProc, g_hpipeInRead, hProc, &g_hpipeInRead, 0, TRUE,
594
DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
595
DuplicateHandle(hProc, g_hpipeOutWrite, hProc, &g_hpipeOutWrite, 0, TRUE,
596
DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
597
DuplicateHandle(hProc, g_hpipeOutWrite, hProc, &g_hpipeStdError, 0, TRUE,
598
DUPLICATE_SAME_ACCESS);
599
if (!g_hpipeInRead && !g_hpipeOutWrite && !g_hpipeStdError) {
600
ErrorMessage(NULL, TRUE);
602
stInfo.hStdInput = g_hpipeInRead;
603
stInfo.hStdOutput = g_hpipeOutWrite;
604
stInfo.hStdError = g_hpipeStdError;
606
bResult = CreateProcess(NULL,
618
CloseHandle(g_hpipeInRead);
619
CloseHandle(g_hpipeOutWrite);
620
CloseHandle(g_hpipeStdError);
624
CloseHandle(g_hpipeInWrite);
625
CloseHandle(g_hpipeOutRead);
626
CloseHandle(g_hpipeStdError);
630
CloseHandle(CreateThread(NULL, 0, ConsoleOutputThread,
632
ResumeThread(g_lpRedirectProc.hThread);
633
CloseHandle(CreateThread(NULL, 0, ConsoleWaitingThread,
640
BOOL RunAndForgetConsole(LPSTR szCmdLine, BOOL bRedirectConsole)
643
PROCESS_INFORMATION prInfo;
646
if (bRedirectConsole) {
647
return RunRedirectedConsole(szCmdLine);
650
memset(&stInfo, 0, sizeof(stInfo));
651
stInfo.cb = sizeof(stInfo);
652
stInfo.dwFlags = STARTF_USESHOWWINDOW;
653
stInfo.wShowWindow = SW_HIDE;
655
bResult = CreateProcess(NULL,
669
if (g_dwOSVersion == OS_VERSION_WIN9X) {
670
/* give some time to rescan the status */
673
CloseHandle(prInfo.hThread);
674
CloseHandle(prInfo.hProcess);
679
BOOL ApacheManageService(LPCSTR szServiceName, LPCSTR szImagePath,
680
LPSTR szComputerName, DWORD dwCommand)
682
CHAR szBuf[MAX_PATH];
683
CHAR szMsg[MAX_PATH];
686
BOOL serviceFlag = TRUE;
687
SC_HANDLE schService;
688
SC_HANDLE schSCManager;
689
SERVICE_STATUS schSStatus;
692
if (g_dwOSVersion == OS_VERSION_WIN9X)
694
sPos = strstr(szImagePath, "-k start");
697
lstrcpyn(szBuf, szImagePath, (int)(sPos - szImagePath));
700
case SERVICE_CONTROL_STOP:
701
lstrcat(szBuf, " -k shutdown -n ");
704
case SERVICE_CONTROL_CONTINUE:
705
sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTART - IDS_MSG_FIRST],
707
addListBoxString(g_hwndStdoutList, szMsg);
708
lstrcat(szBuf, " -k start -n ");
712
case SERVICE_APACHE_RESTART:
713
lstrcat(szBuf, " -k restart -n ");
719
lstrcat(szBuf, szServiceName);
724
g_bConsoleRun = TRUE;
725
SetCursor(g_hCursorHourglass);
726
if (!RunAndForgetConsole(szBuf, serviceFlag))
728
ErrorMessage(NULL, FALSE);
729
g_bConsoleRun = FALSE;
730
SetCursor(g_hCursorArrow);
733
else if (!serviceFlag)
735
sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTARTED - IDS_MSG_FIRST],
737
addListBoxString(g_hwndStdoutList, szMsg);
738
g_bConsoleRun = FALSE;
739
SetCursor(g_hCursorArrow);
745
schSCManager = OpenSCManager(szComputerName, NULL,
751
schService = OpenService(schSCManager, szServiceName,
752
SERVICE_QUERY_STATUS | SERVICE_START |
753
SERVICE_STOP | SERVICE_USER_DEFINED_CONTROL);
754
if (schService != NULL)
757
g_bConsoleRun = TRUE;
758
SetCursor(g_hCursorHourglass);
761
case SERVICE_CONTROL_STOP:
762
sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTOP - IDS_MSG_FIRST],
764
addListBoxString(g_hwndStdoutList, szMsg);
765
if (ControlService(schService, SERVICE_CONTROL_STOP,
768
while (QueryServiceStatus(schService, &schSStatus))
770
if (schSStatus.dwCurrentState == SERVICE_STOP_PENDING)
779
if (QueryServiceStatus(schService, &schSStatus))
781
if (schSStatus.dwCurrentState == SERVICE_STOPPED)
785
g_lpMsg[IDS_MSG_SRVSTOPPED - IDS_MSG_FIRST],
787
addListBoxString(g_hwndStdoutList, szMsg);
792
case SERVICE_CONTROL_CONTINUE:
793
sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTART - IDS_MSG_FIRST],
795
addListBoxString(g_hwndStdoutList, szMsg);
797
if (StartService(schService, 0, NULL))
800
while (QueryServiceStatus(schService, &schSStatus))
802
if (schSStatus.dwCurrentState == SERVICE_START_PENDING)
811
if (QueryServiceStatus(schService, &schSStatus))
813
if (schSStatus.dwCurrentState == SERVICE_RUNNING)
817
g_lpMsg[IDS_MSG_SRVSTARTED - IDS_MSG_FIRST],
819
addListBoxString(g_hwndStdoutList, szMsg);
824
case SERVICE_APACHE_RESTART:
825
sprintf(szMsg, g_lpMsg[IDS_MSG_SRVRESTART - IDS_MSG_FIRST],
827
addListBoxString(g_hwndStdoutList, szMsg);
828
if (ControlService(schService, SERVICE_APACHE_RESTART,
832
while (schSStatus.dwCurrentState == SERVICE_START_PENDING)
835
if (!QueryServiceStatus(schService, &schSStatus))
837
CloseServiceHandle(schService);
838
CloseServiceHandle(schSCManager);
839
g_bConsoleRun = FALSE;
840
SetCursor(g_hCursorArrow);
848
if (schSStatus.dwCurrentState == SERVICE_RUNNING)
852
g_lpMsg[IDS_MSG_SRVRESTARTED - IDS_MSG_FIRST],
854
addListBoxString(g_hwndStdoutList, szMsg);
858
CloseServiceHandle(schService);
859
CloseServiceHandle(schSCManager);
861
ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED - IDS_MSG_FIRST],
864
g_bConsoleRun = FALSE;
865
SetCursor(g_hCursorArrow);
869
g_bRescanServices = TRUE;
871
CloseServiceHandle(schSCManager);
879
BOOL IsServiceRunning(LPCSTR szServiceName, LPCSTR szComputerName,
884
SC_HANDLE schService;
885
SC_HANDLE schSCManager;
886
SERVICE_STATUS schSStatus;
888
if (g_dwOSVersion == OS_VERSION_WIN9X)
890
hWnd = FindWindow("ApacheWin95ServiceMonitor", szServiceName);
891
if (hWnd && GetWindowThreadProcessId(hWnd, &dwPid))
903
schSCManager = OpenSCManager(szComputerName, NULL,
909
schService = OpenService(schSCManager, szServiceName,
910
SERVICE_QUERY_STATUS);
911
if (schService != NULL)
913
if (QueryServiceStatus(schService, &schSStatus))
915
dwPid = schSStatus.dwCurrentState;
920
CloseServiceHandle(schService);
921
CloseServiceHandle(schSCManager);
922
return dwPid == SERVICE_RUNNING ? TRUE : FALSE;
925
g_bRescanServices = TRUE;
927
CloseServiceHandle(schSCManager);
936
BOOL FindRunningServices(void)
941
while (g_stServices[i].szServiceName != NULL)
943
if (!IsServiceRunning(g_stServices[i].szServiceName,
944
g_stServices[i].szComputerName, &dwPid)) {
947
if (g_stServices[i].dwPid != dwPid) {
950
g_stServices[i].dwPid = dwPid;
957
BOOL GetApacheServicesStatus()
959
CHAR szKey[MAX_PATH];
960
CHAR achKey[MAX_PATH];
961
CHAR szImagePath[MAX_PATH];
962
CHAR szBuf[MAX_PATH];
963
CHAR szTmp[MAX_PATH];
964
HKEY hKey, hSubKey, hKeyRemote;
965
DWORD retCode, rv, dwKeyType;
966
DWORD dwBufLen = MAX_PATH;
970
g_bRescanServices = FALSE;
972
am_ClearServicesSt();
973
while (g_stComputers[computers].szComputerName != NULL) {
974
hKeyRemote = g_stComputers[computers].hRegistry;
975
retCode = RegOpenKeyEx(hKeyRemote,
976
"System\\CurrentControlSet\\Services\\",
978
if (retCode != ERROR_SUCCESS)
980
ErrorMessage(NULL, FALSE);
983
for (i = 0, retCode = ERROR_SUCCESS; retCode == ERROR_SUCCESS; i++)
985
retCode = RegEnumKey(hKey, i, achKey, MAX_PATH);
986
if (retCode == ERROR_SUCCESS)
988
lstrcpy(szKey, "System\\CurrentControlSet\\Services\\");
989
lstrcat(szKey, achKey);
991
if (RegOpenKeyEx(hKeyRemote, szKey, 0,
992
KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS)
995
rv = RegQueryValueEx(hSubKey, "ImagePath", NULL,
996
&dwKeyType, szImagePath, &dwBufLen);
998
if (rv == ERROR_SUCCESS
999
&& (dwKeyType == REG_SZ
1000
|| dwKeyType == REG_EXPAND_SZ)
1003
lstrcpy(szBuf, szImagePath);
1005
/* the service name could be httpd*.exe or Apache*.exe */
1006
if (((strstr(szBuf, "\\apache") != NULL)
1007
|| (strstr(szBuf, "\\httpd") != NULL))
1008
&& strstr(szBuf, ".exe")
1009
&& (strstr(szBuf, "--ntservice") != NULL
1010
|| strstr(szBuf, "-k ") != NULL))
1012
g_stServices[stPos].szServiceName = strdup(achKey);
1013
g_stServices[stPos].szImagePath =
1014
strdup(szImagePath);
1015
g_stServices[stPos].szComputerName =
1016
strdup(g_stComputers[computers].szComputerName);
1017
dwBufLen = MAX_PATH;
1018
if (RegQueryValueEx(hSubKey, "Description", NULL,
1019
&dwKeyType, szBuf, &dwBufLen)
1021
g_stServices[stPos].szDescription =
1024
dwBufLen = MAX_PATH;
1025
if (RegQueryValueEx(hSubKey, "DisplayName", NULL,
1026
&dwKeyType, szBuf, &dwBufLen)
1029
if (strcmp(g_stComputers[computers]
1030
.szComputerName, g_szLocalHost) != 0)
1032
strcpy(szTmp, g_stComputers[computers]
1033
.szComputerName + 2);
1035
strcat(szTmp, szBuf);
1038
strcpy(szTmp, szBuf);
1040
g_stServices[stPos].szDisplayName
1045
if (stPos >= MAX_APACHE_SERVICES) {
1046
retCode = !ERROR_SUCCESS;
1050
RegCloseKey(hSubKey);
1057
FindRunningServices();
1062
LRESULT CALLBACK ConnectDlgProc(HWND hDlg, UINT message,
1063
WPARAM wParam, LPARAM lParam)
1065
CHAR szCmp[MAX_COMPUTERNAME_LENGTH+4];
1069
ShowWindow(hDlg, SW_HIDE);
1070
g_hwndConnectDlg = hDlg;
1072
ShowWindow(hDlg, SW_SHOW);
1073
SetFocus(GetDlgItem(hDlg, IDC_COMPUTER));
1077
switch (LOWORD(wParam))
1080
memset(szCmp, 0, MAX_COMPUTERNAME_LENGTH+4);
1081
strcpy(szCmp, "\\\\");
1082
SendMessage(GetDlgItem(hDlg, IDC_COMPUTER), WM_GETTEXT,
1083
(WPARAM) MAX_COMPUTERNAME_LENGTH,
1087
if (strlen(szCmp) < 3) {
1088
EndDialog(hDlg, TRUE);
1091
am_ConnectComputer(szCmp);
1092
SendMessage(g_hwndMain, WM_TIMER, WM_TIMER_RESCAN, 0);
1095
EndDialog(hDlg, TRUE);
1103
memset(&bi, 0, sizeof(BROWSEINFO));
1104
SHGetSpecialFolderLocation(hDlg, CSIDL_NETWORK, &il);
1106
bi.lpszTitle = "ApacheMonitor :\nSelect Network Computer!";
1107
bi.pszDisplayName = szCmp;
1108
bi.hwndOwner = hDlg;
1109
bi.ulFlags = BIF_BROWSEFORCOMPUTER;
1115
if (SHBrowseForFolder(&bi) != NULL) {
1116
SendMessage(GetDlgItem(hDlg, IDC_COMPUTER),
1118
(WPARAM) NULL, (LPARAM) szCmp);
1120
if (SHGetMalloc(&pMalloc)) {
1121
pMalloc->lpVtbl->Free(pMalloc, il);
1122
pMalloc->lpVtbl->Release(pMalloc);
1131
EndDialog(hDlg, TRUE);
1142
LRESULT CALLBACK ServiceDlgProc(HWND hDlg, UINT message,
1143
WPARAM wParam, LPARAM lParam)
1145
CHAR szBuf[MAX_PATH];
1147
static HWND hStatusBar;
1153
LPMEASUREITEMSTRUCT lpmis;
1154
LPDRAWITEMSTRUCT lpdis;
1156
memset(szBuf, 0, MAX_PATH);
1160
ShowWindow(hDlg, SW_HIDE);
1161
g_hwndServiceDlg = hDlg;
1162
SetWindowText(hDlg, g_szTitle);
1163
Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1164
Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1165
Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1166
Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1167
SetWindowText(GetDlgItem(hDlg, IDC_SSTART),
1168
g_lpMsg[IDS_MSG_SSTART - IDS_MSG_FIRST]);
1169
SetWindowText(GetDlgItem(hDlg, IDC_SSTOP),
1170
g_lpMsg[IDS_MSG_SSTOP - IDS_MSG_FIRST]);
1171
SetWindowText(GetDlgItem(hDlg, IDC_SRESTART),
1172
g_lpMsg[IDS_MSG_SRESTART - IDS_MSG_FIRST]);
1173
SetWindowText(GetDlgItem(hDlg, IDC_SMANAGER),
1174
g_lpMsg[IDS_MSG_SERVICES - IDS_MSG_FIRST]);
1175
SetWindowText(GetDlgItem(hDlg, IDC_SCONNECT),
1176
g_lpMsg[IDS_MSG_CONNECT - IDS_MSG_FIRST]);
1177
SetWindowText(GetDlgItem(hDlg, IDC_SEXIT),
1178
g_lpMsg[IDS_MSG_MNUEXIT - IDS_MSG_FIRST]);
1179
if (g_dwOSVersion < OS_VERSION_WINNT)
1181
ShowWindow(GetDlgItem(hDlg, IDC_SMANAGER), SW_HIDE);
1182
ShowWindow(GetDlgItem(hDlg, IDC_SCONNECT), SW_HIDE);
1183
ShowWindow(GetDlgItem(hDlg, IDC_SDISCONN), SW_HIDE);
1185
hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1186
g_hwndStdoutList = GetDlgItem(hDlg, IDL_STDOUT);
1187
hStatusBar = CreateStatusWindow(0x0800 /* SBT_TOOLTIPS */
1188
| WS_CHILD | WS_VISIBLE,
1189
"", hDlg, IDC_STATBAR);
1190
if (GetApacheServicesStatus())
1193
while (g_stServices[i].szServiceName != NULL)
1195
addListBoxItem(hListBox, g_stServices[i].szDisplayName,
1196
g_stServices[i].dwPid == 0 ? g_hBmpStop
1202
ShowWindow(hDlg, SW_SHOW);
1204
SendMessage(hListBox, LB_SETCURSEL, 0, 0);
1208
case WM_MANAGEMESSAGE:
1209
ApacheManageService(g_stServices[LOWORD(wParam)].szServiceName,
1210
g_stServices[LOWORD(wParam)].szImagePath,
1211
g_stServices[LOWORD(wParam)].szComputerName,
1217
case WM_UPDATEMESSAGE:
1218
hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1219
SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
1220
SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)"");
1221
Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1222
Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1223
Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1224
Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1226
while (g_stServices[i].szServiceName != NULL)
1228
addListBoxItem(hListBox, g_stServices[i].szDisplayName,
1229
g_stServices[i].dwPid == 0 ? g_hBmpStop : g_hBmpStart);
1232
SendMessage(hListBox, LB_SETCURSEL, 0, 0);
1233
/* Dirty hack to bring the window to the foreground */
1234
SetWindowPos(hDlg, HWND_TOPMOST, 0, 0, 0, 0,
1235
SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1236
SetWindowPos(hDlg, HWND_NOTOPMOST, 0, 0, 0, 0,
1237
SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1242
case WM_MEASUREITEM:
1243
lpmis = (LPMEASUREITEMSTRUCT) lParam;
1244
lpmis->itemHeight = YBITMAP;
1248
if (g_bConsoleRun) {
1249
SetCursor(g_hCursorHourglass);
1252
SetCursor(g_hCursorArrow);
1257
lpdis = (LPDRAWITEMSTRUCT) lParam;
1258
if (lpdis->itemID == -1) {
1261
switch (lpdis->itemAction)
1264
case ODA_DRAWENTIRE:
1265
g_hBmpPicture = (HBITMAP)SendMessage(lpdis->hwndItem,
1267
lpdis->itemID, (LPARAM) 0);
1269
hdcMem = CreateCompatibleDC(lpdis->hDC);
1270
g_hBmpOld = SelectObject(hdcMem, g_hBmpPicture);
1272
BitBlt(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
1273
lpdis->rcItem.right - lpdis->rcItem.left,
1274
lpdis->rcItem.bottom - lpdis->rcItem.top,
1275
hdcMem, 0, 0, SRCCOPY);
1276
SendMessage(lpdis->hwndItem, LB_GETTEXT,
1277
lpdis->itemID, (LPARAM) szBuf);
1279
GetTextMetrics(lpdis->hDC, &tm);
1280
y = (lpdis->rcItem.bottom + lpdis->rcItem.top - tm.tmHeight) / 2;
1282
SelectObject(hdcMem, g_hBmpOld);
1285
rcBitmap.left = lpdis->rcItem.left + XBITMAP + 2;
1286
rcBitmap.top = lpdis->rcItem.top;
1287
rcBitmap.right = lpdis->rcItem.right;
1288
rcBitmap.bottom = lpdis->rcItem.top + YBITMAP;
1290
if (lpdis->itemState & ODS_SELECTED)
1292
if (g_hBmpPicture == g_hBmpStop)
1294
Button_Enable(GetDlgItem(hDlg, IDC_SSTART), TRUE);
1295
Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1296
Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1298
else if (g_hBmpPicture == g_hBmpStart)
1300
Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1301
Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), TRUE);
1302
Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), TRUE);
1305
Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1306
Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1307
Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1309
if (strcmp(g_stServices[lpdis->itemID].szComputerName,
1310
g_szLocalHost) == 0) {
1311
Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1314
Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), TRUE);
1317
if (g_stServices[lpdis->itemID].szDescription) {
1318
SendMessage(hStatusBar, SB_SETTEXT, 0,
1319
(LPARAM)g_stServices[lpdis->itemID].szDescription);
1322
SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)"");
1324
SetTextColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
1325
SetBkColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHT));
1326
FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_HIGHLIGHTTEXT));
1330
SetTextColor(lpdis->hDC, GetSysColor(COLOR_MENUTEXT));
1331
SetBkColor(lpdis->hDC, GetSysColor(COLOR_WINDOW));
1332
FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_WINDOW+1));
1334
TextOut(lpdis->hDC, XBITMAP + 6, y, szBuf, (int)strlen(szBuf));
1342
switch (LOWORD(wParam))
1345
switch (HIWORD(wParam))
1348
/* if started then stop, if stopped then start */
1349
hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1350
nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1351
if (nItem != LB_ERR)
1353
g_hBmpPicture = (HBITMAP)SendMessage(hListBox,
1356
if (g_hBmpPicture == g_hBmpStop) {
1357
SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1358
SERVICE_CONTROL_CONTINUE);
1361
SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1362
SERVICE_CONTROL_STOP);
1371
EndDialog(hDlg, TRUE);
1375
Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1376
hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1377
nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1378
if (nItem != LB_ERR) {
1379
SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1380
SERVICE_CONTROL_CONTINUE);
1382
Button_Enable(GetDlgItem(hDlg, IDC_SSTART), TRUE);
1386
Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1387
hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1388
nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1389
if (nItem != LB_ERR) {
1390
SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1391
SERVICE_CONTROL_STOP);
1393
Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), TRUE);
1397
Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1398
hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1399
nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1400
if (nItem != LB_ERR) {
1401
SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1402
SERVICE_APACHE_RESTART);
1404
Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), TRUE);
1408
if (g_dwOSVersion >= OS_VERSION_WIN2K) {
1409
ShellExecute(hDlg, "open", "services.msc", "/s",
1413
WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL);
1418
EndDialog(hDlg, TRUE);
1419
SendMessage(g_hwndMain, WM_COMMAND, (WPARAM)IDM_EXIT, 0);
1423
DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGCONNECT),
1424
hDlg, (DLGPROC)ConnectDlgProc);
1428
hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1429
nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1430
if (nItem != LB_ERR) {
1431
am_DisconnectComputer(g_stServices[nItem].szComputerName);
1432
SendMessage(g_hwndMain, WM_TIMER, WM_TIMER_RESCAN, 0);
1439
switch (LOWORD(wParam))
1441
case SIZE_MINIMIZED:
1442
EndDialog(hDlg, TRUE);
1450
EndDialog(hDlg, TRUE);
1460
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
1461
WPARAM wParam, LPARAM lParam)
1463
if (message == g_bUiTaskbarCreated)
1465
/* restore the tray icon on shell restart */
1466
ShowNotifyIcon(hWnd, NIM_ADD);
1467
return DefWindowProc(hWnd, message, wParam, lParam);
1472
GetApacheServicesStatus();
1473
ShowNotifyIcon(hWnd, NIM_ADD);
1474
SetTimer(hWnd, WM_TIMER_REFRESH, REFRESH_TIME, NULL);
1475
SetTimer(hWnd, WM_TIMER_RESCAN, RESCAN_TIME, NULL);
1481
case WM_TIMER_RESCAN:
1483
int nPrev = 0, nNew = 0;
1484
EnterCriticalSection(&g_stcSection);
1485
if (FindRunningServices() || g_bRescanServices)
1487
ShowNotifyIcon(hWnd, NIM_MODIFY);
1488
if (g_hwndServiceDlg)
1489
PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1491
/* check if services list changed */
1492
while (g_stServices[nPrev].szServiceName != NULL)
1494
GetApacheServicesStatus();
1495
while (g_stServices[nNew].szServiceName != NULL)
1499
ShowNotifyIcon(hWnd, NIM_MODIFY);
1500
if (g_hwndServiceDlg) {
1501
PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1504
LeaveCriticalSection(&g_stcSection);
1508
case WM_TIMER_REFRESH:
1510
int nPrev = 0, nNew = 0;
1511
EnterCriticalSection(&g_stcSection);
1512
if (g_bRescanServices)
1514
GetApacheServicesStatus();
1515
ShowNotifyIcon(hWnd, NIM_MODIFY);
1516
if (g_hwndServiceDlg) {
1517
PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1520
else if (FindRunningServices())
1522
ShowNotifyIcon(hWnd, NIM_MODIFY);
1523
if (g_hwndServiceDlg) {
1524
PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1527
LeaveCriticalSection(&g_stcSection);
1534
ShowNotifyIcon(hWnd, NIM_DELETE);
1537
case WM_TRAYMESSAGE:
1540
case WM_LBUTTONDBLCLK:
1541
if (!g_bDlgServiceOn)
1543
g_bDlgServiceOn = TRUE;
1544
DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES),
1545
hWnd, (DLGPROC)ServiceDlgProc);
1546
g_bDlgServiceOn = FALSE;
1547
g_hwndServiceDlg = NULL;
1549
else if (IsWindow(g_hwndServiceDlg))
1551
/* Dirty hack to bring the window to the foreground */
1552
SetWindowPos(g_hwndServiceDlg, HWND_TOPMOST, 0, 0, 0, 0,
1553
SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1554
SetWindowPos(g_hwndServiceDlg, HWND_NOTOPMOST, 0, 0, 0, 0,
1555
SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1556
SetFocus(g_hwndServiceDlg);
1561
ShowTryServicesMenu(hWnd);
1565
ShowTryPopupMenu(hWnd);
1571
if ((LOWORD(wParam) & IDM_SM_START) == IDM_SM_START)
1573
ApacheManageService(g_stServices[LOWORD(wParam)
1574
- IDM_SM_START].szServiceName,
1575
g_stServices[LOWORD(wParam)
1576
- IDM_SM_START].szImagePath,
1577
g_stServices[LOWORD(wParam)
1578
- IDM_SM_START].szComputerName,
1579
SERVICE_CONTROL_CONTINUE);
1582
else if ((LOWORD(wParam) & IDM_SM_STOP) == IDM_SM_STOP)
1584
ApacheManageService(g_stServices[LOWORD(wParam)
1585
- IDM_SM_STOP].szServiceName,
1586
g_stServices[LOWORD(wParam)
1587
- IDM_SM_STOP].szImagePath,
1588
g_stServices[LOWORD(wParam)
1589
- IDM_SM_STOP].szComputerName,
1590
SERVICE_CONTROL_STOP);
1593
else if ((LOWORD(wParam) & IDM_SM_RESTART) == IDM_SM_RESTART)
1595
ApacheManageService(g_stServices[LOWORD(wParam)
1596
- IDM_SM_RESTART].szServiceName,
1597
g_stServices[LOWORD(wParam)
1598
- IDM_SM_RESTART].szImagePath,
1599
g_stServices[LOWORD(wParam)
1600
- IDM_SM_RESTART].szComputerName,
1601
SERVICE_APACHE_RESTART);
1604
switch (LOWORD(wParam))
1607
if (!g_bDlgServiceOn)
1609
g_bDlgServiceOn = TRUE;
1610
DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES),
1611
hWnd, (DLGPROC)ServiceDlgProc);
1612
g_bDlgServiceOn = FALSE;
1613
g_hwndServiceDlg = NULL;
1615
else if (IsWindow(g_hwndServiceDlg)) {
1616
SetFocus(g_hwndServiceDlg);
1621
if (g_dwOSVersion >= OS_VERSION_WIN2K) {
1622
ShellExecute(NULL, "open", "services.msc", "/s",
1626
WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL);
1631
ShowNotifyIcon(hWnd, NIM_DELETE);
1637
return DefWindowProc(hWnd, message, wParam, lParam);
1644
/* Create main invisible window */
1645
HWND CreateMainWindow(HINSTANCE hInstance)
1650
if (!GetSystemOSVersion(&g_dwOSVersion))
1652
ErrorMessage(NULL, TRUE);
1656
wcex.cbSize = sizeof(WNDCLASSEX);
1658
wcex.style = CS_HREDRAW | CS_VREDRAW;
1659
wcex.lpfnWndProc = (WNDPROC)WndProc;
1660
wcex.cbClsExtra = 0;
1661
wcex.cbWndExtra = 0;
1662
wcex.hInstance = hInstance;
1663
wcex.hIcon = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON),
1664
IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
1665
wcex.hCursor = g_hCursorArrow;
1666
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
1667
wcex.lpszMenuName = 0;
1668
wcex.lpszClassName = g_szWindowClass;
1669
wcex.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON),
1670
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1672
if (RegisterClassEx(&wcex)) {
1673
hWnd = CreateWindow(g_szWindowClass, g_szTitle,
1675
NULL, NULL, hInstance, NULL);
1682
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1683
LPSTR lpCmdLine, int nCmdShow)
1685
CHAR szTmp[MAX_LOADSTRING];
1686
CHAR szCmp[MAX_COMPUTERNAME_LENGTH+4];
1688
/* single instance mutex */
1693
g_LangID = GetUserDefaultLangID();
1694
if ((g_LangID & 0xFF) != LANG_ENGLISH) {
1695
g_LangID = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
1697
for (i = IDS_MSG_FIRST; i <= IDS_MSG_LAST; ++i) {
1698
LoadString(hInstance, i, szTmp, MAX_LOADSTRING);
1699
g_lpMsg[i - IDS_MSG_FIRST] = strdup(szTmp);
1701
LoadString(hInstance, IDS_APMONITORTITLE, szTmp, MAX_LOADSTRING);
1702
d = MAX_COMPUTERNAME_LENGTH+1;
1703
strcpy(szCmp, "\\\\");
1704
GetComputerName(szCmp + 2, &d);
1706
g_szLocalHost = strdup(szCmp);
1708
memset(g_stComputers, 0, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);
1709
g_stComputers[0].szComputerName = strdup(szCmp);
1710
g_stComputers[0].hRegistry = HKEY_LOCAL_MACHINE;
1711
g_szTitle = strdup(szTmp);
1712
LoadString(hInstance, IDS_APMONITORCLASS, szTmp, MAX_LOADSTRING);
1713
g_szWindowClass = strdup(szTmp);
1715
g_icoStop = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICOSTOP),
1716
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1717
g_icoRun = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICORUN),
1718
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1719
g_hCursorHourglass = LoadImage(NULL, MAKEINTRESOURCE(OCR_WAIT),
1720
IMAGE_CURSOR, LR_DEFAULTSIZE,
1721
LR_DEFAULTSIZE, LR_SHARED);
1722
g_hCursorArrow = LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL),
1723
IMAGE_CURSOR, LR_DEFAULTSIZE,
1724
LR_DEFAULTSIZE, LR_SHARED);
1725
g_hBmpStart = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPRUN),
1726
IMAGE_BITMAP, XBITMAP, YBITMAP,
1728
g_hBmpStop = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPSTOP),
1729
IMAGE_BITMAP, XBITMAP, YBITMAP,
1732
hMutex = CreateMutex(NULL, FALSE, "APSRVMON_MUTEX");
1733
if ((hMutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS))
1735
ErrorMessage(g_lpMsg[IDS_MSG_APPRUNNING - IDS_MSG_FIRST], FALSE);
1737
CloseHandle(hMutex);
1742
memset(g_stServices, 0, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);
1744
InitCommonControls();
1745
g_hInstance = hInstance;
1746
g_hwndMain = CreateMainWindow(hInstance);
1747
g_bUiTaskbarCreated = RegisterWindowMessage("TaskbarCreated");
1748
InitializeCriticalSection(&g_stcSection);
1749
g_hwndServiceDlg = NULL;
1750
if (g_hwndMain != NULL)
1752
while (GetMessage(&msg, NULL, 0, 0) == TRUE)
1754
TranslateMessage(&msg);
1755
DispatchMessage(&msg);
1757
am_ClearServicesSt();
1759
am_ClearComputersSt();
1760
DeleteCriticalSection(&g_stcSection);
1761
CloseHandle(hMutex);
1762
DestroyIcon(g_icoStop);
1763
DestroyIcon(g_icoRun);
1764
DestroyCursor(g_hCursorHourglass);
1765
DestroyCursor(g_hCursorArrow);
1766
DeleteObject(g_hBmpStart);
1767
DeleteObject(g_hBmpStop);