1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2004 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
59 /* ====================================================================
60 * ApacheMonitor.c Simple program to manage and monitor Apache services.
62 * Contributed by Mladen Turk <mturk@mappingsoft.com>
65 * ====================================================================
68 #define _WIN32_WINNT 0x0400
83 #include "ApacheMonitor.h"
86 #define OS_VERSION_WIN9X 1
87 #define OS_VERSION_WINNT 2
88 #define OS_VERSION_WIN2K 3
89 /* Should be enough */
90 #define MAX_APACHE_SERVICES 128
91 #define MAX_APACHE_COMPUTERS 32
93 #define WM_TRAYMESSAGE (WM_APP+1)
94 #define WM_UPDATEMESSAGE (WM_USER+1)
95 #define WM_MANAGEMESSAGE (WM_USER+2)
96 #define WM_TIMER_REFRESH 10
97 #define WM_TIMER_RESCAN 11
98 #define SERVICE_APACHE_RESTART 128
101 #define MAX_LOADSTRING 100
102 #define REFRESH_TIME 2000 /* service refresh time (ms) */
103 #define RESCAN_TIME 20000 /* registry rescan time (ms) */
105 typedef struct _st_APACHE_SERVICE
111 LPSTR szComputerName;
115 typedef struct _st_MONITORED_COMPUTERS
117 LPSTR szComputerName;
121 /* Global variables */
122 HINSTANCE g_hInstance = NULL;
123 CHAR *g_szTitle; /* The title bar text */
124 CHAR *g_szWindowClass; /* Window Class Name */
127 UINT g_bUiTaskbarCreated;
129 BOOL g_bDlgServiceOn = FALSE;
130 BOOL g_bConsoleRun = FALSE;
131 ST_APACHE_SERVICE g_stServices[MAX_APACHE_SERVICES];
132 ST_MONITORED_COMP g_stComputers[MAX_APACHE_COMPUTERS];
134 HBITMAP g_hBmpStart, g_hBmpStop;
135 HBITMAP g_hBmpPicture, g_hBmpOld;
136 BOOL g_bRescanServices;
137 HWND g_hwndServiceDlg;
139 HWND g_hwndStdoutList;
140 HWND g_hwndConnectDlg;
141 HCURSOR g_hCursorHourglass;
142 HCURSOR g_hCursorArrow;
144 HANDLE g_hpipeOutRead;
145 HANDLE g_hpipeOutWrite;
146 HANDLE g_hpipeInRead;
147 HANDLE g_hpipeInWrite;
148 HANDLE g_hpipeStdError;
150 PROCESS_INFORMATION g_lpRedirectProc;
151 CRITICAL_SECTION g_stcSection;
154 /* locale language support */
155 static CHAR *g_lpMsg[IDS_MSG_LAST - IDS_MSG_FIRST + 1];
158 void am_ClearServicesSt()
161 for (i = 0; i < MAX_APACHE_SERVICES; i++)
163 if (g_stServices[i].szServiceName) {
164 free(g_stServices[i].szServiceName);
166 if (g_stServices[i].szDisplayName) {
167 free(g_stServices[i].szDisplayName);
169 if (g_stServices[i].szDescription) {
170 free(g_stServices[i].szDescription);
172 if (g_stServices[i].szImagePath) {
173 free(g_stServices[i].szImagePath);
175 if (g_stServices[i].szComputerName) {
176 free(g_stServices[i].szComputerName);
180 memset(g_stServices, 0, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);
185 void am_ClearComputersSt()
188 for (i = 0; i < MAX_APACHE_COMPUTERS; i++) {
189 if (g_stComputers[i].szComputerName) {
190 free(g_stComputers[i].szComputerName);
191 RegCloseKey(g_stComputers[i].hRegistry);
194 memset(g_stComputers, 0, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);
199 BOOL am_IsComputerConnected(LPSTR szComputerName)
202 while (g_stComputers[i].szComputerName != NULL) {
203 if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
212 void am_DisconnectComputer(LPSTR szComputerName)
215 while (g_stComputers[i].szComputerName != NULL) {
216 if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
221 if (g_stComputers[i].szComputerName != NULL) {
222 free(g_stComputers[i].szComputerName);
223 RegCloseKey(g_stComputers[i].hRegistry);
224 for (j = i; j < MAX_APACHE_COMPUTERS - 1; j++) {
225 g_stComputers[i].szComputerName= g_stComputers[i+1].szComputerName;
226 g_stComputers[i].hRegistry = g_stComputers[i+1].hRegistry;
228 for (i = j; i < MAX_APACHE_COMPUTERS; i++) {
229 g_stComputers[i].szComputerName = NULL;
230 g_stComputers[i].hRegistry = NULL;
237 void ErrorMessage(LPCSTR szError, BOOL bFatal)
239 LPVOID lpMsgBuf = NULL;
241 MessageBox(NULL, szError, g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST],
242 MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION));
245 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
246 FORMAT_MESSAGE_FROM_SYSTEM |
247 FORMAT_MESSAGE_IGNORE_INSERTS,
248 NULL, GetLastError(), g_LangID,
249 (LPSTR) &lpMsgBuf, 0, NULL);
250 MessageBox(NULL, (LPCSTR)lpMsgBuf,
251 g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST],
252 MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION));
261 BOOL am_ConnectComputer(LPSTR szComputerName)
265 char szTmp[MAX_PATH];
267 while (g_stComputers[i].szComputerName != NULL) {
268 if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
273 if (i > MAX_APACHE_COMPUTERS - 1) {
276 if (RegConnectRegistry(szComputerName, HKEY_LOCAL_MACHINE, &hKeyRemote)
278 sprintf(szTmp, g_lpMsg[IDS_MSG_ECONNECT - IDS_MSG_FIRST],
280 ErrorMessage(szTmp, FALSE);
284 g_stComputers[i].szComputerName = strdup(szComputerName);
285 g_stComputers[i].hRegistry = hKeyRemote;
291 LPSTR GetStringRes(int id)
293 static CHAR buffer[MAX_PATH];
296 LoadString(GetModuleHandle(NULL), id, buffer, MAX_PATH);
301 BOOL GetSystemOSVersion(LPDWORD dwVersion)
305 Try calling GetVersionEx using the OSVERSIONINFOEX structure.
306 If that fails, try using the OSVERSIONINFO structure.
308 memset(&osvi, 0, sizeof(OSVERSIONINFO));
309 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
311 if (!GetVersionEx(&osvi)) {
315 switch (osvi.dwPlatformId)
317 case VER_PLATFORM_WIN32_NT:
318 if (osvi.dwMajorVersion <= 4) {
319 *dwVersion = OS_VERSION_WINNT;
321 else if (osvi.dwMajorVersion == 5) {
322 *dwVersion = OS_VERSION_WIN2K;
329 case VER_PLATFORM_WIN32_WINDOWS:
330 *dwVersion = OS_VERSION_WIN9X;
333 case VER_PLATFORM_WIN32s:
342 static VOID ShowNotifyIcon(HWND hWnd, DWORD dwMessage)
347 memset(&nid, 0, sizeof(nid));
348 nid.cbSize = sizeof(NOTIFYICONDATA);
351 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
352 nid.uCallbackMessage = WM_TRAYMESSAGE;
354 while (g_stServices[i].szServiceName != NULL)
356 if (g_stServices[i].dwPid != 0) {
361 if (dwMessage != NIM_DELETE)
364 nid.hIcon = g_icoRun;
367 nid.hIcon = g_icoStop;
373 if (n == i && n > 0) {
374 lstrcpy(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGALL - IDS_MSG_FIRST]);
377 sprintf(nid.szTip, g_lpMsg[IDS_MSG_RUNNING - IDS_MSG_FIRST], n, i);
380 sprintf(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGNONE - IDS_MSG_FIRST], i);
383 lstrcpy(nid.szTip, g_lpMsg[IDS_MSG_NOSERVICES - IDS_MSG_FIRST]);
385 Shell_NotifyIcon(dwMessage, &nid);
389 void appendMenuItem(HMENU hMenu, UINT uMenuId, LPSTR szName,
390 BOOL fDefault, BOOL fEnabled)
394 memset(&mii, 0, sizeof(MENUITEMINFO));
395 mii.cbSize = sizeof(MENUITEMINFO);
396 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
399 mii.fType = MFT_STRING;
402 mii.fState = MFS_DEFAULT;
405 mii.fState |= MFS_DISABLED;
407 mii.dwTypeData = szName;
410 mii.fType = MFT_SEPARATOR;
412 InsertMenuItem(hMenu, uMenuId, FALSE, &mii);
416 void appendServiceMenu(HMENU hMenu, UINT uMenuId,
417 LPSTR szServiceName, BOOL fRunning)
422 smh = CreatePopupMenu();
424 appendMenuItem(smh, IDM_SM_START + uMenuId,
425 g_lpMsg[IDS_MSG_SSTART - IDS_MSG_FIRST], FALSE, !fRunning);
426 appendMenuItem(smh, IDM_SM_STOP + uMenuId,
427 g_lpMsg[IDS_MSG_SSTOP - IDS_MSG_FIRST], FALSE, fRunning);
428 appendMenuItem(smh, IDM_SM_RESTART + uMenuId,
429 g_lpMsg[IDS_MSG_SRESTART - IDS_MSG_FIRST], FALSE, fRunning);
431 memset(&mii, 0, sizeof(MENUITEMINFO));
432 mii.cbSize = sizeof(MENUITEMINFO);
433 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_SUBMENU
435 mii.fType = MFT_STRING;
437 mii.hbmpChecked = g_hBmpStart;
438 mii.hbmpUnchecked = g_hBmpStop;
439 mii.dwTypeData = szServiceName;
441 mii.fState = fRunning ? MFS_CHECKED : MFS_UNCHECKED;
442 InsertMenuItem(hMenu, IDM_SM_SERVICE + uMenuId, FALSE, &mii);
446 void ShowTryPopupMenu(HWND hWnd)
448 /* create popup menu */
449 HMENU hMenu = CreatePopupMenu();
454 appendMenuItem(hMenu, IDM_RESTORE,
455 g_lpMsg[IDS_MSG_MNUSHOW - IDS_MSG_FIRST],
457 if (g_dwOSVersion >= OS_VERSION_WINNT) {
458 appendMenuItem(hMenu, IDC_SMANAGER,
459 g_lpMsg[IDS_MSG_MNUSERVICES - IDS_MSG_FIRST],
462 appendMenuItem(hMenu, 0, "", FALSE, TRUE);
463 appendMenuItem(hMenu, IDM_EXIT,
464 g_lpMsg[IDS_MSG_MNUEXIT - IDS_MSG_FIRST],
467 if (!SetForegroundWindow(hWnd)) {
468 SetForegroundWindow(NULL);
471 TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
472 pt.x, pt.y, 0, hWnd, NULL);
478 void ShowTryServicesMenu(HWND hWnd)
480 /* create services list popup menu and submenus */
481 HMENU hMenu = CreatePopupMenu();
487 while (g_stServices[i].szServiceName != NULL)
489 appendServiceMenu(hMenu, i, g_stServices[i].szDisplayName,
490 g_stServices[i].dwPid != 0);
495 if (!SetForegroundWindow(hWnd)) {
496 SetForegroundWindow(NULL);
499 TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
500 pt.x, pt.y, 0, hWnd, NULL);
507 BOOL CenterWindow(HWND hwndChild)
509 RECT rChild, rWorkArea;
514 /* Get the Height and Width of the child window */
515 GetWindowRect(hwndChild, &rChild);
516 wChild = rChild.right - rChild.left;
517 hChild = rChild.bottom - rChild.top;
519 /* Get the limits of the 'workarea' */
520 bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT),
523 rWorkArea.left = rWorkArea.top = 0;
524 rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
525 rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
528 /* Calculate new X and Y position*/
529 xNew = (rWorkArea.right - wChild) / 2;
530 yNew = (rWorkArea.bottom - hChild) / 2;
531 return SetWindowPos(hwndChild, HWND_TOP, xNew, yNew, 0, 0,
532 SWP_NOSIZE | SWP_SHOWWINDOW);
536 static void addListBoxItem(HWND hDlg, LPSTR lpStr, HBITMAP hBmp)
540 nItem = SendMessage(hDlg, LB_ADDSTRING, 0, (LPARAM)lpStr);
541 SendMessage(hDlg, LB_SETITEMDATA, nItem, (LPARAM)hBmp);
545 static void addListBoxString(HWND hListBox, LPSTR lpStr)
547 static int nItems = 0;
548 if (!g_bDlgServiceOn) {
552 if (nItems > MAX_LOADSTRING)
554 SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
557 ListBox_SetCurSel(hListBox,
558 ListBox_AddString(hListBox, lpStr));
563 static DWORD WINAPI ConsoleOutputThread(LPVOID lpThreadParameter)
565 static BYTE lpBuffer[MAX_PATH+1];
570 while (ReadFile(g_hpipeOutRead, &ch, 1, &dwReaded, NULL) == TRUE)
574 if (ch == '\n' || nPtr >= MAX_PATH)
576 lpBuffer[nPtr] = '\0';
577 addListBoxString(g_hwndStdoutList, lpBuffer);
580 else if (ch == '\t' && nPtr < (MAX_PATH - 4))
583 for (i = 0; i < 4; ++i) {
584 lpBuffer[nPtr++] = ' ';
587 else if (ch != '\r') {
588 lpBuffer[nPtr++] = ch;
592 CloseHandle(g_hpipeInWrite);
593 CloseHandle(g_hpipeOutRead);
594 CloseHandle(g_hpipeStdError);
599 DWORD WINAPI ConsoleWaitingThread(LPVOID lpThreadParameter)
601 WaitForSingleObject(g_lpRedirectProc.hThread, INFINITE);
602 CloseHandle(g_lpRedirectProc.hThread);
604 g_bConsoleRun = FALSE;
605 SetCursor(g_hCursorArrow);
610 BOOL RunRedirectedConsole(LPSTR szCmdLine)
617 memset(&stInfo, 0, sizeof(stInfo));
618 stInfo.cb = sizeof(stInfo);
619 stInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
620 stInfo.wShowWindow = SW_HIDE;
622 hProc = GetCurrentProcess();
624 if (!CreatePipe(&g_hpipeInRead, &g_hpipeInWrite, NULL, MAX_PATH)) {
625 ErrorMessage(NULL, TRUE);
627 if (!CreatePipe(&g_hpipeOutRead, &g_hpipeOutWrite, NULL, MAX_PATH*8)) {
628 ErrorMessage(NULL, TRUE);
630 DuplicateHandle(hProc, g_hpipeInRead, hProc, &g_hpipeInRead, 0, TRUE,
631 DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
632 DuplicateHandle(hProc, g_hpipeOutWrite, hProc, &g_hpipeOutWrite, 0, TRUE,
633 DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
634 DuplicateHandle(hProc, g_hpipeOutWrite, hProc, &g_hpipeStdError, 0, TRUE,
635 DUPLICATE_SAME_ACCESS);
636 if (!g_hpipeInRead && !g_hpipeOutWrite && !g_hpipeStdError) {
637 ErrorMessage(NULL, TRUE);
639 stInfo.hStdInput = g_hpipeInRead;
640 stInfo.hStdOutput = g_hpipeOutWrite;
641 stInfo.hStdError = g_hpipeStdError;
643 bResult = CreateProcess(NULL,
655 CloseHandle(g_hpipeInRead);
656 CloseHandle(g_hpipeOutWrite);
657 CloseHandle(g_hpipeStdError);
661 CloseHandle(g_hpipeInWrite);
662 CloseHandle(g_hpipeOutRead);
663 CloseHandle(g_hpipeStdError);
667 CloseHandle(CreateThread(NULL, 0, ConsoleOutputThread,
669 ResumeThread(g_lpRedirectProc.hThread);
670 CloseHandle(CreateThread(NULL, 0, ConsoleWaitingThread,
677 BOOL RunAndForgetConsole(LPSTR szCmdLine, BOOL bRedirectConsole)
680 PROCESS_INFORMATION prInfo;
683 if (bRedirectConsole) {
684 return RunRedirectedConsole(szCmdLine);
687 memset(&stInfo, 0, sizeof(stInfo));
688 stInfo.cb = sizeof(stInfo);
689 stInfo.dwFlags = STARTF_USESHOWWINDOW;
690 stInfo.wShowWindow = SW_HIDE;
692 bResult = CreateProcess(NULL,
706 if (g_dwOSVersion == OS_VERSION_WIN9X) {
707 /* give some time to rescan the status */
710 CloseHandle(prInfo.hThread);
711 CloseHandle(prInfo.hProcess);
716 BOOL ApacheManageService(LPCSTR szServiceName, LPCSTR szImagePath,
717 LPSTR szComputerName, DWORD dwCommand)
719 CHAR szBuf[MAX_PATH];
720 CHAR szMsg[MAX_PATH];
723 BOOL serviceFlag = TRUE;
724 SC_HANDLE schService;
725 SC_HANDLE schSCManager;
726 SERVICE_STATUS schSStatus;
729 if (g_dwOSVersion == OS_VERSION_WIN9X)
731 sPos = strstr(szImagePath, "-k start");
734 lstrcpyn(szBuf, szImagePath, sPos - szImagePath);
737 case SERVICE_CONTROL_STOP:
738 lstrcat(szBuf, " -k shutdown -n ");
741 case SERVICE_CONTROL_CONTINUE:
742 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTART - IDS_MSG_FIRST],
744 addListBoxString(g_hwndStdoutList, szMsg);
745 lstrcat(szBuf, " -k start -n ");
749 case SERVICE_APACHE_RESTART:
750 lstrcat(szBuf, " -k restart -n ");
756 lstrcat(szBuf, szServiceName);
761 g_bConsoleRun = TRUE;
762 SetCursor(g_hCursorHourglass);
763 if (!RunAndForgetConsole(szBuf, serviceFlag))
765 ErrorMessage(NULL, FALSE);
766 g_bConsoleRun = FALSE;
767 SetCursor(g_hCursorArrow);
770 else if (!serviceFlag)
772 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTARTED - IDS_MSG_FIRST],
774 addListBoxString(g_hwndStdoutList, szMsg);
775 g_bConsoleRun = FALSE;
776 SetCursor(g_hCursorArrow);
782 schSCManager = OpenSCManager(szComputerName, NULL,
788 schService = OpenService(schSCManager, szServiceName,
789 SERVICE_QUERY_STATUS | SERVICE_START |
790 SERVICE_STOP | SERVICE_USER_DEFINED_CONTROL);
791 if (schService != NULL)
794 g_bConsoleRun = TRUE;
795 SetCursor(g_hCursorHourglass);
798 case SERVICE_CONTROL_STOP:
799 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTOP - IDS_MSG_FIRST],
801 addListBoxString(g_hwndStdoutList, szMsg);
802 if (ControlService(schService, SERVICE_CONTROL_STOP,
805 while (QueryServiceStatus(schService, &schSStatus))
807 if (schSStatus.dwCurrentState == SERVICE_STOP_PENDING)
816 if (QueryServiceStatus(schService, &schSStatus))
818 if (schSStatus.dwCurrentState == SERVICE_STOPPED)
822 g_lpMsg[IDS_MSG_SRVSTOPPED - IDS_MSG_FIRST],
824 addListBoxString(g_hwndStdoutList, szMsg);
829 case SERVICE_CONTROL_CONTINUE:
830 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTART - IDS_MSG_FIRST],
832 addListBoxString(g_hwndStdoutList, szMsg);
834 if (StartService(schService, 0, NULL))
837 while (QueryServiceStatus(schService, &schSStatus))
839 if (schSStatus.dwCurrentState == SERVICE_START_PENDING)
848 if (QueryServiceStatus(schService, &schSStatus))
850 if (schSStatus.dwCurrentState == SERVICE_RUNNING)
854 g_lpMsg[IDS_MSG_SRVSTARTED - IDS_MSG_FIRST],
856 addListBoxString(g_hwndStdoutList, szMsg);
861 case SERVICE_APACHE_RESTART:
862 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVRESTART - IDS_MSG_FIRST],
864 addListBoxString(g_hwndStdoutList, szMsg);
865 if (ControlService(schService, SERVICE_APACHE_RESTART,
869 while (schSStatus.dwCurrentState == SERVICE_START_PENDING)
872 if (!QueryServiceStatus(schService, &schSStatus))
874 CloseServiceHandle(schService);
875 CloseServiceHandle(schSCManager);
876 g_bConsoleRun = FALSE;
877 SetCursor(g_hCursorArrow);
885 if (schSStatus.dwCurrentState == SERVICE_RUNNING)
889 g_lpMsg[IDS_MSG_SRVRESTARTED - IDS_MSG_FIRST],
891 addListBoxString(g_hwndStdoutList, szMsg);
895 CloseServiceHandle(schService);
896 CloseServiceHandle(schSCManager);
898 ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED - IDS_MSG_FIRST],
901 g_bConsoleRun = FALSE;
902 SetCursor(g_hCursorArrow);
906 g_bRescanServices = TRUE;
908 CloseServiceHandle(schSCManager);
916 BOOL IsServiceRunning(LPCSTR szServiceName, LPCSTR szComputerName,
921 SC_HANDLE schService;
922 SC_HANDLE schSCManager;
923 SERVICE_STATUS schSStatus;
925 if (g_dwOSVersion == OS_VERSION_WIN9X)
927 hWnd = FindWindow("ApacheWin95ServiceMonitor", szServiceName);
928 if (hWnd && GetWindowThreadProcessId(hWnd, &dwPid))
940 schSCManager = OpenSCManager(szComputerName, NULL,
946 schService = OpenService(schSCManager, szServiceName,
947 SERVICE_QUERY_STATUS);
948 if (schService != NULL)
950 if (QueryServiceStatus(schService, &schSStatus))
952 dwPid = schSStatus.dwCurrentState;
957 CloseServiceHandle(schService);
958 CloseServiceHandle(schSCManager);
959 return dwPid == SERVICE_RUNNING ? TRUE : FALSE;
962 g_bRescanServices = TRUE;
964 CloseServiceHandle(schSCManager);
973 BOOL FindRunningServices(void)
978 while (g_stServices[i].szServiceName != NULL)
980 if (!IsServiceRunning(g_stServices[i].szServiceName,
981 g_stServices[i].szComputerName, &dwPid)) {
984 if (g_stServices[i].dwPid != dwPid) {
987 g_stServices[i].dwPid = dwPid;
994 BOOL GetApacheServicesStatus()
996 CHAR szKey[MAX_PATH];
997 CHAR achKey[MAX_PATH];
998 CHAR szImagePath[MAX_PATH];
999 CHAR szBuf[MAX_PATH];
1000 CHAR szTmp[MAX_PATH];
1001 HKEY hKey, hSubKey, hKeyRemote;
1002 DWORD retCode, rv, dwKeyType;
1003 DWORD dwBufLen = MAX_PATH;
1007 g_bRescanServices = FALSE;
1009 am_ClearServicesSt();
1010 while (g_stComputers[computers].szComputerName != NULL) {
1011 hKeyRemote = g_stComputers[computers].hRegistry;
1012 retCode = RegOpenKeyEx(hKeyRemote,
1013 "System\\CurrentControlSet\\Services\\",
1014 0, KEY_READ, &hKey);
1015 if (retCode != ERROR_SUCCESS)
1017 ErrorMessage(NULL, FALSE);
1020 for (i = 0, retCode = ERROR_SUCCESS; retCode == ERROR_SUCCESS; i++)
1022 retCode = RegEnumKey(hKey, i, achKey, MAX_PATH);
1023 if (retCode == ERROR_SUCCESS)
1025 lstrcpy(szKey, "System\\CurrentControlSet\\Services\\");
1026 lstrcat(szKey, achKey);
1028 if (RegOpenKeyEx(hKeyRemote, szKey, 0,
1029 KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS)
1031 dwBufLen = MAX_PATH;
1032 rv = RegQueryValueEx(hSubKey, "ImagePath", NULL,
1033 &dwKeyType, szImagePath, &dwBufLen);
1035 if (rv == ERROR_SUCCESS
1036 && (dwKeyType == REG_SZ
1037 || dwKeyType == REG_EXPAND_SZ)
1040 lstrcpy(szBuf, szImagePath);
1042 /* the service name could be Apache*.exe */
1043 if ((strstr(szBuf, "\\apache") != NULL)
1044 && strstr(szBuf, ".exe")
1045 && (strstr(szBuf, "--ntservice") != NULL
1046 || strstr(szBuf, "-k ") != NULL))
1048 g_stServices[stPos].szServiceName = strdup(achKey);
1049 g_stServices[stPos].szImagePath =
1050 strdup(szImagePath);
1051 g_stServices[stPos].szComputerName =
1052 strdup(g_stComputers[computers].szComputerName);
1053 dwBufLen = MAX_PATH;
1054 if (RegQueryValueEx(hSubKey, "Description", NULL,
1055 &dwKeyType, szBuf, &dwBufLen)
1057 g_stServices[stPos].szDescription =
1060 dwBufLen = MAX_PATH;
1061 if (RegQueryValueEx(hSubKey, "DisplayName", NULL,
1062 &dwKeyType, szBuf, &dwBufLen)
1065 if (strcmp(g_stComputers[computers]
1066 .szComputerName, g_szLocalHost) != 0)
1068 strcpy(szTmp, g_stComputers[computers]
1069 .szComputerName + 2);
1071 strcat(szTmp, szBuf);
1074 strcpy(szTmp, szBuf);
1076 g_stServices[stPos].szDisplayName
1081 if (stPos >= MAX_APACHE_SERVICES) {
1082 retCode = !ERROR_SUCCESS;
1086 RegCloseKey(hSubKey);
1093 FindRunningServices();
1098 LRESULT CALLBACK ConnectDlgProc(HWND hDlg, UINT message,
1099 WPARAM wParam, LPARAM lParam)
1101 CHAR szCmp[MAX_COMPUTERNAME_LENGTH+4];
1105 ShowWindow(hDlg, SW_HIDE);
1106 g_hwndConnectDlg = hDlg;
1108 ShowWindow(hDlg, SW_SHOW);
1109 SetFocus(GetDlgItem(hDlg, IDC_COMPUTER));
1113 switch (LOWORD(wParam))
1116 memset(szCmp, 0, MAX_COMPUTERNAME_LENGTH+4);
1117 strcpy(szCmp, "\\\\");
1118 SendMessage(GetDlgItem(hDlg, IDC_COMPUTER), WM_GETTEXT,
1119 (WPARAM) MAX_COMPUTERNAME_LENGTH,
1123 if (strlen(szCmp) < 3) {
1124 EndDialog(hDlg, TRUE);
1127 am_ConnectComputer(szCmp);
1128 SendMessage(g_hwndMain, WM_TIMER, WM_TIMER_RESCAN, 0);
1131 EndDialog(hDlg, TRUE);
1139 memset(&bi, 0, sizeof(BROWSEINFO));
1140 SHGetSpecialFolderLocation(hDlg, CSIDL_NETWORK, &il);
1142 bi.lpszTitle = "ApacheMonitor :\nSelect Network Computer!";
1143 bi.pszDisplayName = szCmp;
1144 bi.hwndOwner = hDlg;
1145 bi.ulFlags = BIF_BROWSEFORCOMPUTER;
1151 if (SHBrowseForFolder(&bi) != NULL) {
1152 SendMessage(GetDlgItem(hDlg, IDC_COMPUTER),
1154 (WPARAM) NULL, (LPARAM) szCmp);
1156 if (SHGetMalloc(&pMalloc)) {
1157 pMalloc->lpVtbl->Free(pMalloc, il);
1158 pMalloc->lpVtbl->Release(pMalloc);
1167 EndDialog(hDlg, TRUE);
1178 LRESULT CALLBACK ServiceDlgProc(HWND hDlg, UINT message,
1179 WPARAM wParam, LPARAM lParam)
1181 CHAR szBuf[MAX_PATH];
1183 static HWND hStatusBar;
1189 LPMEASUREITEMSTRUCT lpmis;
1190 LPDRAWITEMSTRUCT lpdis;
1192 memset(szBuf, 0, MAX_PATH);
1196 ShowWindow(hDlg, SW_HIDE);
1197 g_hwndServiceDlg = hDlg;
1198 SetWindowText(hDlg, g_szTitle);
1199 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1200 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1201 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1202 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1203 SetWindowText(GetDlgItem(hDlg, IDC_SSTART),
1204 g_lpMsg[IDS_MSG_SSTART - IDS_MSG_FIRST]);
1205 SetWindowText(GetDlgItem(hDlg, IDC_SSTOP),
1206 g_lpMsg[IDS_MSG_SSTOP - IDS_MSG_FIRST]);
1207 SetWindowText(GetDlgItem(hDlg, IDC_SRESTART),
1208 g_lpMsg[IDS_MSG_SRESTART - IDS_MSG_FIRST]);
1209 SetWindowText(GetDlgItem(hDlg, IDC_SMANAGER),
1210 g_lpMsg[IDS_MSG_SERVICES - IDS_MSG_FIRST]);
1211 SetWindowText(GetDlgItem(hDlg, IDC_SCONNECT),
1212 g_lpMsg[IDS_MSG_CONNECT - IDS_MSG_FIRST]);
1213 SetWindowText(GetDlgItem(hDlg, IDC_SEXIT),
1214 g_lpMsg[IDS_MSG_MNUEXIT - IDS_MSG_FIRST]);
1215 if (g_dwOSVersion < OS_VERSION_WINNT)
1217 ShowWindow(GetDlgItem(hDlg, IDC_SMANAGER), SW_HIDE);
1218 ShowWindow(GetDlgItem(hDlg, IDC_SCONNECT), SW_HIDE);
1219 ShowWindow(GetDlgItem(hDlg, IDC_SDISCONN), SW_HIDE);
1221 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1222 g_hwndStdoutList = GetDlgItem(hDlg, IDL_STDOUT);
1223 hStatusBar = CreateStatusWindow(0x0800 /* SBT_TOOLTIPS */
1224 | WS_CHILD | WS_VISIBLE,
1225 "", hDlg, IDC_STATBAR);
1226 if (GetApacheServicesStatus())
1229 while (g_stServices[i].szServiceName != NULL)
1231 addListBoxItem(hListBox, g_stServices[i].szDisplayName,
1232 g_stServices[i].dwPid == 0 ? g_hBmpStop
1238 ShowWindow(hDlg, SW_SHOW);
1240 SendMessage(hListBox, LB_SETCURSEL, 0, 0);
1244 case WM_MANAGEMESSAGE:
1245 ApacheManageService(g_stServices[LOWORD(wParam)].szServiceName,
1246 g_stServices[LOWORD(wParam)].szImagePath,
1247 g_stServices[LOWORD(wParam)].szComputerName,
1253 case WM_UPDATEMESSAGE:
1254 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1255 SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
1256 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)"");
1257 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1258 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1259 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1260 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1262 while (g_stServices[i].szServiceName != NULL)
1264 addListBoxItem(hListBox, g_stServices[i].szDisplayName,
1265 g_stServices[i].dwPid == 0 ? g_hBmpStop : g_hBmpStart);
1268 SendMessage(hListBox, LB_SETCURSEL, 0, 0);
1269 /* Dirty hack to bring the window to the foreground */
1270 SetWindowPos(hDlg, HWND_TOPMOST, 0, 0, 0, 0,
1271 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1272 SetWindowPos(hDlg, HWND_NOTOPMOST, 0, 0, 0, 0,
1273 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1278 case WM_MEASUREITEM:
1279 lpmis = (LPMEASUREITEMSTRUCT) lParam;
1280 lpmis->itemHeight = YBITMAP;
1284 if (g_bConsoleRun) {
1285 SetCursor(g_hCursorHourglass);
1288 SetCursor(g_hCursorArrow);
1293 lpdis = (LPDRAWITEMSTRUCT) lParam;
1294 if (lpdis->itemID == -1) {
1297 switch (lpdis->itemAction)
1300 case ODA_DRAWENTIRE:
1301 g_hBmpPicture = (HBITMAP)SendMessage(lpdis->hwndItem,
1303 lpdis->itemID, (LPARAM) 0);
1305 hdcMem = CreateCompatibleDC(lpdis->hDC);
1306 g_hBmpOld = SelectObject(hdcMem, g_hBmpPicture);
1308 BitBlt(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
1309 lpdis->rcItem.right - lpdis->rcItem.left,
1310 lpdis->rcItem.bottom - lpdis->rcItem.top,
1311 hdcMem, 0, 0, SRCCOPY);
1312 SendMessage(lpdis->hwndItem, LB_GETTEXT,
1313 lpdis->itemID, (LPARAM) szBuf);
1315 GetTextMetrics(lpdis->hDC, &tm);
1316 y = (lpdis->rcItem.bottom + lpdis->rcItem.top - tm.tmHeight) / 2;
1318 SelectObject(hdcMem, g_hBmpOld);
1321 rcBitmap.left = lpdis->rcItem.left + XBITMAP + 2;
1322 rcBitmap.top = lpdis->rcItem.top;
1323 rcBitmap.right = lpdis->rcItem.right;
1324 rcBitmap.bottom = lpdis->rcItem.top + YBITMAP;
1326 if (lpdis->itemState & ODS_SELECTED)
1328 if (g_hBmpPicture == g_hBmpStop)
1330 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), TRUE);
1331 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1332 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1334 else if (g_hBmpPicture == g_hBmpStart)
1336 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1337 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), TRUE);
1338 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), TRUE);
1341 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1342 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1343 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1345 if (strcmp(g_stServices[lpdis->itemID].szComputerName,
1346 g_szLocalHost) == 0) {
1347 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1350 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), TRUE);
1353 if (g_stServices[lpdis->itemID].szDescription) {
1354 SendMessage(hStatusBar, SB_SETTEXT, 0,
1355 (LPARAM)g_stServices[lpdis->itemID].szDescription);
1358 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)"");
1360 SetTextColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
1361 SetBkColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHT));
1362 FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_HIGHLIGHTTEXT));
1366 SetTextColor(lpdis->hDC, GetSysColor(COLOR_MENUTEXT));
1367 SetBkColor(lpdis->hDC, GetSysColor(COLOR_WINDOW));
1368 FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_WINDOW+1));
1370 TextOut(lpdis->hDC, XBITMAP + 6, y, szBuf, strlen(szBuf));
1378 switch (LOWORD(wParam))
1381 switch (HIWORD(wParam))
1384 /* if started then stop, if stopped then start */
1385 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1386 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1387 if (nItem != LB_ERR)
1389 g_hBmpPicture = (HBITMAP)SendMessage(hListBox,
1392 if (g_hBmpPicture == g_hBmpStop) {
1393 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1394 SERVICE_CONTROL_CONTINUE);
1397 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1398 SERVICE_CONTROL_STOP);
1407 EndDialog(hDlg, TRUE);
1411 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1412 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1413 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1414 if (nItem != LB_ERR) {
1415 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1416 SERVICE_CONTROL_CONTINUE);
1418 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), TRUE);
1422 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1423 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1424 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1425 if (nItem != LB_ERR) {
1426 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1427 SERVICE_CONTROL_STOP);
1429 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), TRUE);
1433 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1434 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1435 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1436 if (nItem != LB_ERR) {
1437 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1438 SERVICE_APACHE_RESTART);
1440 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), TRUE);
1444 if (g_dwOSVersion >= OS_VERSION_WIN2K) {
1445 ShellExecute(hDlg, "open", "services.msc", "/s",
1449 WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL);
1454 EndDialog(hDlg, TRUE);
1455 SendMessage(g_hwndMain, WM_COMMAND, (WPARAM)IDM_EXIT, 0);
1459 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGCONNECT),
1460 hDlg, (DLGPROC)ConnectDlgProc);
1464 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1465 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1466 if (nItem != LB_ERR) {
1467 am_DisconnectComputer(g_stServices[nItem].szComputerName);
1468 SendMessage(g_hwndMain, WM_TIMER, WM_TIMER_RESCAN, 0);
1475 switch (LOWORD(wParam))
1477 case SIZE_MINIMIZED:
1478 EndDialog(hDlg, TRUE);
1486 EndDialog(hDlg, TRUE);
1496 LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
1497 WPARAM wParam, LPARAM lParam)
1499 if (message == g_bUiTaskbarCreated)
1501 /* restore the tray icon on shell restart */
1502 ShowNotifyIcon(hWnd, NIM_ADD);
1503 return DefWindowProc(hWnd, message, wParam, lParam);
1508 GetApacheServicesStatus();
1509 ShowNotifyIcon(hWnd, NIM_ADD);
1510 SetTimer(hWnd, WM_TIMER_REFRESH, REFRESH_TIME, NULL);
1511 SetTimer(hWnd, WM_TIMER_RESCAN, RESCAN_TIME, NULL);
1517 case WM_TIMER_RESCAN:
1519 int nPrev = 0, nNew = 0;
1520 EnterCriticalSection(&g_stcSection);
1521 if (FindRunningServices() || g_bRescanServices)
1523 ShowNotifyIcon(hWnd, NIM_MODIFY);
1524 if (g_hwndServiceDlg)
1525 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1527 /* check if services list changed */
1528 while (g_stServices[nPrev].szServiceName != NULL)
1530 GetApacheServicesStatus();
1531 while (g_stServices[nNew].szServiceName != NULL)
1535 ShowNotifyIcon(hWnd, NIM_MODIFY);
1536 if (g_hwndServiceDlg) {
1537 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1540 LeaveCriticalSection(&g_stcSection);
1544 case WM_TIMER_REFRESH:
1546 int nPrev = 0, nNew = 0;
1547 EnterCriticalSection(&g_stcSection);
1548 if (g_bRescanServices)
1550 GetApacheServicesStatus();
1551 ShowNotifyIcon(hWnd, NIM_MODIFY);
1552 if (g_hwndServiceDlg) {
1553 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1556 else if (FindRunningServices())
1558 ShowNotifyIcon(hWnd, NIM_MODIFY);
1559 if (g_hwndServiceDlg) {
1560 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1563 LeaveCriticalSection(&g_stcSection);
1570 ShowNotifyIcon(hWnd, NIM_DELETE);
1573 case WM_TRAYMESSAGE:
1576 case WM_LBUTTONDBLCLK:
1577 if (!g_bDlgServiceOn)
1579 g_bDlgServiceOn = TRUE;
1580 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES),
1581 hWnd, (DLGPROC)ServiceDlgProc);
1582 g_bDlgServiceOn = FALSE;
1583 g_hwndServiceDlg = NULL;
1585 else if (IsWindow(g_hwndServiceDlg))
1587 /* Dirty hack to bring the window to the foreground */
1588 SetWindowPos(g_hwndServiceDlg, HWND_TOPMOST, 0, 0, 0, 0,
1589 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1590 SetWindowPos(g_hwndServiceDlg, HWND_NOTOPMOST, 0, 0, 0, 0,
1591 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1592 SetFocus(g_hwndServiceDlg);
1597 ShowTryServicesMenu(hWnd);
1601 ShowTryPopupMenu(hWnd);
1607 if ((LOWORD(wParam) & IDM_SM_START) == IDM_SM_START)
1609 ApacheManageService(g_stServices[LOWORD(wParam)
1610 - IDM_SM_START].szServiceName,
1611 g_stServices[LOWORD(wParam)
1612 - IDM_SM_START].szImagePath,
1613 g_stServices[LOWORD(wParam)
1614 - IDM_SM_START].szComputerName,
1615 SERVICE_CONTROL_CONTINUE);
1618 else if ((LOWORD(wParam) & IDM_SM_STOP) == IDM_SM_STOP)
1620 ApacheManageService(g_stServices[LOWORD(wParam)
1621 - IDM_SM_STOP].szServiceName,
1622 g_stServices[LOWORD(wParam)
1623 - IDM_SM_STOP].szImagePath,
1624 g_stServices[LOWORD(wParam)
1625 - IDM_SM_STOP].szComputerName,
1626 SERVICE_CONTROL_STOP);
1629 else if ((LOWORD(wParam) & IDM_SM_RESTART) == IDM_SM_RESTART)
1631 ApacheManageService(g_stServices[LOWORD(wParam)
1632 - IDM_SM_RESTART].szServiceName,
1633 g_stServices[LOWORD(wParam)
1634 - IDM_SM_RESTART].szImagePath,
1635 g_stServices[LOWORD(wParam)
1636 - IDM_SM_RESTART].szComputerName,
1637 SERVICE_APACHE_RESTART);
1640 switch (LOWORD(wParam))
1643 if (!g_bDlgServiceOn)
1645 g_bDlgServiceOn = TRUE;
1646 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES),
1647 hWnd, (DLGPROC)ServiceDlgProc);
1648 g_bDlgServiceOn = FALSE;
1649 g_hwndServiceDlg = NULL;
1651 else if (IsWindow(g_hwndServiceDlg)) {
1652 SetFocus(g_hwndServiceDlg);
1657 if (g_dwOSVersion >= OS_VERSION_WIN2K) {
1658 ShellExecute(NULL, "open", "services.msc", "/s",
1662 WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL);
1667 ShowNotifyIcon(hWnd, NIM_DELETE);
1673 return DefWindowProc(hWnd, message, wParam, lParam);
1680 /* Create main invisible window */
1681 HWND CreateMainWindow(HINSTANCE hInstance)
1686 if (!GetSystemOSVersion(&g_dwOSVersion))
1688 ErrorMessage(NULL, TRUE);
1692 wcex.cbSize = sizeof(WNDCLASSEX);
1694 wcex.style = CS_HREDRAW | CS_VREDRAW;
1695 wcex.lpfnWndProc = (WNDPROC)WndProc;
1696 wcex.cbClsExtra = 0;
1697 wcex.cbWndExtra = 0;
1698 wcex.hInstance = hInstance;
1699 wcex.hIcon = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON),
1700 IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
1701 wcex.hCursor = g_hCursorArrow;
1702 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
1703 wcex.lpszMenuName = 0;
1704 wcex.lpszClassName = g_szWindowClass;
1705 wcex.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON),
1706 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1708 if (RegisterClassEx(&wcex)) {
1709 hWnd = CreateWindow(g_szWindowClass, g_szTitle,
1711 NULL, NULL, hInstance, NULL);
1718 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1719 LPSTR lpCmdLine, int nCmdShow)
1721 CHAR szTmp[MAX_LOADSTRING];
1722 CHAR szCmp[MAX_COMPUTERNAME_LENGTH+4];
1724 /* single instance mutex */
1729 g_LangID = GetUserDefaultLangID();
1730 if ((g_LangID & 0xFF) != LANG_ENGLISH) {
1731 g_LangID = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
1733 for (i = IDS_MSG_FIRST; i <= IDS_MSG_LAST; ++i) {
1734 LoadString(hInstance, i, szTmp, MAX_LOADSTRING);
1735 g_lpMsg[i - IDS_MSG_FIRST] = strdup(szTmp);
1737 LoadString(hInstance, IDS_APMONITORTITLE, szTmp, MAX_LOADSTRING);
1738 d = MAX_COMPUTERNAME_LENGTH+1;
1739 strcpy(szCmp, "\\\\");
1740 GetComputerName(szCmp + 2, &d);
1742 g_szLocalHost = strdup(szCmp);
1744 memset(g_stComputers, 0, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);
1745 g_stComputers[0].szComputerName = strdup(szCmp);
1746 g_stComputers[0].hRegistry = HKEY_LOCAL_MACHINE;
1747 g_szTitle = strdup(szTmp);
1748 LoadString(hInstance, IDS_APMONITORCLASS, szTmp, MAX_LOADSTRING);
1749 g_szWindowClass = strdup(szTmp);
1751 g_icoStop = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICOSTOP),
1752 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1753 g_icoRun = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICORUN),
1754 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1755 g_hCursorHourglass = LoadImage(NULL, MAKEINTRESOURCE(OCR_WAIT),
1756 IMAGE_CURSOR, LR_DEFAULTSIZE,
1757 LR_DEFAULTSIZE, LR_SHARED);
1758 g_hCursorArrow = LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL),
1759 IMAGE_CURSOR, LR_DEFAULTSIZE,
1760 LR_DEFAULTSIZE, LR_SHARED);
1761 g_hBmpStart = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPRUN),
1762 IMAGE_BITMAP, XBITMAP, YBITMAP,
1764 g_hBmpStop = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPSTOP),
1765 IMAGE_BITMAP, XBITMAP, YBITMAP,
1768 hMutex = CreateMutex(NULL, FALSE, "APSRVMON_MUTEX");
1769 if ((hMutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS))
1771 ErrorMessage(g_lpMsg[IDS_MSG_APPRUNNING - IDS_MSG_FIRST], FALSE);
1773 CloseHandle(hMutex);
1778 memset(g_stServices, 0, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);
1780 InitCommonControls();
1781 g_hInstance = hInstance;
1782 g_hwndMain = CreateMainWindow(hInstance);
1783 g_bUiTaskbarCreated = RegisterWindowMessage("TaskbarCreated");
1784 InitializeCriticalSection(&g_stcSection);
1785 g_hwndServiceDlg = NULL;
1786 if (g_hwndMain != NULL)
1788 while (GetMessage(&msg, NULL, 0, 0) == TRUE)
1790 TranslateMessage(&msg);
1791 DispatchMessage(&msg);
1793 am_ClearServicesSt();
1795 am_ClearComputersSt();
1796 DeleteCriticalSection(&g_stcSection);
1797 CloseHandle(hMutex);
1798 DestroyIcon(g_icoStop);
1799 DestroyIcon(g_icoRun);
1800 DestroyCursor(g_hCursorHourglass);
1801 DestroyCursor(g_hCursorArrow);
1802 DeleteObject(g_hBmpStart);
1803 DeleteObject(g_hBmpStop);