1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000-2002 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];
157 void am_ClearServicesSt()
160 for (i = 0; i < MAX_APACHE_SERVICES; i++)
162 if (g_stServices[i].szServiceName)
163 free(g_stServices[i].szServiceName);
164 if (g_stServices[i].szDisplayName)
165 free(g_stServices[i].szDisplayName);
166 if (g_stServices[i].szDescription)
167 free(g_stServices[i].szDescription);
168 if (g_stServices[i].szImagePath)
169 free(g_stServices[i].szImagePath);
170 if (g_stServices[i].szComputerName)
171 free(g_stServices[i].szComputerName);
174 ZeroMemory(g_stServices, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);
178 void am_ClearComputersSt()
181 for (i = 0; i < MAX_APACHE_COMPUTERS; i++)
183 if (g_stComputers[i].szComputerName) {
184 free(g_stComputers[i].szComputerName);
185 RegCloseKey(g_stComputers[i].hRegistry);
188 ZeroMemory(g_stComputers, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);
192 BOOL am_IsComputerConnected( LPSTR szComputerName)
195 while (g_stComputers[i].szComputerName != NULL) {
196 if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0)
203 void am_DisconnectComputer( LPSTR szComputerName)
206 while (g_stComputers[i].szComputerName != NULL) {
207 if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0)
211 if (g_stComputers[i].szComputerName != NULL) {
212 free(g_stComputers[i].szComputerName);
213 RegCloseKey(g_stComputers[i].hRegistry);
214 for( j = i; j < MAX_APACHE_COMPUTERS - 1; j++) {
215 g_stComputers[i].szComputerName = g_stComputers[i+1].szComputerName;
216 g_stComputers[i].hRegistry = g_stComputers[i+1].hRegistry;
218 for( i = j; i < MAX_APACHE_COMPUTERS; i++) {
219 g_stComputers[i].szComputerName = NULL;
220 g_stComputers[i].hRegistry = NULL;
226 void ErrorMessage(LPCSTR szError, BOOL bFatal)
228 LPVOID lpMsgBuf = NULL;
230 MessageBox(NULL, szError, g_lpMsg[IDS_MSG_ERROR-IDS_MSG_FIRST],
231 MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION));
234 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
235 FORMAT_MESSAGE_FROM_SYSTEM |
236 FORMAT_MESSAGE_IGNORE_INSERTS,
240 (LPSTR) &lpMsgBuf, 0, NULL);
241 MessageBox(NULL, (LPCSTR)lpMsgBuf, g_lpMsg[IDS_MSG_ERROR-IDS_MSG_FIRST],
242 MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION));
249 BOOL am_ConnectComputer(LPSTR szComputerName)
253 char szTmp[MAX_PATH];
255 while (g_stComputers[i].szComputerName != NULL) {
256 if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0)
260 if (i > MAX_APACHE_COMPUTERS - 1)
262 if (RegConnectRegistry(szComputerName, HKEY_LOCAL_MACHINE, &hKeyRemote) != ERROR_SUCCESS)
264 sprintf(szTmp, g_lpMsg[IDS_MSG_ECONNECT-IDS_MSG_FIRST], szComputerName);
265 ErrorMessage(szTmp, FALSE);
270 g_stComputers[i].szComputerName = strdup(szComputerName);
271 g_stComputers[i].hRegistry = hKeyRemote;
276 LPSTR GetStringRes(int id)
278 static CHAR buffer[MAX_PATH];
281 LoadString(GetModuleHandle (NULL), id, buffer, MAX_PATH);
285 BOOL GetSystemOSVersion(LPDWORD dwVersion)
289 Try calling GetVersionEx using the OSVERSIONINFOEX structure.
290 If that fails, try using the OSVERSIONINFO structure.
292 ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
293 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
295 if (!GetVersionEx(&osvi))
298 switch (osvi.dwPlatformId)
300 case VER_PLATFORM_WIN32_NT:
301 if (osvi.dwMajorVersion <= 4)
302 *dwVersion = OS_VERSION_WINNT;
303 else if (osvi.dwMajorVersion == 5)
304 *dwVersion = OS_VERSION_WIN2K;
309 case VER_PLATFORM_WIN32_WINDOWS:
310 *dwVersion = OS_VERSION_WIN9X;
313 case VER_PLATFORM_WIN32s:
323 static VOID ShowNotifyIcon(HWND hWnd, DWORD dwMessage)
329 ZeroMemory(&nid,sizeof(nid));
330 nid.cbSize = sizeof(NOTIFYICONDATA);
333 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
334 nid.uCallbackMessage = WM_TRAYMESSAGE;
336 while (g_stServices[i].szServiceName != NULL)
338 if (g_stServices[i].dwPid != 0)
342 if (dwMessage != NIM_DELETE)
345 nid.hIcon = g_icoRun;
347 nid.hIcon = g_icoStop;
352 lstrcpy(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGALL-IDS_MSG_FIRST]);
354 sprintf(nid.szTip, g_lpMsg[IDS_MSG_RUNNING-IDS_MSG_FIRST], n, i);
356 sprintf(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGNONE-IDS_MSG_FIRST], i);
358 lstrcpy(nid.szTip, g_lpMsg[IDS_MSG_NOSERVICES-IDS_MSG_FIRST]);
359 Shell_NotifyIcon(dwMessage, &nid);
362 void appendMenuItem(HMENU hMenu, UINT uMenuId, LPSTR szName, BOOL fDefault, BOOL fEnabled)
366 ZeroMemory(&mii, sizeof(MENUITEMINFO));
367 mii.cbSize = sizeof(MENUITEMINFO);
368 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
371 mii.fType = MFT_STRING;
374 mii.fState = MFS_DEFAULT;
376 mii.fState |= MFS_DISABLED;
377 mii.dwTypeData = szName;
380 mii.fType = MFT_SEPARATOR;
381 InsertMenuItem(hMenu, uMenuId, FALSE, &mii);
384 void appendServiceMenu(HMENU hMenu, UINT uMenuId, LPSTR szServiceName, BOOL fRunning)
389 smh = CreatePopupMenu();
391 appendMenuItem(smh, IDM_SM_START + uMenuId, g_lpMsg[IDS_MSG_SSTART-IDS_MSG_FIRST], FALSE, !fRunning);
392 appendMenuItem(smh, IDM_SM_STOP + uMenuId, g_lpMsg[IDS_MSG_SSTOP-IDS_MSG_FIRST], FALSE, fRunning);
393 appendMenuItem(smh, IDM_SM_RESTART + uMenuId, g_lpMsg[IDS_MSG_SRESTART-IDS_MSG_FIRST], FALSE, fRunning);
395 ZeroMemory(&mii, sizeof(MENUITEMINFO));
396 mii.cbSize = sizeof(MENUITEMINFO);
397 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_SUBMENU | MIIM_CHECKMARKS;
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);
408 void ShowTryPopupMenu(HWND hWnd)
410 /* create popup menu */
411 HMENU hMenu = CreatePopupMenu();
416 appendMenuItem(hMenu, IDM_RESTORE, g_lpMsg[IDS_MSG_MNUSHOW-IDS_MSG_FIRST], TRUE, TRUE);
417 if (g_dwOSVersion >= OS_VERSION_WINNT)
418 appendMenuItem(hMenu, IDC_SMANAGER, g_lpMsg[IDS_MSG_MNUSERVICES-IDS_MSG_FIRST], FALSE, TRUE);
419 appendMenuItem(hMenu, 0, "", FALSE, TRUE);
420 appendMenuItem(hMenu, IDM_EXIT, g_lpMsg[IDS_MSG_MNUEXIT-IDS_MSG_FIRST], FALSE, TRUE);
422 if (!SetForegroundWindow(hWnd))
423 SetForegroundWindow(NULL);
425 TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
426 pt.x, pt.y, 0, hWnd, NULL);
431 void ShowTryServicesMenu(HWND hWnd)
433 /* create services list popup menu and submenus */
434 HMENU hMenu = CreatePopupMenu();
440 while (g_stServices[i].szServiceName != NULL)
442 appendServiceMenu(hMenu, i, g_stServices[i].szDisplayName,
443 g_stServices[i].dwPid != 0);
448 if (!SetForegroundWindow(hWnd))
449 SetForegroundWindow(NULL);
451 TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
452 pt.x, pt.y, 0, hWnd, NULL);
458 BOOL CenterWindow(HWND hwndChild)
460 RECT rChild, rWorkArea;
465 /* Get the Height and Width of the child window */
466 GetWindowRect(hwndChild, &rChild);
467 wChild = rChild.right - rChild.left;
468 hChild = rChild.bottom - rChild.top;
470 /* Get the limits of the 'workarea' */
471 bResult = SystemParametersInfo(
477 rWorkArea.left = rWorkArea.top = 0;
478 rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
479 rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
482 /* Calculate new X and Y position*/
483 xNew = (rWorkArea.right - wChild)/2;
484 yNew = (rWorkArea.bottom - hChild)/2;
485 return SetWindowPos (hwndChild, HWND_TOP, xNew, yNew, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);
488 static void addListBoxItem(HWND hDlg, LPSTR lpStr, HBITMAP hBmp)
492 nItem = SendMessage(hDlg, LB_ADDSTRING, 0, (LPARAM)lpStr);
493 SendMessage(hDlg, LB_SETITEMDATA, nItem, (LPARAM)hBmp);
496 static void addListBoxString(HWND hListBox, LPSTR lpStr)
498 static int nItems = 0;
499 if (!g_bDlgServiceOn)
503 if ( nItems > MAX_LOADSTRING)
505 SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
508 ListBox_SetCurSel(hListBox,
509 ListBox_AddString(hListBox, lpStr));
512 static DWORD WINAPI ConsoleOutputThread(LPVOID lpThreadParameter)
514 static BYTE lpBuffer[MAX_PATH+1];
519 while (ReadFile(g_hpipeOutRead, &ch, 1, &dwReaded, NULL) == TRUE)
523 if (ch == '\n' || nPtr >= MAX_PATH)
525 lpBuffer[nPtr] = '\0';
526 addListBoxString(g_hwndStdoutList, lpBuffer);
529 else if (ch == '\t' && nPtr < (MAX_PATH - 4))
532 for (i = 0; i < 4; ++i)
533 lpBuffer[nPtr++] = ' ';
536 lpBuffer[nPtr++] = ch;
539 CloseHandle(g_hpipeInWrite);
540 CloseHandle(g_hpipeOutRead);
541 CloseHandle(g_hpipeStdError);
546 DWORD WINAPI ConsoleWaitingThread(LPVOID lpThreadParameter)
548 WaitForSingleObject(g_lpRedirectProc.hThread, INFINITE);
549 CloseHandle(g_lpRedirectProc.hThread);
551 g_bConsoleRun = FALSE;
552 SetCursor(g_hCursorArrow);
557 BOOL RunRedirectedConsole(LPSTR szCmdLine)
564 ZeroMemory(&stInfo, sizeof(stInfo));
565 stInfo.cb = sizeof(stInfo);
566 stInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
567 stInfo.wShowWindow = SW_HIDE;
569 hProc = GetCurrentProcess();
571 if (!CreatePipe(&g_hpipeInRead, &g_hpipeInWrite, NULL, MAX_PATH))
572 ErrorMessage(NULL, TRUE);
573 if (!CreatePipe(&g_hpipeOutRead, &g_hpipeOutWrite, NULL, MAX_PATH*8))
574 ErrorMessage(NULL, TRUE);
576 DuplicateHandle(hProc, g_hpipeInRead, hProc, &g_hpipeInRead, 0, TRUE,
577 DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
578 DuplicateHandle(hProc, g_hpipeOutWrite, hProc, &g_hpipeOutWrite, 0, TRUE,
579 DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS);
580 DuplicateHandle(hProc, g_hpipeOutWrite, hProc, &g_hpipeStdError, 0, TRUE,
581 DUPLICATE_SAME_ACCESS);
582 if (!g_hpipeInRead && !g_hpipeOutWrite && !g_hpipeStdError)
583 ErrorMessage(NULL, TRUE);
585 stInfo.hStdInput = g_hpipeInRead;
586 stInfo.hStdOutput = g_hpipeOutWrite;
587 stInfo.hStdError = g_hpipeStdError;
589 bResult = CreateProcess(NULL,
601 CloseHandle(g_hpipeInRead);
602 CloseHandle(g_hpipeOutWrite);
603 CloseHandle(g_hpipeStdError);
607 CloseHandle(g_hpipeInWrite);
608 CloseHandle(g_hpipeOutRead);
609 CloseHandle(g_hpipeStdError);
613 CloseHandle(CreateThread(NULL, 0, ConsoleOutputThread, 0, 0, &dwThreadId));
614 ResumeThread(g_lpRedirectProc.hThread);
615 CloseHandle(CreateThread(NULL, 0, ConsoleWaitingThread, 0, 0, &dwThreadId));
620 BOOL RunAndForgetConsole(LPSTR szCmdLine,
621 BOOL bRedirectConsole)
625 PROCESS_INFORMATION prInfo;
628 if (bRedirectConsole)
629 return RunRedirectedConsole(szCmdLine);
632 ZeroMemory(&stInfo, sizeof(stInfo));
633 stInfo.cb = sizeof(stInfo);
634 stInfo.dwFlags = STARTF_USESHOWWINDOW;
635 stInfo.wShowWindow = SW_HIDE;
637 bResult = CreateProcess(NULL,
652 if (g_dwOSVersion == OS_VERSION_WIN9X) /* give some time to rescan the status */
654 CloseHandle(prInfo.hThread);
655 CloseHandle(prInfo.hProcess);
659 BOOL ApacheManageService(LPCSTR szServiceName, LPCSTR szImagePath, LPSTR szComputerName, DWORD dwCommand)
662 CHAR szBuf[MAX_PATH];
663 CHAR szMsg[MAX_PATH];
666 BOOL serviceFlag = TRUE;
667 SC_HANDLE schService;
668 SC_HANDLE schSCManager;
669 SERVICE_STATUS schSStatus;
673 if (g_dwOSVersion == OS_VERSION_WIN9X)
675 sPos = strstr(szImagePath, "-k start");
678 lstrcpyn(szBuf, szImagePath, sPos - szImagePath);
681 case SERVICE_CONTROL_STOP:
682 lstrcat(szBuf, " -k shutdown -n ");
684 case SERVICE_CONTROL_CONTINUE:
685 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTART-IDS_MSG_FIRST], szServiceName);
686 addListBoxString(g_hwndStdoutList, szMsg);
687 lstrcat(szBuf, " -k start -n ");
690 case SERVICE_APACHE_RESTART:
691 lstrcat(szBuf, " -k restart -n ");
696 lstrcat(szBuf, szServiceName);
700 g_bConsoleRun = TRUE;
701 SetCursor(g_hCursorHourglass);
702 if (!RunAndForgetConsole(szBuf, serviceFlag))
704 ErrorMessage(NULL, FALSE);
705 g_bConsoleRun = FALSE;
706 SetCursor(g_hCursorArrow);
709 else if (!serviceFlag)
711 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTARTED-IDS_MSG_FIRST], szServiceName);
712 addListBoxString(g_hwndStdoutList, szMsg);
713 g_bConsoleRun = FALSE;
714 SetCursor(g_hCursorArrow);
720 /* Apache 2.0 uses '-k runservice' as cmdline parameter */
721 sPos = strstr(szImagePath, "--ntservice");
724 sPos = strstr(szImagePath, "-k runservice");
728 lstrcpyn(szBuf, szImagePath, sPos - szImagePath);
731 schSCManager = OpenSCManager(
734 SC_MANAGER_ALL_ACCESS
739 schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS);
740 if (schService != NULL)
743 g_bConsoleRun = TRUE;
744 SetCursor(g_hCursorHourglass);
747 case SERVICE_CONTROL_STOP:
748 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTOP-IDS_MSG_FIRST], szServiceName);
749 addListBoxString(g_hwndStdoutList, szMsg);
750 if (ControlService(schService, SERVICE_CONTROL_STOP, &schSStatus))
753 while (QueryServiceStatus(schService, &schSStatus))
755 if (schSStatus.dwCurrentState == SERVICE_STOP_PENDING)
761 if (QueryServiceStatus(schService, &schSStatus))
763 if (schSStatus.dwCurrentState == SERVICE_STOPPED)
766 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTOPPED-IDS_MSG_FIRST], szServiceName);
767 addListBoxString(g_hwndStdoutList, szMsg);
771 case SERVICE_CONTROL_CONTINUE:
772 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTART-IDS_MSG_FIRST], szServiceName);
773 addListBoxString(g_hwndStdoutList, szMsg);
774 args = (char **)malloc(3 * sizeof(char*));
777 args[1] = "--ntservice";
781 args[2] = "runservice";
783 if (StartService(schService, serviceFlag ? 2 : 3, args))
786 while (QueryServiceStatus(schService, &schSStatus))
788 if (schSStatus.dwCurrentState == SERVICE_START_PENDING)
794 if (QueryServiceStatus(schService, &schSStatus))
796 if (schSStatus.dwCurrentState == SERVICE_RUNNING)
799 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVSTARTED-IDS_MSG_FIRST], szServiceName);
800 addListBoxString(g_hwndStdoutList, szMsg);
803 /* is this OK to do? */
806 case SERVICE_APACHE_RESTART:
807 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVRESTART-IDS_MSG_FIRST], szServiceName);
808 addListBoxString(g_hwndStdoutList, szMsg);
809 if (ControlService(schService, SERVICE_APACHE_RESTART, &schSStatus))
812 while (schSStatus.dwCurrentState == SERVICE_START_PENDING)
815 if (!QueryServiceStatus(schService, &schSStatus))
817 CloseServiceHandle(schService);
818 CloseServiceHandle(schSCManager);
819 g_bConsoleRun = FALSE;
820 SetCursor(g_hCursorArrow);
827 if (schSStatus.dwCurrentState == SERVICE_RUNNING)
830 sprintf(szMsg, g_lpMsg[IDS_MSG_SRVRESTARTED-IDS_MSG_FIRST], szServiceName);
831 addListBoxString(g_hwndStdoutList, szMsg);
835 CloseServiceHandle(schService);
836 CloseServiceHandle(schSCManager);
838 ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED-IDS_MSG_FIRST], FALSE);
839 g_bConsoleRun = FALSE;
840 SetCursor(g_hCursorArrow);
845 g_bRescanServices = TRUE;
847 CloseServiceHandle(schSCManager);
854 BOOL IsServiceRunning(LPCSTR szServiceName, LPCSTR szComputerName, LPDWORD lpdwPid)
859 SC_HANDLE schService;
860 SC_HANDLE schSCManager;
861 SERVICE_STATUS schSStatus;
863 if (g_dwOSVersion == OS_VERSION_WIN9X)
865 hWnd = FindWindow("ApacheWin95ServiceMonitor", szServiceName);
866 if (hWnd && GetWindowThreadProcessId(hWnd, &dwPid))
878 schSCManager = OpenSCManager(
881 SC_MANAGER_ALL_ACCESS
886 schService = OpenService(schSCManager, szServiceName, SERVICE_QUERY_STATUS);
887 if (schService != NULL)
889 if (QueryServiceStatus(schService, &schSStatus))
892 dwPid = schSStatus.dwCurrentState;
896 CloseServiceHandle(schService);
897 CloseServiceHandle(schSCManager);
898 return dwPid == SERVICE_RUNNING ? TRUE : FALSE;
901 g_bRescanServices = TRUE;
903 CloseServiceHandle(schSCManager);
911 BOOL FindRunningServices(void)
916 while (g_stServices[i].szServiceName != NULL)
918 if (!IsServiceRunning(g_stServices[i].szServiceName, g_stServices[i].szComputerName, &dwPid))
920 if (g_stServices[i].dwPid != dwPid)
922 g_stServices[i].dwPid = dwPid;
928 BOOL GetApacheServicesStatus()
931 CHAR szKey[MAX_PATH];
932 CHAR achKey[MAX_PATH];
933 CHAR szImagePath[MAX_PATH];
934 CHAR szBuf[MAX_PATH];
935 CHAR szTmp[MAX_PATH];
937 HKEY hKey, hSubKey, hKeyRemote;
938 DWORD retCode, rv, dwKeyType;
939 DWORD dwBufLen = MAX_PATH;
943 g_bRescanServices = FALSE;
946 am_ClearServicesSt();
947 while (g_stComputers[computers].szComputerName != NULL) {
948 hKeyRemote = g_stComputers[computers].hRegistry;
949 retCode = RegOpenKeyEx(hKeyRemote,
950 "System\\CurrentControlSet\\Services\\",
952 if (retCode != ERROR_SUCCESS)
954 ErrorMessage(NULL, FALSE);
957 for (i = 0, retCode = ERROR_SUCCESS; retCode == ERROR_SUCCESS; i++)
960 retCode = RegEnumKey(hKey, i, achKey, MAX_PATH);
961 if (retCode == ERROR_SUCCESS)
963 lstrcpy(szKey, "System\\CurrentControlSet\\Services\\");
964 lstrcat(szKey, achKey);
966 if (RegOpenKeyEx(hKeyRemote, szKey, 0,
967 KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS)
970 rv = RegQueryValueEx(hSubKey, "ImagePath", NULL,
971 &dwKeyType, szImagePath, &dwBufLen);
973 if (rv == ERROR_SUCCESS && (dwKeyType == REG_SZ || dwKeyType == REG_EXPAND_SZ) && dwBufLen)
975 lstrcpy(szBuf, szImagePath);
977 /* the service name could be Apache*.exe */
978 if (strstr(szBuf, "\\apache") != NULL && strstr(szBuf, ".exe") &&
979 (strstr(szBuf, "--ntservice") != NULL || strstr(szBuf, "-k ") !=NULL))
981 g_stServices[stPos].szServiceName = strdup(achKey);
982 g_stServices[stPos].szImagePath = strdup(szImagePath);
983 g_stServices[stPos].szComputerName = strdup(g_stComputers[computers].szComputerName);
985 if (RegQueryValueEx(hSubKey, "Description", NULL,
986 &dwKeyType, szBuf, &dwBufLen) == ERROR_SUCCESS)
987 g_stServices[stPos].szDescription = strdup(szBuf);
990 if (RegQueryValueEx(hSubKey, "DisplayName", NULL,
991 &dwKeyType, szBuf, &dwBufLen) == ERROR_SUCCESS) {
992 if (strcmp(g_stComputers[computers].szComputerName, g_szLocalHost) != 0) {
993 strcpy(szTmp, g_stComputers[computers].szComputerName + 2);
995 strcat(szTmp, szBuf);
998 strcpy( szTmp, szBuf);
999 g_stServices[stPos].szDisplayName= strdup(szTmp);
1003 if (stPos >= MAX_APACHE_SERVICES)
1004 retCode = !ERROR_SUCCESS;
1007 RegCloseKey(hSubKey);
1014 FindRunningServices();
1018 LRESULT CALLBACK ConnectDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1020 CHAR szCmp[MAX_COMPUTERNAME_LENGTH+4];
1025 ShowWindow(hDlg, SW_HIDE);
1026 g_hwndConnectDlg = hDlg;
1028 ShowWindow(hDlg, SW_SHOW);
1029 SetFocus(GetDlgItem(hDlg, IDC_COMPUTER));
1032 switch (LOWORD(wParam))
1035 ZeroMemory(szCmp, MAX_COMPUTERNAME_LENGTH+4);
1036 strcpy(szCmp, "\\\\");
1037 SendMessage(GetDlgItem(hDlg, IDC_COMPUTER), WM_GETTEXT,
1038 (WPARAM) MAX_COMPUTERNAME_LENGTH, (LPARAM) szCmp+2);
1041 if (strlen(szCmp) < 3) {
1042 EndDialog(hDlg, TRUE);
1045 am_ConnectComputer(szCmp);
1046 SendMessage(g_hwndMain, WM_TIMER, WM_TIMER_RESCAN, 0);
1048 EndDialog(hDlg, TRUE);
1055 memset(&bi, 0, sizeof(BROWSEINFO));
1056 SHGetSpecialFolderLocation(hDlg, CSIDL_NETWORK, &il);
1058 bi.lpszTitle = "ApacheMonitor :\nSelect Network Computer!";
1059 bi.pszDisplayName = szCmp;
1060 bi.hwndOwner = hDlg;
1061 bi.ulFlags = BIF_BROWSEFORCOMPUTER;
1067 if (SHBrowseForFolder(&bi) != NULL) {
1068 SendMessage(GetDlgItem(hDlg, IDC_COMPUTER), WM_SETTEXT,
1069 (WPARAM) NULL, (LPARAM) szCmp);
1071 if (SHGetMalloc(&pMalloc)) {
1072 pMalloc->lpVtbl->Free(pMalloc, il);
1073 pMalloc->lpVtbl->Release(pMalloc);
1081 EndDialog(hDlg, TRUE);
1090 LRESULT CALLBACK ServiceDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1093 CHAR szBuf[MAX_PATH];
1095 static HWND hStatusBar;
1101 LPMEASUREITEMSTRUCT lpmis;
1102 LPDRAWITEMSTRUCT lpdis;
1104 ZeroMemory(szBuf, MAX_PATH);
1109 ShowWindow(hDlg, SW_HIDE);
1110 g_hwndServiceDlg = hDlg;
1111 SetWindowText(hDlg, g_szTitle);
1112 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1113 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1114 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1115 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1116 SetWindowText(GetDlgItem(hDlg, IDC_SSTART), g_lpMsg[IDS_MSG_SSTART-IDS_MSG_FIRST]);
1117 SetWindowText(GetDlgItem(hDlg, IDC_SSTOP), g_lpMsg[IDS_MSG_SSTOP-IDS_MSG_FIRST]);
1118 SetWindowText(GetDlgItem(hDlg, IDC_SRESTART), g_lpMsg[IDS_MSG_SRESTART-IDS_MSG_FIRST]);
1119 SetWindowText(GetDlgItem(hDlg, IDC_SMANAGER), g_lpMsg[IDS_MSG_SERVICES-IDS_MSG_FIRST]);
1120 SetWindowText(GetDlgItem(hDlg, IDC_SCONNECT), g_lpMsg[IDS_MSG_CONNECT-IDS_MSG_FIRST]);
1121 SetWindowText(GetDlgItem(hDlg, IDC_SEXIT), g_lpMsg[IDS_MSG_MNUEXIT-IDS_MSG_FIRST]);
1122 if (g_dwOSVersion < OS_VERSION_WINNT)
1124 ShowWindow(GetDlgItem(hDlg, IDC_SMANAGER), SW_HIDE);
1125 ShowWindow(GetDlgItem(hDlg, IDC_SCONNECT), SW_HIDE);
1126 ShowWindow(GetDlgItem(hDlg, IDC_SDISCONN), SW_HIDE);
1128 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1129 g_hwndStdoutList = GetDlgItem(hDlg, IDL_STDOUT);
1130 hStatusBar = CreateStatusWindow(0x0800 /* SBT_TOOLTIPS */
1131 | WS_CHILD | WS_VISIBLE,
1132 "", hDlg, IDC_STATBAR);
1133 if (GetApacheServicesStatus())
1136 while (g_stServices[i].szServiceName != NULL)
1138 addListBoxItem(hListBox, g_stServices[i].szDisplayName,
1139 g_stServices[i].dwPid == 0 ? g_hBmpStop : g_hBmpStart);
1144 ShowWindow(hDlg, SW_SHOW);
1146 SendMessage(hListBox, LB_SETCURSEL, 0, 0);
1149 case WM_MANAGEMESSAGE:
1150 ApacheManageService(g_stServices[LOWORD(wParam)].szServiceName,
1151 g_stServices[LOWORD(wParam)].szImagePath,
1152 g_stServices[LOWORD(wParam)].szComputerName,
1157 case WM_UPDATEMESSAGE:
1158 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1159 SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
1160 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)"");
1161 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1162 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1163 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1164 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1166 while (g_stServices[i].szServiceName != NULL)
1168 addListBoxItem(hListBox, g_stServices[i].szDisplayName,
1169 g_stServices[i].dwPid == 0 ? g_hBmpStop : g_hBmpStart);
1172 SendMessage(hListBox, LB_SETCURSEL, 0, 0);
1173 /* Dirty hack to bring the window to the foreground */
1174 SetWindowPos(hDlg, HWND_TOPMOST, 0, 0, 0, 0,
1175 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1176 SetWindowPos(hDlg, HWND_NOTOPMOST, 0, 0, 0, 0,
1177 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1181 case WM_MEASUREITEM:
1183 lpmis = (LPMEASUREITEMSTRUCT) lParam;
1184 lpmis->itemHeight = YBITMAP;
1188 SetCursor(g_hCursorHourglass);
1190 SetCursor(g_hCursorArrow);
1193 lpdis = (LPDRAWITEMSTRUCT) lParam;
1194 if (lpdis->itemID == -1)
1198 switch (lpdis->itemAction)
1201 case ODA_DRAWENTIRE:
1202 g_hBmpPicture = (HBITMAP)SendMessage(lpdis->hwndItem,
1203 LB_GETITEMDATA, lpdis->itemID, (LPARAM) 0);
1205 hdcMem = CreateCompatibleDC(lpdis->hDC);
1206 g_hBmpOld = SelectObject(hdcMem, g_hBmpPicture);
1209 lpdis->rcItem.left, lpdis->rcItem.top,
1210 lpdis->rcItem.right - lpdis->rcItem.left,
1211 lpdis->rcItem.bottom - lpdis->rcItem.top,
1212 hdcMem, 0, 0, SRCCOPY);
1213 SendMessage(lpdis->hwndItem, LB_GETTEXT,
1214 lpdis->itemID, (LPARAM) szBuf);
1216 GetTextMetrics(lpdis->hDC, &tm);
1217 y = (lpdis->rcItem.bottom + lpdis->rcItem.top -
1220 SelectObject(hdcMem, g_hBmpOld);
1223 rcBitmap.left = lpdis->rcItem.left + XBITMAP + 2;
1224 rcBitmap.top = lpdis->rcItem.top;
1225 rcBitmap.right = lpdis->rcItem.right;
1226 rcBitmap.bottom = lpdis->rcItem.top + YBITMAP;
1228 if (lpdis->itemState & ODS_SELECTED)
1230 if (g_hBmpPicture == g_hBmpStop)
1232 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), TRUE);
1233 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1234 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1236 else if (g_hBmpPicture == g_hBmpStart)
1238 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1239 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), TRUE);
1240 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), TRUE);
1243 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1244 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1245 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1247 if (strcmp(g_stServices[lpdis->itemID].szComputerName, g_szLocalHost) == 0)
1248 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1250 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), TRUE);
1252 if (g_stServices[lpdis->itemID].szDescription)
1253 SendMessage(hStatusBar, SB_SETTEXT, 0,
1254 (LPARAM)g_stServices[lpdis->itemID].szDescription);
1256 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)"");
1258 SetTextColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
1259 SetBkColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHT));
1260 FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_HIGHLIGHTTEXT));
1264 SetTextColor(lpdis->hDC, GetSysColor(COLOR_MENUTEXT));
1265 SetBkColor(lpdis->hDC, GetSysColor(COLOR_WINDOW));
1266 FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_WINDOW+1));
1280 switch (LOWORD(wParam))
1283 switch (HIWORD(wParam))
1286 /* if started then stop, if stopped then start the service */
1287 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1288 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1289 if (nItem != LB_ERR)
1291 g_hBmpPicture = (HBITMAP)SendMessage(hListBox, LB_GETITEMDATA,
1293 if (g_hBmpPicture == g_hBmpStop)
1295 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem, SERVICE_CONTROL_CONTINUE);
1298 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem, SERVICE_CONTROL_STOP);
1306 EndDialog(hDlg, TRUE);
1309 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1310 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1311 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1312 if (nItem != LB_ERR)
1314 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem, SERVICE_CONTROL_CONTINUE);
1316 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), TRUE);
1319 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1320 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1321 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1322 if (nItem != LB_ERR)
1324 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem, SERVICE_CONTROL_STOP);
1326 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), TRUE);
1329 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1330 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1331 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1332 if (nItem != LB_ERR)
1334 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem, SERVICE_APACHE_RESTART);
1336 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), TRUE);
1339 if (g_dwOSVersion >= OS_VERSION_WIN2K)
1340 ShellExecute(hDlg, "open", "services.msc", "/s", NULL, SW_NORMAL);
1342 WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL);
1345 EndDialog( hDlg, TRUE);
1346 SendMessage( g_hwndMain, WM_COMMAND, (WPARAM)IDM_EXIT, 0);
1349 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGCONNECT),
1350 hDlg, (DLGPROC)ConnectDlgProc);
1353 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1354 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1355 if (nItem != LB_ERR) {
1356 am_DisconnectComputer(g_stServices[nItem].szComputerName);
1357 SendMessage(g_hwndMain, WM_TIMER, WM_TIMER_RESCAN, 0);
1363 switch (LOWORD(wParam))
1365 case SIZE_MINIMIZED:
1366 EndDialog(hDlg, TRUE);
1373 EndDialog(hDlg, TRUE);
1382 LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
1383 WPARAM wParam, LPARAM lParam)
1385 if (message == g_bUiTaskbarCreated)
1387 /* restore the tray icon on shell restart */
1388 ShowNotifyIcon(hWnd, NIM_ADD);
1389 return DefWindowProc(hWnd, message, wParam, lParam);
1394 GetApacheServicesStatus();
1395 ShowNotifyIcon(hWnd, NIM_ADD);
1396 SetTimer(hWnd, WM_TIMER_REFRESH, REFRESH_TIME, NULL);
1397 SetTimer(hWnd, WM_TIMER_RESCAN, RESCAN_TIME, NULL);
1402 case WM_TIMER_RESCAN:
1404 int nPrev = 0, nNew = 0;
1405 EnterCriticalSection(&g_stcSection);
1406 if (FindRunningServices() || g_bRescanServices)
1408 ShowNotifyIcon(hWnd, NIM_MODIFY);
1409 if (g_hwndServiceDlg)
1410 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1412 /* check if services list changed */
1413 while (g_stServices[nPrev].szServiceName != NULL)
1415 GetApacheServicesStatus();
1416 while (g_stServices[nNew].szServiceName != NULL)
1420 ShowNotifyIcon(hWnd, NIM_MODIFY);
1421 if (g_hwndServiceDlg)
1422 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1424 LeaveCriticalSection(&g_stcSection);
1427 case WM_TIMER_REFRESH:
1429 int nPrev = 0, nNew = 0;
1430 EnterCriticalSection(&g_stcSection);
1431 if (g_bRescanServices)
1433 GetApacheServicesStatus();
1434 ShowNotifyIcon(hWnd, NIM_MODIFY);
1435 if (g_hwndServiceDlg)
1436 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1438 else if (FindRunningServices())
1440 ShowNotifyIcon(hWnd, NIM_MODIFY);
1441 if (g_hwndServiceDlg)
1442 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1444 LeaveCriticalSection(&g_stcSection);
1450 ShowNotifyIcon(hWnd, NIM_DELETE);
1452 case WM_TRAYMESSAGE:
1455 case WM_LBUTTONDBLCLK:
1456 if (!g_bDlgServiceOn)
1458 g_bDlgServiceOn = TRUE;
1459 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES),
1460 hWnd, (DLGPROC)ServiceDlgProc);
1461 g_bDlgServiceOn = FALSE;
1462 g_hwndServiceDlg = NULL;
1464 else if (IsWindow(g_hwndServiceDlg))
1466 /* Dirty hack to bring the window to the foreground */
1467 SetWindowPos(g_hwndServiceDlg, HWND_TOPMOST, 0, 0, 0, 0,
1468 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1469 SetWindowPos(g_hwndServiceDlg, HWND_NOTOPMOST, 0, 0, 0, 0,
1470 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1471 SetFocus(g_hwndServiceDlg);
1475 ShowTryServicesMenu(hWnd);
1478 ShowTryPopupMenu(hWnd);
1483 if ((LOWORD(wParam) & IDM_SM_START) == IDM_SM_START)
1485 ApacheManageService(g_stServices[LOWORD(wParam) - IDM_SM_START].szServiceName,
1486 g_stServices[LOWORD(wParam) - IDM_SM_START].szImagePath,
1487 g_stServices[LOWORD(wParam) - IDM_SM_START].szComputerName,
1488 SERVICE_CONTROL_CONTINUE);
1491 else if ((LOWORD(wParam) & IDM_SM_STOP) == IDM_SM_STOP)
1493 ApacheManageService(g_stServices[LOWORD(wParam) - IDM_SM_STOP].szServiceName,
1494 g_stServices[LOWORD(wParam) - IDM_SM_STOP].szImagePath,
1495 g_stServices[LOWORD(wParam) - IDM_SM_STOP].szComputerName,
1496 SERVICE_CONTROL_STOP);
1499 else if ((LOWORD(wParam) & IDM_SM_RESTART) == IDM_SM_RESTART)
1501 ApacheManageService(g_stServices[LOWORD(wParam) - IDM_SM_RESTART].szServiceName,
1502 g_stServices[LOWORD(wParam) - IDM_SM_RESTART].szImagePath,
1503 g_stServices[LOWORD(wParam) - IDM_SM_RESTART].szComputerName,
1504 SERVICE_APACHE_RESTART);
1507 switch (LOWORD(wParam))
1510 if (!g_bDlgServiceOn)
1512 g_bDlgServiceOn = TRUE;
1513 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES),
1514 hWnd, (DLGPROC)ServiceDlgProc);
1515 g_bDlgServiceOn = FALSE;
1516 g_hwndServiceDlg = NULL;
1518 else if (IsWindow(g_hwndServiceDlg))
1519 SetFocus(g_hwndServiceDlg);
1522 if (g_dwOSVersion >= OS_VERSION_WIN2K)
1523 ShellExecute(NULL, "open", "services.msc", "/s", NULL, SW_NORMAL);
1525 WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL);
1528 ShowNotifyIcon(hWnd, NIM_DELETE);
1533 return DefWindowProc(hWnd, message, wParam, lParam);
1539 /* Create main invisible window */
1540 HWND CreateMainWindow(HINSTANCE hInstance)
1545 if (!GetSystemOSVersion(&g_dwOSVersion))
1547 ErrorMessage(NULL, TRUE);
1551 wcex.cbSize = sizeof(WNDCLASSEX);
1553 wcex.style = CS_HREDRAW | CS_VREDRAW;
1554 wcex.lpfnWndProc = (WNDPROC)WndProc;
1555 wcex.cbClsExtra = 0;
1556 wcex.cbWndExtra = 0;
1557 wcex.hInstance = hInstance;
1558 wcex.hIcon = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON),
1559 IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
1560 wcex.hCursor = g_hCursorArrow;
1561 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
1562 wcex.lpszMenuName = 0;
1563 wcex.lpszClassName = g_szWindowClass;
1564 wcex.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON),
1565 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1567 if (RegisterClassEx(&wcex))
1568 hWnd = CreateWindow(g_szWindowClass, g_szTitle,
1570 NULL, NULL, hInstance, NULL);
1577 int WINAPI WinMain(HINSTANCE hInstance,
1578 HINSTANCE hPrevInstance,
1582 CHAR szTmp[MAX_LOADSTRING];
1583 CHAR szCmp[MAX_COMPUTERNAME_LENGTH+4];
1585 /* single instance mutex */
1590 g_LangID = GetUserDefaultLangID();
1591 if ((g_LangID & 0xFF) != LANG_ENGLISH)
1592 g_LangID = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
1594 for (i = IDS_MSG_FIRST; i <= IDS_MSG_LAST; ++i) {
1595 LoadString(hInstance, i, szTmp, MAX_LOADSTRING);
1596 g_lpMsg[i - IDS_MSG_FIRST] = strdup(szTmp);
1598 LoadString(hInstance, IDS_APMONITORTITLE, szTmp, MAX_LOADSTRING);
1599 d = MAX_COMPUTERNAME_LENGTH+1;
1600 strcpy(szCmp, "\\\\");
1601 GetComputerName(szCmp + 2, &d);
1603 g_szLocalHost = strdup(szCmp);
1605 ZeroMemory(g_stComputers, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);
1606 g_stComputers[0].szComputerName = strdup(szCmp);
1607 g_stComputers[0].hRegistry = HKEY_LOCAL_MACHINE;
1608 g_szTitle = strdup(szTmp);
1609 LoadString(hInstance, IDS_APMONITORCLASS, szTmp, MAX_LOADSTRING);
1610 g_szWindowClass = strdup(szTmp);
1612 g_icoStop = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICOSTOP),
1613 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1614 g_icoRun = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICORUN),
1615 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1616 g_hCursorHourglass = LoadImage(NULL, MAKEINTRESOURCE(OCR_WAIT), IMAGE_CURSOR,
1617 LR_DEFAULTSIZE, LR_DEFAULTSIZE, LR_SHARED);
1618 g_hCursorArrow = LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR,
1619 LR_DEFAULTSIZE, LR_DEFAULTSIZE, LR_SHARED);
1620 g_hBmpStart = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPRUN),
1621 IMAGE_BITMAP, XBITMAP, YBITMAP, LR_DEFAULTCOLOR);
1622 g_hBmpStop = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPSTOP),
1623 IMAGE_BITMAP, XBITMAP, YBITMAP, LR_DEFAULTCOLOR);
1625 hMutex = CreateMutex(NULL, FALSE, "APSRVMON_MUTEX");
1626 if ((hMutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS))
1628 ErrorMessage(g_lpMsg[IDS_MSG_APPRUNNING-IDS_MSG_FIRST], FALSE);
1630 CloseHandle(hMutex);
1635 ZeroMemory(g_stServices, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);
1637 InitCommonControls();
1638 g_hInstance = hInstance;
1639 g_hwndMain = CreateMainWindow(hInstance);
1640 g_bUiTaskbarCreated = RegisterWindowMessage("TaskbarCreated");
1641 InitializeCriticalSection(&g_stcSection);
1642 g_hwndServiceDlg = NULL;
1643 if (g_hwndMain != NULL)
1645 while (GetMessage(&msg, NULL, 0, 0) == TRUE)
1647 TranslateMessage(&msg);
1648 DispatchMessage(&msg);
1650 am_ClearServicesSt();
1652 am_ClearComputersSt();
1653 DeleteCriticalSection(&g_stcSection);
1654 CloseHandle(hMutex);
1655 DestroyIcon(g_icoStop);
1656 DestroyIcon(g_icoRun);
1657 DestroyCursor(g_hCursorHourglass);
1658 DestroyCursor(g_hCursorArrow);
1659 DeleteObject(g_hBmpStart);
1660 DeleteObject(g_hBmpStop);