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 #define OS_VERSION_WINXP 4
53 #define OS_VERSION_VISTA 5
54 /* Should be enough */
55 #define MAX_APACHE_SERVICES 128
56 #define MAX_APACHE_COMPUTERS 32
58 #define WM_TRAYMESSAGE (WM_APP+1)
59 #define WM_UPDATEMESSAGE (WM_USER+1)
60 #define WM_MANAGEMESSAGE (WM_USER+2)
61 #define WM_TIMER_REFRESH 10
62 #define WM_TIMER_RESCAN 11
63 #define SERVICE_APACHE_RESTART 128
66 #define MAX_LOADSTRING 100
67 #define REFRESH_TIME 2000 /* service refresh time (ms) */
68 #define RESCAN_TIME 20000 /* registry rescan time (ms) */
70 typedef struct _st_APACHE_SERVICE
80 typedef struct _st_MONITORED_COMPUTERS
86 /* Global variables */
87 HINSTANCE g_hInstance = NULL;
88 CHAR *g_szTitle; /* The title bar text */
89 CHAR *g_szWindowClass; /* Window Class Name */
92 UINT g_bUiTaskbarCreated;
94 BOOL g_bDlgServiceOn = FALSE;
95 BOOL g_bConsoleRun = FALSE;
96 ST_APACHE_SERVICE g_stServices[MAX_APACHE_SERVICES];
97 ST_MONITORED_COMP g_stComputers[MAX_APACHE_COMPUTERS];
99 HBITMAP g_hBmpStart, g_hBmpStop;
100 HBITMAP g_hBmpPicture, g_hBmpOld;
101 BOOL g_bRescanServices;
102 HWND g_hwndServiceDlg;
104 HWND g_hwndStdoutList;
105 HWND g_hwndConnectDlg;
106 HCURSOR g_hCursorHourglass;
107 HCURSOR g_hCursorArrow;
109 HANDLE g_hpipeOutRead;
110 HANDLE g_hpipeOutWrite;
111 HANDLE g_hpipeInRead;
112 HANDLE g_hpipeInWrite;
113 HANDLE g_hpipeStdError;
115 PROCESS_INFORMATION g_lpRedirectProc;
116 CRITICAL_SECTION g_stcSection;
119 /* locale language support */
120 static CHAR *g_lpMsg[IDS_MSG_LAST - IDS_MSG_FIRST + 1];
123 void am_ClearServicesSt()
126 for (i = 0; i < MAX_APACHE_SERVICES; i++)
128 if (g_stServices[i].szServiceName) {
129 free(g_stServices[i].szServiceName);
131 if (g_stServices[i].szDisplayName) {
132 free(g_stServices[i].szDisplayName);
134 if (g_stServices[i].szDescription) {
135 free(g_stServices[i].szDescription);
137 if (g_stServices[i].szImagePath) {
138 free(g_stServices[i].szImagePath);
140 if (g_stServices[i].szComputerName) {
141 free(g_stServices[i].szComputerName);
145 memset(g_stServices, 0, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);
150 void am_ClearComputersSt()
153 for (i = 0; i < MAX_APACHE_COMPUTERS; i++) {
154 if (g_stComputers[i].szComputerName) {
155 free(g_stComputers[i].szComputerName);
156 RegCloseKey(g_stComputers[i].hRegistry);
159 memset(g_stComputers, 0, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);
164 BOOL am_IsComputerConnected(LPSTR szComputerName)
167 while (g_stComputers[i].szComputerName != NULL) {
168 if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
177 void am_DisconnectComputer(LPSTR szComputerName)
180 while (g_stComputers[i].szComputerName != NULL) {
181 if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
186 if (g_stComputers[i].szComputerName != NULL) {
187 free(g_stComputers[i].szComputerName);
188 RegCloseKey(g_stComputers[i].hRegistry);
189 for (j = i; j < MAX_APACHE_COMPUTERS - 1; j++) {
190 g_stComputers[i].szComputerName= g_stComputers[i+1].szComputerName;
191 g_stComputers[i].hRegistry = g_stComputers[i+1].hRegistry;
193 for (i = j; i < MAX_APACHE_COMPUTERS; i++) {
194 g_stComputers[i].szComputerName = NULL;
195 g_stComputers[i].hRegistry = NULL;
202 void ErrorMessage(LPCSTR szError, BOOL bFatal)
204 LPVOID lpMsgBuf = NULL;
206 MessageBox(NULL, szError, g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST],
207 MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION));
210 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
211 FORMAT_MESSAGE_FROM_SYSTEM |
212 FORMAT_MESSAGE_IGNORE_INSERTS,
213 NULL, GetLastError(), g_LangID,
214 (LPSTR) &lpMsgBuf, 0, NULL);
215 MessageBox(NULL, (LPCSTR)lpMsgBuf,
216 g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST],
217 MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION));
226 BOOL am_ConnectComputer(LPSTR szComputerName)
230 char szTmp[MAX_PATH];
232 while (g_stComputers[i].szComputerName != NULL) {
233 if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
238 if (i > MAX_APACHE_COMPUTERS - 1) {
241 if (RegConnectRegistry(szComputerName, HKEY_LOCAL_MACHINE, &hKeyRemote)
243 sprintf(szTmp, g_lpMsg[IDS_MSG_ECONNECT - IDS_MSG_FIRST],
245 ErrorMessage(szTmp, FALSE);
249 g_stComputers[i].szComputerName = strdup(szComputerName);
250 g_stComputers[i].hRegistry = hKeyRemote;
256 LPSTR GetStringRes(int id)
258 static CHAR buffer[MAX_PATH];
261 LoadString(GetModuleHandle(NULL), id, buffer, MAX_PATH);
266 BOOL GetSystemOSVersion(LPDWORD dwVersion)
270 Try calling GetVersionEx using the OSVERSIONINFOEX structure.
271 If that fails, try using the OSVERSIONINFO structure.
273 memset(&osvi, 0, sizeof(OSVERSIONINFO));
274 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
276 if (!GetVersionEx(&osvi)) {
280 switch (osvi.dwPlatformId)
282 case VER_PLATFORM_WIN32_NT:
283 if (osvi.dwMajorVersion == 5) {
284 if (osvi.dwMinorVersion == 1)
285 *dwVersion = OS_VERSION_WINXP;
287 *dwVersion = OS_VERSION_WIN2K;
289 else if (osvi.dwMajorVersion == 6) {
290 *dwVersion = OS_VERSION_VISTA;
293 *dwVersion = OS_VERSION_WINNT;
296 case VER_PLATFORM_WIN32_WINDOWS:
297 *dwVersion = OS_VERSION_WIN9X;
300 case VER_PLATFORM_WIN32s:
309 static VOID ShowNotifyIcon(HWND hWnd, DWORD dwMessage)
314 memset(&nid, 0, sizeof(nid));
315 nid.cbSize = sizeof(NOTIFYICONDATA);
318 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
319 nid.uCallbackMessage = WM_TRAYMESSAGE;
321 while (g_stServices[i].szServiceName != NULL)
323 if (g_stServices[i].dwPid != 0) {
328 if (dwMessage != NIM_DELETE)
331 nid.hIcon = g_icoRun;
334 nid.hIcon = g_icoStop;
340 if (n == i && n > 0) {
341 lstrcpy(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGALL - IDS_MSG_FIRST]);
344 sprintf(nid.szTip, g_lpMsg[IDS_MSG_RUNNING - IDS_MSG_FIRST], n, i);
347 sprintf(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGNONE - IDS_MSG_FIRST], i);
350 lstrcpy(nid.szTip, g_lpMsg[IDS_MSG_NOSERVICES - IDS_MSG_FIRST]);
352 Shell_NotifyIcon(dwMessage, &nid);
356 void appendMenuItem(HMENU hMenu, UINT uMenuId, LPSTR szName,
357 BOOL fDefault, BOOL fEnabled)
361 memset(&mii, 0, sizeof(MENUITEMINFO));
362 mii.cbSize = sizeof(MENUITEMINFO);
363 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
366 mii.fType = MFT_STRING;
369 mii.fState = MFS_DEFAULT;
372 mii.fState |= MFS_DISABLED;
374 mii.dwTypeData = szName;
377 mii.fType = MFT_SEPARATOR;
379 InsertMenuItem(hMenu, uMenuId, FALSE, &mii);
383 void appendServiceMenu(HMENU hMenu, UINT uMenuId,
384 LPSTR szServiceName, BOOL fRunning)
389 smh = CreatePopupMenu();
391 appendMenuItem(smh, IDM_SM_START + uMenuId,
392 g_lpMsg[IDS_MSG_SSTART - IDS_MSG_FIRST], FALSE, !fRunning);
393 appendMenuItem(smh, IDM_SM_STOP + uMenuId,
394 g_lpMsg[IDS_MSG_SSTOP - IDS_MSG_FIRST], FALSE, fRunning);
395 appendMenuItem(smh, IDM_SM_RESTART + uMenuId,
396 g_lpMsg[IDS_MSG_SRESTART - IDS_MSG_FIRST], FALSE, fRunning);
398 memset(&mii, 0, sizeof(MENUITEMINFO));
399 mii.cbSize = sizeof(MENUITEMINFO);
400 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_SUBMENU
402 mii.fType = MFT_STRING;
404 mii.hbmpChecked = g_hBmpStart;
405 mii.hbmpUnchecked = g_hBmpStop;
406 mii.dwTypeData = szServiceName;
408 mii.fState = fRunning ? MFS_CHECKED : MFS_UNCHECKED;
409 InsertMenuItem(hMenu, IDM_SM_SERVICE + uMenuId, FALSE, &mii);
413 void ShowTryPopupMenu(HWND hWnd)
415 /* create popup menu */
416 HMENU hMenu = CreatePopupMenu();
421 appendMenuItem(hMenu, IDM_RESTORE,
422 g_lpMsg[IDS_MSG_MNUSHOW - IDS_MSG_FIRST],
424 if (g_dwOSVersion >= OS_VERSION_WINNT) {
425 appendMenuItem(hMenu, IDC_SMANAGER,
426 g_lpMsg[IDS_MSG_MNUSERVICES - IDS_MSG_FIRST],
429 appendMenuItem(hMenu, 0, "", FALSE, TRUE);
430 appendMenuItem(hMenu, IDM_EXIT,
431 g_lpMsg[IDS_MSG_MNUEXIT - IDS_MSG_FIRST],
434 if (!SetForegroundWindow(hWnd)) {
435 SetForegroundWindow(NULL);
438 TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
439 pt.x, pt.y, 0, hWnd, NULL);
445 void ShowTryServicesMenu(HWND hWnd)
447 /* create services list popup menu and submenus */
448 HMENU hMenu = CreatePopupMenu();
454 while (g_stServices[i].szServiceName != NULL)
456 appendServiceMenu(hMenu, i, g_stServices[i].szDisplayName,
457 g_stServices[i].dwPid != 0);
462 if (!SetForegroundWindow(hWnd)) {
463 SetForegroundWindow(NULL);
466 TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
467 pt.x, pt.y, 0, hWnd, NULL);
474 BOOL CenterWindow(HWND hwndChild)
476 RECT rChild, rWorkArea;
481 /* Get the Height and Width of the child window */
482 GetWindowRect(hwndChild, &rChild);
483 wChild = rChild.right - rChild.left;
484 hChild = rChild.bottom - rChild.top;
486 /* Get the limits of the 'workarea' */
487 bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT),
490 rWorkArea.left = rWorkArea.top = 0;
491 rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
492 rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
495 /* Calculate new X and Y position*/
496 xNew = (rWorkArea.right - wChild) / 2;
497 yNew = (rWorkArea.bottom - hChild) / 2;
498 return SetWindowPos(hwndChild, HWND_TOP, xNew, yNew, 0, 0,
499 SWP_NOSIZE | SWP_SHOWWINDOW);
503 static void addListBoxItem(HWND hDlg, LPSTR lpStr, HBITMAP hBmp)
507 nItem = SendMessage(hDlg, LB_ADDSTRING, 0, (LPARAM)lpStr);
508 SendMessage(hDlg, LB_SETITEMDATA, nItem, (LPARAM)hBmp);
512 static void addListBoxString(HWND hListBox, LPSTR lpStr)
514 static int nItems = 0;
515 if (!g_bDlgServiceOn) {
519 if (nItems > MAX_LOADSTRING)
521 SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
524 ListBox_SetCurSel(hListBox,
525 ListBox_AddString(hListBox, lpStr));
530 static DWORD WINAPI ConsoleOutputThread(LPVOID lpThreadParameter)
532 static BYTE lpBuffer[MAX_PATH+1];
537 while (ReadFile(g_hpipeOutRead, &ch, 1, &dwReaded, NULL) == TRUE)
541 if (ch == '\n' || nPtr >= MAX_PATH)
543 lpBuffer[nPtr] = '\0';
544 addListBoxString(g_hwndStdoutList, lpBuffer);
547 else if (ch == '\t' && nPtr < (MAX_PATH - 4))
550 for (i = 0; i < 4; ++i) {
551 lpBuffer[nPtr++] = ' ';
554 else if (ch != '\r') {
555 lpBuffer[nPtr++] = ch;
559 CloseHandle(g_hpipeInWrite);
560 CloseHandle(g_hpipeOutRead);
561 CloseHandle(g_hpipeStdError);
566 DWORD WINAPI ConsoleWaitingThread(LPVOID lpThreadParameter)
568 WaitForSingleObject(g_lpRedirectProc.hThread, INFINITE);
569 CloseHandle(g_lpRedirectProc.hThread);
571 g_bConsoleRun = FALSE;
572 SetCursor(g_hCursorArrow);
577 BOOL RunRedirectedConsole(LPSTR szCmdLine)
584 memset(&stInfo, 0, sizeof(stInfo));
585 stInfo.cb = sizeof(stInfo);
586 stInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
587 stInfo.wShowWindow = SW_HIDE;
589 hProc = GetCurrentProcess();
591 if (!CreatePipe(&g_hpipeInRead, &g_hpipeInWrite, NULL, MAX_PATH)) {
592 ErrorMessage(NULL, TRUE);
594 if (!CreatePipe(&g_hpipeOutRead, &g_hpipeOutWrite, NULL, MAX_PATH*8)) {
595 ErrorMessage(NULL, TRUE);
597 DuplicateHandle(hProc, g_hpipeInRead, hProc, &g_hpipeInRead, 0, TRUE,
598 DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
599 DuplicateHandle(hProc, g_hpipeOutWrite, hProc, &g_hpipeOutWrite, 0, TRUE,
600 DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
601 DuplicateHandle(hProc, g_hpipeOutWrite, hProc, &g_hpipeStdError, 0, TRUE,
602 DUPLICATE_SAME_ACCESS);
603 if (!g_hpipeInRead && !g_hpipeOutWrite && !g_hpipeStdError) {
604 ErrorMessage(NULL, TRUE);
606 stInfo.hStdInput = g_hpipeInRead;
607 stInfo.hStdOutput = g_hpipeOutWrite;
608 stInfo.hStdError = g_hpipeStdError;
610 bResult = CreateProcess(NULL,
622 CloseHandle(g_hpipeInRead);
623 CloseHandle(g_hpipeOutWrite);
624 CloseHandle(g_hpipeStdError);
628 CloseHandle(g_hpipeInWrite);
629 CloseHandle(g_hpipeOutRead);
630 CloseHandle(g_hpipeStdError);
634 CloseHandle(CreateThread(NULL, 0, ConsoleOutputThread,
636 ResumeThread(g_lpRedirectProc.hThread);
637 CloseHandle(CreateThread(NULL, 0, ConsoleWaitingThread,
644 BOOL RunAndForgetConsole(LPSTR szCmdLine, BOOL bRedirectConsole)
647 PROCESS_INFORMATION prInfo;
650 if (bRedirectConsole) {
651 return RunRedirectedConsole(szCmdLine);
654 memset(&stInfo, 0, sizeof(stInfo));
655 stInfo.cb = sizeof(stInfo);
656 stInfo.dwFlags = STARTF_USESHOWWINDOW;
657 stInfo.wShowWindow = SW_HIDE;
659 bResult = CreateProcess(NULL,
673 if (g_dwOSVersion == OS_VERSION_WIN9X) {
674 /* give some time to rescan the status */
677 CloseHandle(prInfo.hThread);
678 CloseHandle(prInfo.hProcess);
683 BOOL ApacheManageService(LPCSTR szServiceName, LPCSTR szImagePath,
684 LPSTR szComputerName, DWORD dwCommand)
686 CHAR szBuf[MAX_PATH];
687 CHAR szMsg[MAX_PATH];
690 BOOL serviceFlag = TRUE;
691 SC_HANDLE schService;
692 SC_HANDLE schSCManager;
693 SERVICE_STATUS schSStatus;
696 if (g_dwOSVersion == OS_VERSION_WIN9X)
698 sPos = strstr(szImagePath, "-k start");
701 lstrcpyn(szBuf, szImagePath, (int)(sPos - szImagePath));
704 case SERVICE_CONTROL_STOP:
705 lstrcat(szBuf, " -k shutdown -n ");
708 case SERVICE_CONTROL_CONTINUE:
709 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTART - IDS_MSG_FIRST],
711 addListBoxString(g_hwndStdoutList, szMsg);
712 lstrcat(szBuf, " -k start -n ");
716 case SERVICE_APACHE_RESTART:
717 lstrcat(szBuf, " -k restart -n ");
723 lstrcat(szBuf, szServiceName);
728 g_bConsoleRun = TRUE;
729 SetCursor(g_hCursorHourglass);
730 if (!RunAndForgetConsole(szBuf, serviceFlag))
732 ErrorMessage(NULL, FALSE);
733 g_bConsoleRun = FALSE;
734 SetCursor(g_hCursorArrow);
737 else if (!serviceFlag)
739 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTARTED - IDS_MSG_FIRST],
741 addListBoxString(g_hwndStdoutList, szMsg);
742 g_bConsoleRun = FALSE;
743 SetCursor(g_hCursorArrow);
749 schSCManager = OpenSCManager(szComputerName, NULL,
755 schService = OpenService(schSCManager, szServiceName,
756 SERVICE_QUERY_STATUS | SERVICE_START |
757 SERVICE_STOP | SERVICE_USER_DEFINED_CONTROL);
758 if (schService != NULL)
761 g_bConsoleRun = TRUE;
762 SetCursor(g_hCursorHourglass);
765 case SERVICE_CONTROL_STOP:
766 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTOP - IDS_MSG_FIRST],
768 addListBoxString(g_hwndStdoutList, szMsg);
769 if (ControlService(schService, SERVICE_CONTROL_STOP,
772 while (QueryServiceStatus(schService, &schSStatus))
774 if (schSStatus.dwCurrentState == SERVICE_STOP_PENDING)
783 if (QueryServiceStatus(schService, &schSStatus))
785 if (schSStatus.dwCurrentState == SERVICE_STOPPED)
789 g_lpMsg[IDS_MSG_SRVSTOPPED - IDS_MSG_FIRST],
791 addListBoxString(g_hwndStdoutList, szMsg);
796 case SERVICE_CONTROL_CONTINUE:
797 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTART - IDS_MSG_FIRST],
799 addListBoxString(g_hwndStdoutList, szMsg);
801 if (StartService(schService, 0, NULL))
804 while (QueryServiceStatus(schService, &schSStatus))
806 if (schSStatus.dwCurrentState == SERVICE_START_PENDING)
815 if (QueryServiceStatus(schService, &schSStatus))
817 if (schSStatus.dwCurrentState == SERVICE_RUNNING)
821 g_lpMsg[IDS_MSG_SRVSTARTED - IDS_MSG_FIRST],
823 addListBoxString(g_hwndStdoutList, szMsg);
828 case SERVICE_APACHE_RESTART:
829 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVRESTART - IDS_MSG_FIRST],
831 addListBoxString(g_hwndStdoutList, szMsg);
832 if (ControlService(schService, SERVICE_APACHE_RESTART,
836 while (schSStatus.dwCurrentState == SERVICE_START_PENDING)
839 if (!QueryServiceStatus(schService, &schSStatus))
841 CloseServiceHandle(schService);
842 CloseServiceHandle(schSCManager);
843 g_bConsoleRun = FALSE;
844 SetCursor(g_hCursorArrow);
852 if (schSStatus.dwCurrentState == SERVICE_RUNNING)
856 g_lpMsg[IDS_MSG_SRVRESTARTED - IDS_MSG_FIRST],
858 addListBoxString(g_hwndStdoutList, szMsg);
862 CloseServiceHandle(schService);
863 CloseServiceHandle(schSCManager);
865 ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED - IDS_MSG_FIRST],
868 g_bConsoleRun = FALSE;
869 SetCursor(g_hCursorArrow);
873 g_bRescanServices = TRUE;
875 CloseServiceHandle(schSCManager);
883 BOOL IsServiceRunning(LPCSTR szServiceName, LPCSTR szComputerName,
888 SC_HANDLE schService;
889 SC_HANDLE schSCManager;
890 SERVICE_STATUS schSStatus;
892 if (g_dwOSVersion == OS_VERSION_WIN9X)
894 hWnd = FindWindow("ApacheWin95ServiceMonitor", szServiceName);
895 if (hWnd && GetWindowThreadProcessId(hWnd, &dwPid))
907 schSCManager = OpenSCManager(szComputerName, NULL,
913 schService = OpenService(schSCManager, szServiceName,
914 SERVICE_QUERY_STATUS);
915 if (schService != NULL)
917 if (QueryServiceStatus(schService, &schSStatus))
919 dwPid = schSStatus.dwCurrentState;
924 CloseServiceHandle(schService);
925 CloseServiceHandle(schSCManager);
926 return dwPid == SERVICE_RUNNING ? TRUE : FALSE;
929 g_bRescanServices = TRUE;
931 CloseServiceHandle(schSCManager);
940 BOOL FindRunningServices(void)
945 while (g_stServices[i].szServiceName != NULL)
947 if (!IsServiceRunning(g_stServices[i].szServiceName,
948 g_stServices[i].szComputerName, &dwPid)) {
951 if (g_stServices[i].dwPid != dwPid) {
954 g_stServices[i].dwPid = dwPid;
961 BOOL GetApacheServicesStatus()
963 CHAR szKey[MAX_PATH];
964 CHAR achKey[MAX_PATH];
965 CHAR szImagePath[MAX_PATH];
966 CHAR szBuf[MAX_PATH];
967 CHAR szTmp[MAX_PATH];
968 HKEY hKey, hSubKey, hKeyRemote;
969 DWORD retCode, rv, dwKeyType;
970 DWORD dwBufLen = MAX_PATH;
974 g_bRescanServices = FALSE;
976 am_ClearServicesSt();
977 while (g_stComputers[computers].szComputerName != NULL) {
978 hKeyRemote = g_stComputers[computers].hRegistry;
979 retCode = RegOpenKeyEx(hKeyRemote,
980 "System\\CurrentControlSet\\Services\\",
982 if (retCode != ERROR_SUCCESS)
984 ErrorMessage(NULL, FALSE);
987 for (i = 0, retCode = ERROR_SUCCESS; retCode == ERROR_SUCCESS; i++)
989 retCode = RegEnumKey(hKey, i, achKey, MAX_PATH);
990 if (retCode == ERROR_SUCCESS)
992 lstrcpy(szKey, "System\\CurrentControlSet\\Services\\");
993 lstrcat(szKey, achKey);
995 if (RegOpenKeyEx(hKeyRemote, szKey, 0,
996 KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS)
999 rv = RegQueryValueEx(hSubKey, "ImagePath", NULL,
1000 &dwKeyType, szImagePath, &dwBufLen);
1002 if (rv == ERROR_SUCCESS
1003 && (dwKeyType == REG_SZ
1004 || dwKeyType == REG_EXPAND_SZ)
1007 lstrcpy(szBuf, szImagePath);
1009 /* the service name could be httpd*.exe or Apache*.exe */
1010 if (((strstr(szBuf, "\\apache") != NULL)
1011 || (strstr(szBuf, "\\httpd") != NULL))
1012 && strstr(szBuf, ".exe")
1013 && (strstr(szBuf, "--ntservice") != NULL
1014 || strstr(szBuf, "-k ") != NULL))
1016 g_stServices[stPos].szServiceName = strdup(achKey);
1017 g_stServices[stPos].szImagePath =
1018 strdup(szImagePath);
1019 g_stServices[stPos].szComputerName =
1020 strdup(g_stComputers[computers].szComputerName);
1021 dwBufLen = MAX_PATH;
1022 if (RegQueryValueEx(hSubKey, "Description", NULL,
1023 &dwKeyType, szBuf, &dwBufLen)
1025 g_stServices[stPos].szDescription =
1028 dwBufLen = MAX_PATH;
1029 if (RegQueryValueEx(hSubKey, "DisplayName", NULL,
1030 &dwKeyType, szBuf, &dwBufLen)
1033 if (strcmp(g_stComputers[computers]
1034 .szComputerName, g_szLocalHost) != 0)
1036 strcpy(szTmp, g_stComputers[computers]
1037 .szComputerName + 2);
1039 strcat(szTmp, szBuf);
1042 strcpy(szTmp, szBuf);
1044 g_stServices[stPos].szDisplayName
1049 if (stPos >= MAX_APACHE_SERVICES) {
1050 retCode = !ERROR_SUCCESS;
1054 RegCloseKey(hSubKey);
1061 FindRunningServices();
1066 LRESULT CALLBACK ConnectDlgProc(HWND hDlg, UINT message,
1067 WPARAM wParam, LPARAM lParam)
1069 CHAR szCmp[MAX_COMPUTERNAME_LENGTH+4];
1073 ShowWindow(hDlg, SW_HIDE);
1074 g_hwndConnectDlg = hDlg;
1076 ShowWindow(hDlg, SW_SHOW);
1077 SetFocus(GetDlgItem(hDlg, IDC_COMPUTER));
1081 switch (LOWORD(wParam))
1084 memset(szCmp, 0, MAX_COMPUTERNAME_LENGTH+4);
1085 strcpy(szCmp, "\\\\");
1086 SendMessage(GetDlgItem(hDlg, IDC_COMPUTER), WM_GETTEXT,
1087 (WPARAM) MAX_COMPUTERNAME_LENGTH,
1091 if (strlen(szCmp) < 3) {
1092 EndDialog(hDlg, TRUE);
1095 am_ConnectComputer(szCmp);
1096 SendMessage(g_hwndMain, WM_TIMER, WM_TIMER_RESCAN, 0);
1099 EndDialog(hDlg, TRUE);
1107 memset(&bi, 0, sizeof(BROWSEINFO));
1108 SHGetSpecialFolderLocation(hDlg, CSIDL_NETWORK, &il);
1110 bi.lpszTitle = "ApacheMonitor :\nSelect Network Computer!";
1111 bi.pszDisplayName = szCmp;
1112 bi.hwndOwner = hDlg;
1113 bi.ulFlags = BIF_BROWSEFORCOMPUTER;
1119 if (SHBrowseForFolder(&bi) != NULL) {
1120 SendMessage(GetDlgItem(hDlg, IDC_COMPUTER),
1122 (WPARAM) NULL, (LPARAM) szCmp);
1124 if (SHGetMalloc(&pMalloc)) {
1125 pMalloc->lpVtbl->Free(pMalloc, il);
1126 pMalloc->lpVtbl->Release(pMalloc);
1135 EndDialog(hDlg, TRUE);
1146 LRESULT CALLBACK ServiceDlgProc(HWND hDlg, UINT message,
1147 WPARAM wParam, LPARAM lParam)
1149 CHAR szBuf[MAX_PATH];
1151 static HWND hStatusBar;
1157 LPMEASUREITEMSTRUCT lpmis;
1158 LPDRAWITEMSTRUCT lpdis;
1160 memset(szBuf, 0, MAX_PATH);
1164 ShowWindow(hDlg, SW_HIDE);
1165 g_hwndServiceDlg = hDlg;
1166 SetWindowText(hDlg, g_szTitle);
1167 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1168 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1169 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1170 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1171 SetWindowText(GetDlgItem(hDlg, IDC_SSTART),
1172 g_lpMsg[IDS_MSG_SSTART - IDS_MSG_FIRST]);
1173 SetWindowText(GetDlgItem(hDlg, IDC_SSTOP),
1174 g_lpMsg[IDS_MSG_SSTOP - IDS_MSG_FIRST]);
1175 SetWindowText(GetDlgItem(hDlg, IDC_SRESTART),
1176 g_lpMsg[IDS_MSG_SRESTART - IDS_MSG_FIRST]);
1177 SetWindowText(GetDlgItem(hDlg, IDC_SMANAGER),
1178 g_lpMsg[IDS_MSG_SERVICES - IDS_MSG_FIRST]);
1179 SetWindowText(GetDlgItem(hDlg, IDC_SCONNECT),
1180 g_lpMsg[IDS_MSG_CONNECT - IDS_MSG_FIRST]);
1181 SetWindowText(GetDlgItem(hDlg, IDC_SEXIT),
1182 g_lpMsg[IDS_MSG_MNUEXIT - IDS_MSG_FIRST]);
1183 if (g_dwOSVersion < OS_VERSION_WINNT)
1185 ShowWindow(GetDlgItem(hDlg, IDC_SMANAGER), SW_HIDE);
1186 ShowWindow(GetDlgItem(hDlg, IDC_SCONNECT), SW_HIDE);
1187 ShowWindow(GetDlgItem(hDlg, IDC_SDISCONN), SW_HIDE);
1189 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1190 g_hwndStdoutList = GetDlgItem(hDlg, IDL_STDOUT);
1191 hStatusBar = CreateStatusWindow(0x0800 /* SBT_TOOLTIPS */
1192 | WS_CHILD | WS_VISIBLE,
1193 "", hDlg, IDC_STATBAR);
1194 if (GetApacheServicesStatus())
1197 while (g_stServices[i].szServiceName != NULL)
1199 addListBoxItem(hListBox, g_stServices[i].szDisplayName,
1200 g_stServices[i].dwPid == 0 ? g_hBmpStop
1206 ShowWindow(hDlg, SW_SHOW);
1208 SendMessage(hListBox, LB_SETCURSEL, 0, 0);
1212 case WM_MANAGEMESSAGE:
1213 ApacheManageService(g_stServices[LOWORD(wParam)].szServiceName,
1214 g_stServices[LOWORD(wParam)].szImagePath,
1215 g_stServices[LOWORD(wParam)].szComputerName,
1221 case WM_UPDATEMESSAGE:
1222 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1223 SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
1224 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)"");
1225 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1226 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1227 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1228 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1230 while (g_stServices[i].szServiceName != NULL)
1232 addListBoxItem(hListBox, g_stServices[i].szDisplayName,
1233 g_stServices[i].dwPid == 0 ? g_hBmpStop : g_hBmpStart);
1236 SendMessage(hListBox, LB_SETCURSEL, 0, 0);
1237 /* Dirty hack to bring the window to the foreground */
1238 SetWindowPos(hDlg, HWND_TOPMOST, 0, 0, 0, 0,
1239 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1240 SetWindowPos(hDlg, HWND_NOTOPMOST, 0, 0, 0, 0,
1241 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1246 case WM_MEASUREITEM:
1247 lpmis = (LPMEASUREITEMSTRUCT) lParam;
1248 lpmis->itemHeight = YBITMAP;
1252 if (g_bConsoleRun) {
1253 SetCursor(g_hCursorHourglass);
1256 SetCursor(g_hCursorArrow);
1261 lpdis = (LPDRAWITEMSTRUCT) lParam;
1262 if (lpdis->itemID == -1) {
1265 switch (lpdis->itemAction)
1268 case ODA_DRAWENTIRE:
1269 g_hBmpPicture = (HBITMAP)SendMessage(lpdis->hwndItem,
1271 lpdis->itemID, (LPARAM) 0);
1273 hdcMem = CreateCompatibleDC(lpdis->hDC);
1274 g_hBmpOld = SelectObject(hdcMem, g_hBmpPicture);
1276 BitBlt(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
1277 lpdis->rcItem.right - lpdis->rcItem.left,
1278 lpdis->rcItem.bottom - lpdis->rcItem.top,
1279 hdcMem, 0, 0, SRCCOPY);
1280 SendMessage(lpdis->hwndItem, LB_GETTEXT,
1281 lpdis->itemID, (LPARAM) szBuf);
1283 GetTextMetrics(lpdis->hDC, &tm);
1284 y = (lpdis->rcItem.bottom + lpdis->rcItem.top - tm.tmHeight) / 2;
1286 SelectObject(hdcMem, g_hBmpOld);
1289 rcBitmap.left = lpdis->rcItem.left + XBITMAP + 2;
1290 rcBitmap.top = lpdis->rcItem.top;
1291 rcBitmap.right = lpdis->rcItem.right;
1292 rcBitmap.bottom = lpdis->rcItem.top + YBITMAP;
1294 if (lpdis->itemState & ODS_SELECTED)
1296 if (g_hBmpPicture == g_hBmpStop)
1298 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), TRUE);
1299 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1300 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1302 else if (g_hBmpPicture == g_hBmpStart)
1304 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1305 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), TRUE);
1306 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), TRUE);
1309 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1310 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1311 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1313 if (strcmp(g_stServices[lpdis->itemID].szComputerName,
1314 g_szLocalHost) == 0) {
1315 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1318 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), TRUE);
1321 if (g_stServices[lpdis->itemID].szDescription) {
1322 SendMessage(hStatusBar, SB_SETTEXT, 0,
1323 (LPARAM)g_stServices[lpdis->itemID].szDescription);
1326 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)"");
1328 SetTextColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
1329 SetBkColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHT));
1330 FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_HIGHLIGHTTEXT));
1334 SetTextColor(lpdis->hDC, GetSysColor(COLOR_MENUTEXT));
1335 SetBkColor(lpdis->hDC, GetSysColor(COLOR_WINDOW));
1336 FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_WINDOW+1));
1338 TextOut(lpdis->hDC, XBITMAP + 6, y, szBuf, (int)strlen(szBuf));
1346 switch (LOWORD(wParam))
1349 switch (HIWORD(wParam))
1352 /* if started then stop, if stopped then start */
1353 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1354 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1355 if (nItem != LB_ERR)
1357 g_hBmpPicture = (HBITMAP)SendMessage(hListBox,
1360 if (g_hBmpPicture == g_hBmpStop) {
1361 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1362 SERVICE_CONTROL_CONTINUE);
1365 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1366 SERVICE_CONTROL_STOP);
1375 EndDialog(hDlg, TRUE);
1379 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1380 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1381 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1382 if (nItem != LB_ERR) {
1383 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1384 SERVICE_CONTROL_CONTINUE);
1386 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), TRUE);
1390 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1391 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1392 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1393 if (nItem != LB_ERR) {
1394 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1395 SERVICE_CONTROL_STOP);
1397 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), TRUE);
1401 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1402 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1403 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1404 if (nItem != LB_ERR) {
1405 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1406 SERVICE_APACHE_RESTART);
1408 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), TRUE);
1412 if (g_dwOSVersion >= OS_VERSION_WIN2K) {
1413 ShellExecute(hDlg, "open", "services.msc", "/s",
1417 WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL);
1422 EndDialog(hDlg, TRUE);
1423 SendMessage(g_hwndMain, WM_COMMAND, (WPARAM)IDM_EXIT, 0);
1427 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGCONNECT),
1428 hDlg, (DLGPROC)ConnectDlgProc);
1432 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1433 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1434 if (nItem != LB_ERR) {
1435 am_DisconnectComputer(g_stServices[nItem].szComputerName);
1436 SendMessage(g_hwndMain, WM_TIMER, WM_TIMER_RESCAN, 0);
1443 switch (LOWORD(wParam))
1445 case SIZE_MINIMIZED:
1446 EndDialog(hDlg, TRUE);
1454 EndDialog(hDlg, TRUE);
1464 LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
1465 WPARAM wParam, LPARAM lParam)
1467 if (message == g_bUiTaskbarCreated)
1469 /* restore the tray icon on shell restart */
1470 ShowNotifyIcon(hWnd, NIM_ADD);
1471 return DefWindowProc(hWnd, message, wParam, lParam);
1476 GetApacheServicesStatus();
1477 ShowNotifyIcon(hWnd, NIM_ADD);
1478 SetTimer(hWnd, WM_TIMER_REFRESH, REFRESH_TIME, NULL);
1479 SetTimer(hWnd, WM_TIMER_RESCAN, RESCAN_TIME, NULL);
1485 case WM_TIMER_RESCAN:
1487 int nPrev = 0, nNew = 0;
1488 EnterCriticalSection(&g_stcSection);
1489 if (FindRunningServices() || g_bRescanServices)
1491 ShowNotifyIcon(hWnd, NIM_MODIFY);
1492 if (g_hwndServiceDlg)
1493 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1495 /* check if services list changed */
1496 while (g_stServices[nPrev].szServiceName != NULL)
1498 GetApacheServicesStatus();
1499 while (g_stServices[nNew].szServiceName != NULL)
1503 ShowNotifyIcon(hWnd, NIM_MODIFY);
1504 if (g_hwndServiceDlg) {
1505 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1508 LeaveCriticalSection(&g_stcSection);
1512 case WM_TIMER_REFRESH:
1514 int nPrev = 0, nNew = 0;
1515 EnterCriticalSection(&g_stcSection);
1516 if (g_bRescanServices)
1518 GetApacheServicesStatus();
1519 ShowNotifyIcon(hWnd, NIM_MODIFY);
1520 if (g_hwndServiceDlg) {
1521 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1524 else if (FindRunningServices())
1526 ShowNotifyIcon(hWnd, NIM_MODIFY);
1527 if (g_hwndServiceDlg) {
1528 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1531 LeaveCriticalSection(&g_stcSection);
1538 ShowNotifyIcon(hWnd, NIM_DELETE);
1541 case WM_TRAYMESSAGE:
1544 case WM_LBUTTONDBLCLK:
1545 if (!g_bDlgServiceOn)
1547 g_bDlgServiceOn = TRUE;
1548 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES),
1549 hWnd, (DLGPROC)ServiceDlgProc);
1550 g_bDlgServiceOn = FALSE;
1551 g_hwndServiceDlg = NULL;
1553 else if (IsWindow(g_hwndServiceDlg))
1555 /* Dirty hack to bring the window to the foreground */
1556 SetWindowPos(g_hwndServiceDlg, HWND_TOPMOST, 0, 0, 0, 0,
1557 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1558 SetWindowPos(g_hwndServiceDlg, HWND_NOTOPMOST, 0, 0, 0, 0,
1559 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1560 SetFocus(g_hwndServiceDlg);
1565 ShowTryServicesMenu(hWnd);
1569 ShowTryPopupMenu(hWnd);
1575 if ((LOWORD(wParam) & IDM_SM_START) == IDM_SM_START)
1577 ApacheManageService(g_stServices[LOWORD(wParam)
1578 - IDM_SM_START].szServiceName,
1579 g_stServices[LOWORD(wParam)
1580 - IDM_SM_START].szImagePath,
1581 g_stServices[LOWORD(wParam)
1582 - IDM_SM_START].szComputerName,
1583 SERVICE_CONTROL_CONTINUE);
1586 else if ((LOWORD(wParam) & IDM_SM_STOP) == IDM_SM_STOP)
1588 ApacheManageService(g_stServices[LOWORD(wParam)
1589 - IDM_SM_STOP].szServiceName,
1590 g_stServices[LOWORD(wParam)
1591 - IDM_SM_STOP].szImagePath,
1592 g_stServices[LOWORD(wParam)
1593 - IDM_SM_STOP].szComputerName,
1594 SERVICE_CONTROL_STOP);
1597 else if ((LOWORD(wParam) & IDM_SM_RESTART) == IDM_SM_RESTART)
1599 ApacheManageService(g_stServices[LOWORD(wParam)
1600 - IDM_SM_RESTART].szServiceName,
1601 g_stServices[LOWORD(wParam)
1602 - IDM_SM_RESTART].szImagePath,
1603 g_stServices[LOWORD(wParam)
1604 - IDM_SM_RESTART].szComputerName,
1605 SERVICE_APACHE_RESTART);
1608 switch (LOWORD(wParam))
1611 if (!g_bDlgServiceOn)
1613 g_bDlgServiceOn = TRUE;
1614 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES),
1615 hWnd, (DLGPROC)ServiceDlgProc);
1616 g_bDlgServiceOn = FALSE;
1617 g_hwndServiceDlg = NULL;
1619 else if (IsWindow(g_hwndServiceDlg)) {
1620 SetFocus(g_hwndServiceDlg);
1625 if (g_dwOSVersion >= OS_VERSION_WIN2K) {
1626 ShellExecute(NULL, "open", "services.msc", "/s",
1630 WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL);
1635 ShowNotifyIcon(hWnd, NIM_DELETE);
1641 return DefWindowProc(hWnd, message, wParam, lParam);
1648 /* Create main invisible window */
1649 HWND CreateMainWindow(HINSTANCE hInstance)
1654 if (!GetSystemOSVersion(&g_dwOSVersion))
1656 ErrorMessage(NULL, TRUE);
1660 wcex.cbSize = sizeof(WNDCLASSEX);
1662 wcex.style = CS_HREDRAW | CS_VREDRAW;
1663 wcex.lpfnWndProc = (WNDPROC)WndProc;
1664 wcex.cbClsExtra = 0;
1665 wcex.cbWndExtra = 0;
1666 wcex.hInstance = hInstance;
1667 wcex.hIcon = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON),
1668 IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
1669 wcex.hCursor = g_hCursorArrow;
1670 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
1671 wcex.lpszMenuName = 0;
1672 wcex.lpszClassName = g_szWindowClass;
1673 wcex.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON),
1674 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1676 if (RegisterClassEx(&wcex)) {
1677 hWnd = CreateWindow(g_szWindowClass, g_szTitle,
1679 NULL, NULL, hInstance, NULL);
1686 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1687 LPSTR lpCmdLine, int nCmdShow)
1689 CHAR szTmp[MAX_LOADSTRING];
1690 CHAR szCmp[MAX_COMPUTERNAME_LENGTH+4];
1692 /* single instance mutex */
1697 g_LangID = GetUserDefaultLangID();
1698 if ((g_LangID & 0xFF) != LANG_ENGLISH) {
1699 g_LangID = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
1701 for (i = IDS_MSG_FIRST; i <= IDS_MSG_LAST; ++i) {
1702 LoadString(hInstance, i, szTmp, MAX_LOADSTRING);
1703 g_lpMsg[i - IDS_MSG_FIRST] = strdup(szTmp);
1705 LoadString(hInstance, IDS_APMONITORTITLE, szTmp, MAX_LOADSTRING);
1706 d = MAX_COMPUTERNAME_LENGTH+1;
1707 strcpy(szCmp, "\\\\");
1708 GetComputerName(szCmp + 2, &d);
1710 g_szLocalHost = strdup(szCmp);
1712 memset(g_stComputers, 0, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);
1713 g_stComputers[0].szComputerName = strdup(szCmp);
1714 g_stComputers[0].hRegistry = HKEY_LOCAL_MACHINE;
1715 g_szTitle = strdup(szTmp);
1716 LoadString(hInstance, IDS_APMONITORCLASS, szTmp, MAX_LOADSTRING);
1717 g_szWindowClass = strdup(szTmp);
1719 g_icoStop = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICOSTOP),
1720 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1721 g_icoRun = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICORUN),
1722 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1723 g_hCursorHourglass = LoadImage(NULL, MAKEINTRESOURCE(OCR_WAIT),
1724 IMAGE_CURSOR, LR_DEFAULTSIZE,
1725 LR_DEFAULTSIZE, LR_SHARED);
1726 g_hCursorArrow = LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL),
1727 IMAGE_CURSOR, LR_DEFAULTSIZE,
1728 LR_DEFAULTSIZE, LR_SHARED);
1729 g_hBmpStart = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPRUN),
1730 IMAGE_BITMAP, XBITMAP, YBITMAP,
1732 g_hBmpStop = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPSTOP),
1733 IMAGE_BITMAP, XBITMAP, YBITMAP,
1736 hMutex = CreateMutex(NULL, FALSE, "APSRVMON_MUTEX");
1737 if ((hMutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS))
1739 ErrorMessage(g_lpMsg[IDS_MSG_APPRUNNING - IDS_MSG_FIRST], FALSE);
1741 CloseHandle(hMutex);
1746 memset(g_stServices, 0, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);
1748 InitCommonControls();
1749 g_hInstance = hInstance;
1750 g_hwndMain = CreateMainWindow(hInstance);
1751 g_bUiTaskbarCreated = RegisterWindowMessage("TaskbarCreated");
1752 InitializeCriticalSection(&g_stcSection);
1753 g_hwndServiceDlg = NULL;
1754 if (g_hwndMain != NULL)
1756 while (GetMessage(&msg, NULL, 0, 0) == TRUE)
1758 TranslateMessage(&msg);
1759 DispatchMessage(&msg);
1761 am_ClearServicesSt();
1763 am_ClearComputersSt();
1764 DeleteCriticalSection(&g_stcSection);
1765 CloseHandle(hMutex);
1766 DestroyIcon(g_icoStop);
1767 DestroyIcon(g_icoRun);
1768 DestroyCursor(g_hCursorHourglass);
1769 DestroyCursor(g_hCursorArrow);
1770 DeleteObject(g_hBmpStart);
1771 DeleteObject(g_hBmpStop);