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 0x0500
34 #if defined(_MSC_VER) && _MSC_VER >= 1400
35 #define _CRT_SECURE_NO_DEPRECATE
47 #include "ApacheMonitor.h"
50 /** Properly quote a value as a string in the C preprocessor */
51 #define AM_STRINGIFY(n) AM_STRINGIFY_HELPER(n)
52 /** Helper macro for AM_STRINGIFY */
53 #define AM_STRINGIFY_HELPER(n) #n
56 #define OS_VERSION_WINNT 2
57 #define OS_VERSION_WIN2K 3
59 /* Should be enough */
60 #define MAX_APACHE_SERVICES 128
61 #define MAX_APACHE_COMPUTERS 32
63 #define WM_TRAYMESSAGE (WM_APP+1)
64 #define WM_UPDATEMESSAGE (WM_USER+1)
65 #define WM_MANAGEMESSAGE (WM_USER+2)
66 #define WM_TIMER_REFRESH 10
67 #define WM_TIMER_RESCAN 11
68 #define SERVICE_APACHE_RESTART 128
71 #define MAX_LOADSTRING 100
72 #define REFRESH_TIME 2000 /* service refresh time (ms) */
73 #define RESCAN_TIME 20000 /* registry rescan time (ms) */
75 typedef struct _st_APACHE_SERVICE
81 LPTSTR szComputerName;
85 typedef struct _st_MONITORED_COMPUTERS
87 LPTSTR szComputerName;
91 /* Global variables */
92 HINSTANCE g_hInstance = NULL;
93 TCHAR *g_szTitle; /* The title bar text */
94 TCHAR *g_szWindowClass; /* Window Class Name */
97 UINT g_bUiTaskbarCreated;
99 BOOL g_bDlgServiceOn = FALSE;
100 BOOL g_bConsoleRun = FALSE;
101 ST_APACHE_SERVICE g_stServices[MAX_APACHE_SERVICES];
102 ST_MONITORED_COMP g_stComputers[MAX_APACHE_COMPUTERS];
104 HBITMAP g_hBmpStart, g_hBmpStop;
105 HBITMAP g_hBmpPicture, g_hBmpOld;
106 BOOL g_bRescanServices;
107 HWND g_hwndServiceDlg;
109 HWND g_hwndStdoutList;
110 HWND g_hwndConnectDlg;
111 HCURSOR g_hCursorHourglass;
112 HCURSOR g_hCursorArrow;
115 CRITICAL_SECTION g_stcSection;
116 LPTSTR g_szLocalHost;
118 /* locale language support */
119 static TCHAR *g_lpMsg[IDS_MSG_LAST - IDS_MSG_FIRST + 1];
122 void am_ClearServicesSt()
125 for (i = 0; i < MAX_APACHE_SERVICES; i++)
127 if (g_stServices[i].szServiceName) {
128 free(g_stServices[i].szServiceName);
130 if (g_stServices[i].szDisplayName) {
131 free(g_stServices[i].szDisplayName);
133 if (g_stServices[i].szDescription) {
134 free(g_stServices[i].szDescription);
136 if (g_stServices[i].szImagePath) {
137 free(g_stServices[i].szImagePath);
139 if (g_stServices[i].szComputerName) {
140 free(g_stServices[i].szComputerName);
144 memset(g_stServices, 0, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);
149 void am_ClearComputersSt()
152 for (i = 0; i < MAX_APACHE_COMPUTERS; i++) {
153 if (g_stComputers[i].szComputerName) {
154 free(g_stComputers[i].szComputerName);
155 RegCloseKey(g_stComputers[i].hRegistry);
158 memset(g_stComputers, 0, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);
163 BOOL am_IsComputerConnected(LPTSTR szComputerName)
166 while (g_stComputers[i].szComputerName != NULL) {
167 if (_tcscmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
176 void am_DisconnectComputer(LPTSTR szComputerName)
179 while (g_stComputers[i].szComputerName != NULL) {
180 if (_tcscmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
185 if (g_stComputers[i].szComputerName != NULL) {
186 free(g_stComputers[i].szComputerName);
187 RegCloseKey(g_stComputers[i].hRegistry);
188 for (j = i; j < MAX_APACHE_COMPUTERS - 1; j++) {
189 g_stComputers[j].szComputerName= g_stComputers[j+1].szComputerName;
190 g_stComputers[j].hRegistry = g_stComputers[j+1].hRegistry;
192 g_stComputers[j].szComputerName = NULL;
193 g_stComputers[j].hRegistry = NULL;
198 void ErrorMessage(LPCTSTR szError, BOOL bFatal)
200 LPVOID lpMsgBuf = NULL;
202 MessageBox(NULL, szError, g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST],
203 MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION));
206 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
207 FORMAT_MESSAGE_FROM_SYSTEM |
208 FORMAT_MESSAGE_IGNORE_INSERTS,
209 NULL, GetLastError(), g_LangID,
210 (LPTSTR) &lpMsgBuf, 0, NULL);
211 MessageBox(NULL, (LPCTSTR)lpMsgBuf,
212 g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST],
213 MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION));
222 int am_RespawnAsUserAdmin(HWND hwnd, DWORD op, LPCTSTR szService,
223 LPCTSTR szComputerName)
225 TCHAR args[MAX_PATH + MAX_COMPUTERNAME_LENGTH + 12];
227 if (g_dwOSVersion < OS_VERSION_WIN2K) {
228 ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED - IDS_MSG_FIRST], FALSE);
232 _sntprintf(args, sizeof(args) / sizeof(TCHAR),
233 _T("%d \"%s\" \"%s\""), op, szService,
234 szComputerName ? szComputerName : _T(""));
235 if (!ShellExecute(hwnd, _T("runas"), __targv[0], args, NULL, SW_NORMAL)) {
236 ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED - IDS_MSG_FIRST],
245 BOOL am_ConnectComputer(LPTSTR szComputerName)
249 TCHAR szTmp[MAX_PATH];
251 while (g_stComputers[i].szComputerName != NULL) {
252 if (_tcscmp(g_stComputers[i].szComputerName, szComputerName) == 0) {
257 if (i > MAX_APACHE_COMPUTERS - 1) {
260 if (RegConnectRegistry(szComputerName, HKEY_LOCAL_MACHINE, &hKeyRemote)
262 _sntprintf(szTmp, sizeof(szTmp) / sizeof(TCHAR),
263 g_lpMsg[IDS_MSG_ECONNECT - IDS_MSG_FIRST],
265 ErrorMessage(szTmp, FALSE);
269 g_stComputers[i].szComputerName = _tcsdup(szComputerName);
270 g_stComputers[i].hRegistry = hKeyRemote;
276 LPTSTR GetStringRes(int id)
278 static TCHAR buffer[MAX_PATH];
281 LoadString(GetModuleHandle(NULL), id, buffer, MAX_PATH);
286 BOOL GetSystemOSVersion(LPDWORD dwVersion)
290 Try calling GetVersionEx using the OSVERSIONINFOEX structure.
291 If that fails, try using the OSVERSIONINFO structure.
293 memset(&osvi, 0, sizeof(OSVERSIONINFO));
294 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
296 if (!GetVersionEx(&osvi)) {
300 switch (osvi.dwPlatformId)
302 case VER_PLATFORM_WIN32_NT:
303 if (osvi.dwMajorVersion >= 5)
304 *dwVersion = OS_VERSION_WIN2K;
306 *dwVersion = OS_VERSION_WINNT;
309 case VER_PLATFORM_WIN32_WINDOWS:
310 case VER_PLATFORM_WIN32s:
319 static VOID ShowNotifyIcon(HWND hWnd, DWORD dwMessage)
324 memset(&nid, 0, sizeof(nid));
325 nid.cbSize = sizeof(NOTIFYICONDATA);
328 nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
329 nid.uCallbackMessage = WM_TRAYMESSAGE;
331 while (g_stServices[i].szServiceName != NULL)
333 if (g_stServices[i].dwPid != 0) {
338 if (dwMessage != NIM_DELETE)
341 nid.hIcon = g_icoRun;
344 nid.hIcon = g_icoStop;
350 if (n == i && n > 0) {
351 _tcscpy(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGALL - IDS_MSG_FIRST]);
354 _sntprintf(nid.szTip, sizeof(nid.szTip) / sizeof(TCHAR),
355 g_lpMsg[IDS_MSG_RUNNING - IDS_MSG_FIRST], n, i);
358 _sntprintf(nid.szTip, sizeof(nid.szTip) / sizeof(TCHAR),
359 g_lpMsg[IDS_MSG_RUNNINGNONE - IDS_MSG_FIRST], i);
362 _tcscpy(nid.szTip, g_lpMsg[IDS_MSG_NOSERVICES - IDS_MSG_FIRST]);
364 Shell_NotifyIcon(dwMessage, &nid);
368 void appendMenuItem(HMENU hMenu, UINT uMenuId, LPTSTR szName,
369 BOOL fDefault, BOOL fEnabled)
373 memset(&mii, 0, sizeof(MENUITEMINFO));
374 mii.cbSize = sizeof(MENUITEMINFO);
375 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
378 mii.fType = MFT_STRING;
381 mii.fState = MFS_DEFAULT;
384 mii.fState |= MFS_DISABLED;
386 mii.dwTypeData = szName;
389 mii.fType = MFT_SEPARATOR;
391 InsertMenuItem(hMenu, uMenuId, FALSE, &mii);
395 void appendServiceMenu(HMENU hMenu, UINT uMenuId,
396 LPTSTR szServiceName, BOOL fRunning)
401 smh = CreatePopupMenu();
403 appendMenuItem(smh, IDM_SM_START + uMenuId,
404 g_lpMsg[IDS_MSG_SSTART - IDS_MSG_FIRST], FALSE, !fRunning);
405 appendMenuItem(smh, IDM_SM_STOP + uMenuId,
406 g_lpMsg[IDS_MSG_SSTOP - IDS_MSG_FIRST], FALSE, fRunning);
407 appendMenuItem(smh, IDM_SM_RESTART + uMenuId,
408 g_lpMsg[IDS_MSG_SRESTART - IDS_MSG_FIRST], FALSE, fRunning);
410 memset(&mii, 0, sizeof(MENUITEMINFO));
411 mii.cbSize = sizeof(MENUITEMINFO);
412 mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_SUBMENU
414 mii.fType = MFT_STRING;
416 mii.hbmpChecked = g_hBmpStart;
417 mii.hbmpUnchecked = g_hBmpStop;
418 mii.dwTypeData = szServiceName;
420 mii.fState = fRunning ? MFS_CHECKED : MFS_UNCHECKED;
421 InsertMenuItem(hMenu, IDM_SM_SERVICE + uMenuId, FALSE, &mii);
425 void ShowTryPopupMenu(HWND hWnd)
427 /* create popup menu */
428 HMENU hMenu = CreatePopupMenu();
433 appendMenuItem(hMenu, IDM_RESTORE,
434 g_lpMsg[IDS_MSG_MNUSHOW - IDS_MSG_FIRST],
436 appendMenuItem(hMenu, IDC_SMANAGER,
437 g_lpMsg[IDS_MSG_MNUSERVICES - IDS_MSG_FIRST],
439 appendMenuItem(hMenu, 0, _T(""), FALSE, TRUE);
440 appendMenuItem(hMenu, IDM_EXIT,
441 g_lpMsg[IDS_MSG_MNUEXIT - IDS_MSG_FIRST],
444 if (!SetForegroundWindow(hWnd)) {
445 SetForegroundWindow(NULL);
448 TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
449 pt.x, pt.y, 0, hWnd, NULL);
455 void ShowTryServicesMenu(HWND hWnd)
457 /* create services list popup menu and submenus */
458 HMENU hMenu = CreatePopupMenu();
464 while (g_stServices[i].szServiceName != NULL)
466 appendServiceMenu(hMenu, i, g_stServices[i].szDisplayName,
467 g_stServices[i].dwPid != 0);
472 if (!SetForegroundWindow(hWnd)) {
473 SetForegroundWindow(NULL);
476 TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
477 pt.x, pt.y, 0, hWnd, NULL);
484 BOOL CenterWindow(HWND hwndChild)
486 RECT rChild, rWorkArea;
491 /* Get the Height and Width of the child window */
492 GetWindowRect(hwndChild, &rChild);
493 wChild = rChild.right - rChild.left;
494 hChild = rChild.bottom - rChild.top;
496 /* Get the limits of the 'workarea' */
497 bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT),
500 rWorkArea.left = rWorkArea.top = 0;
501 rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
502 rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
505 /* Calculate new X and Y position*/
506 xNew = (rWorkArea.right - wChild) / 2;
507 yNew = (rWorkArea.bottom - hChild) / 2;
508 return SetWindowPos(hwndChild, HWND_TOP, xNew, yNew, 0, 0,
509 SWP_NOSIZE | SWP_SHOWWINDOW);
513 static void addListBoxItem(HWND hDlg, LPTSTR lpStr, HBITMAP hBmp)
517 nItem = SendMessage(hDlg, LB_ADDSTRING, 0, (LPARAM)lpStr);
518 SendMessage(hDlg, LB_SETITEMDATA, nItem, (LPARAM)hBmp);
522 static void addListBoxString(HWND hListBox, LPTSTR lpStr)
524 static int nItems = 0;
525 if (!g_bDlgServiceOn) {
529 if (nItems > MAX_LOADSTRING)
531 SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
534 ListBox_SetCurSel(hListBox,
535 ListBox_AddString(hListBox, lpStr));
540 BOOL ApacheManageService(LPCTSTR szServiceName, LPCTSTR szImagePath,
541 LPTSTR szComputerName, DWORD dwCommand)
543 TCHAR szMsg[MAX_PATH];
545 BOOL serviceFlag = TRUE;
546 SC_HANDLE schService;
547 SC_HANDLE schSCManager;
548 SERVICE_STATUS schSStatus;
551 schSCManager = OpenSCManager(szComputerName, NULL,
554 ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED - IDS_MSG_FIRST],
559 schService = OpenService(schSCManager, szServiceName,
560 SERVICE_QUERY_STATUS | SERVICE_START |
561 SERVICE_STOP | SERVICE_USER_DEFINED_CONTROL);
562 if (schService == NULL)
564 /* Avoid recursion of ImagePath NULL (from this Respawn) */
566 am_RespawnAsUserAdmin(g_hwndMain, dwCommand,
567 szServiceName, szComputerName);
570 ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED - IDS_MSG_FIRST],
573 CloseServiceHandle(schSCManager);
579 g_bConsoleRun = TRUE;
580 SetCursor(g_hCursorHourglass);
583 case SERVICE_CONTROL_STOP:
584 _sntprintf(szMsg, sizeof(szMsg) / sizeof(TCHAR),
585 g_lpMsg[IDS_MSG_SRVSTOP - IDS_MSG_FIRST],
587 addListBoxString(g_hwndStdoutList, szMsg);
588 if (ControlService(schService, SERVICE_CONTROL_STOP,
591 while (QueryServiceStatus(schService, &schSStatus))
593 if (schSStatus.dwCurrentState == SERVICE_STOP_PENDING)
602 if (QueryServiceStatus(schService, &schSStatus))
604 if (schSStatus.dwCurrentState == SERVICE_STOPPED)
607 _sntprintf(szMsg, sizeof(szMsg) / sizeof(TCHAR),
608 g_lpMsg[IDS_MSG_SRVSTOPPED - IDS_MSG_FIRST],
610 addListBoxString(g_hwndStdoutList, szMsg);
615 case SERVICE_CONTROL_CONTINUE:
616 _sntprintf(szMsg, sizeof(szMsg) / sizeof(TCHAR),
617 g_lpMsg[IDS_MSG_SRVSTART - IDS_MSG_FIRST],
619 addListBoxString(g_hwndStdoutList, szMsg);
621 if (StartService(schService, 0, NULL))
624 while (QueryServiceStatus(schService, &schSStatus))
626 if (schSStatus.dwCurrentState == SERVICE_START_PENDING)
635 if (QueryServiceStatus(schService, &schSStatus))
637 if (schSStatus.dwCurrentState == SERVICE_RUNNING)
640 _sntprintf(szMsg, sizeof(szMsg) / sizeof(TCHAR),
641 g_lpMsg[IDS_MSG_SRVSTARTED - IDS_MSG_FIRST],
643 addListBoxString(g_hwndStdoutList, szMsg);
648 case SERVICE_APACHE_RESTART:
649 _sntprintf(szMsg, sizeof(szMsg) / sizeof(TCHAR),
650 g_lpMsg[IDS_MSG_SRVRESTART - IDS_MSG_FIRST],
652 addListBoxString(g_hwndStdoutList, szMsg);
653 if (ControlService(schService, SERVICE_APACHE_RESTART,
657 while (schSStatus.dwCurrentState == SERVICE_START_PENDING)
660 if (!QueryServiceStatus(schService, &schSStatus))
662 CloseServiceHandle(schService);
663 CloseServiceHandle(schSCManager);
664 g_bConsoleRun = FALSE;
665 SetCursor(g_hCursorArrow);
673 if (schSStatus.dwCurrentState == SERVICE_RUNNING)
676 _sntprintf(szMsg, sizeof(szMsg) / sizeof(TCHAR),
677 g_lpMsg[IDS_MSG_SRVRESTARTED - IDS_MSG_FIRST],
679 addListBoxString(g_hwndStdoutList, szMsg);
683 CloseServiceHandle(schService);
684 CloseServiceHandle(schSCManager);
686 ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED - IDS_MSG_FIRST],
689 g_bConsoleRun = FALSE;
690 SetCursor(g_hCursorArrow);
697 BOOL IsServiceRunning(LPCTSTR szServiceName, LPCTSTR szComputerName,
701 SC_HANDLE schService;
702 SC_HANDLE schSCManager;
703 SERVICE_STATUS schSStatus;
706 schSCManager = OpenSCManager(szComputerName, NULL,
712 schService = OpenService(schSCManager, szServiceName,
713 SERVICE_QUERY_STATUS);
714 if (schService != NULL)
716 if (QueryServiceStatus(schService, &schSStatus))
718 dwPid = schSStatus.dwCurrentState;
723 CloseServiceHandle(schService);
724 CloseServiceHandle(schSCManager);
725 return dwPid == SERVICE_RUNNING ? TRUE : FALSE;
728 g_bRescanServices = TRUE;
730 CloseServiceHandle(schSCManager);
735 BOOL FindRunningServices(void)
740 while (g_stServices[i].szServiceName != NULL)
742 if (!IsServiceRunning(g_stServices[i].szServiceName,
743 g_stServices[i].szComputerName, &dwPid)) {
746 if (g_stServices[i].dwPid != dwPid) {
749 g_stServices[i].dwPid = dwPid;
756 BOOL GetApacheServicesStatus()
758 TCHAR szKey[MAX_PATH];
759 TCHAR achKey[MAX_PATH];
760 TCHAR szImagePath[MAX_PATH];
761 TCHAR szBuf[MAX_PATH];
762 TCHAR szTmp[MAX_PATH];
763 HKEY hKey, hSubKey, hKeyRemote;
764 DWORD retCode, rv, dwKeyType;
765 DWORD dwBufLen = MAX_PATH;
769 g_bRescanServices = FALSE;
771 am_ClearServicesSt();
772 while (g_stComputers[computers].szComputerName != NULL) {
773 hKeyRemote = g_stComputers[computers].hRegistry;
774 retCode = RegOpenKeyEx(hKeyRemote,
775 _T("System\\CurrentControlSet\\Services\\"),
777 if (retCode != ERROR_SUCCESS)
779 ErrorMessage(NULL, FALSE);
782 for (i = 0, retCode = ERROR_SUCCESS; retCode == ERROR_SUCCESS; i++)
784 retCode = RegEnumKey(hKey, i, achKey, MAX_PATH);
785 if (retCode == ERROR_SUCCESS)
787 _tcscpy(szKey, _T("System\\CurrentControlSet\\Services\\"));
788 _tcscat(szKey, achKey);
790 if (RegOpenKeyEx(hKeyRemote, szKey, 0,
791 KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS)
794 rv = RegQueryValueEx(hSubKey, _T("ImagePath"), NULL,
795 &dwKeyType, (LPBYTE)szImagePath, &dwBufLen);
797 if (rv == ERROR_SUCCESS
798 && (dwKeyType == REG_SZ
799 || dwKeyType == REG_EXPAND_SZ)
802 _tcscpy(szBuf, szImagePath);
804 /* the service name could be httpd*.exe or Apache*.exe */
805 if (((_tcsstr(szBuf, _T("\\apache")) != NULL)
806 || (_tcsstr(szBuf, _T("\\httpd")) != NULL))
807 && _tcsstr(szBuf, _T(".exe"))
808 && (_tcsstr(szBuf, _T("--ntservice")) != NULL
809 || _tcsstr(szBuf, _T("-k ")) != NULL))
811 g_stServices[stPos].szServiceName = _tcsdup(achKey);
812 g_stServices[stPos].szImagePath = _tcsdup(szImagePath);
813 g_stServices[stPos].szComputerName =
814 _tcsdup(g_stComputers[computers].szComputerName);
816 if (RegQueryValueEx(hSubKey, _T("Description"), NULL,
817 &dwKeyType, (LPBYTE)szBuf, &dwBufLen)
819 g_stServices[stPos].szDescription = _tcsdup(szBuf);
822 if (RegQueryValueEx(hSubKey, _T("DisplayName"), NULL,
823 &dwKeyType, (LPBYTE)szBuf, &dwBufLen)
826 if (_tcscmp(g_stComputers[computers]
827 .szComputerName, g_szLocalHost) != 0)
829 _tcscpy(szTmp, g_stComputers[computers]
830 .szComputerName + 2);
831 _tcscat(szTmp, _T("@"));
832 _tcscat(szTmp, szBuf);
835 _tcscpy(szTmp, szBuf);
837 g_stServices[stPos].szDisplayName = _tcsdup(szTmp);
841 if (stPos >= MAX_APACHE_SERVICES) {
842 retCode = !ERROR_SUCCESS;
846 RegCloseKey(hSubKey);
853 FindRunningServices();
858 LRESULT CALLBACK ConnectDlgProc(HWND hDlg, UINT message,
859 WPARAM wParam, LPARAM lParam)
861 TCHAR szCmp[MAX_COMPUTERNAME_LENGTH+4];
865 ShowWindow(hDlg, SW_HIDE);
866 g_hwndConnectDlg = hDlg;
868 ShowWindow(hDlg, SW_SHOW);
869 SetFocus(GetDlgItem(hDlg, IDC_COMPUTER));
873 switch (LOWORD(wParam))
876 memset(szCmp, 0, sizeof(szCmp));
877 _tcscpy(szCmp, _T("\\\\"));
878 SendMessage(GetDlgItem(hDlg, IDC_COMPUTER), WM_GETTEXT,
879 (WPARAM) MAX_COMPUTERNAME_LENGTH,
883 if (_tcslen(szCmp) < 3) {
884 EndDialog(hDlg, TRUE);
887 am_ConnectComputer(szCmp);
888 SendMessage(g_hwndMain, WM_TIMER, WM_TIMER_RESCAN, 0);
891 EndDialog(hDlg, TRUE);
899 memset(&bi, 0, sizeof(BROWSEINFO));
900 SHGetSpecialFolderLocation(hDlg, CSIDL_NETWORK, &il);
902 bi.lpszTitle = _T("ApacheMonitor :\nSelect Network Computer!");
903 bi.pszDisplayName = szCmp;
905 bi.ulFlags = BIF_BROWSEFORCOMPUTER;
911 if (SHBrowseForFolder(&bi) != NULL) {
912 SendMessage(GetDlgItem(hDlg, IDC_COMPUTER),
914 (WPARAM) NULL, (LPARAM) szCmp);
916 if (SHGetMalloc(&pMalloc)) {
917 pMalloc->lpVtbl->Free(pMalloc, il);
918 pMalloc->lpVtbl->Release(pMalloc);
927 EndDialog(hDlg, TRUE);
938 LRESULT CALLBACK ServiceDlgProc(HWND hDlg, UINT message,
939 WPARAM wParam, LPARAM lParam)
941 TCHAR szBuf[MAX_PATH];
943 static HWND hStatusBar;
949 LPMEASUREITEMSTRUCT lpmis;
950 LPDRAWITEMSTRUCT lpdis;
952 memset(szBuf, 0, sizeof(szBuf));
956 ShowWindow(hDlg, SW_HIDE);
957 g_hwndServiceDlg = hDlg;
958 SetWindowText(hDlg, g_szTitle);
959 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
960 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
961 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
962 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
963 SetWindowText(GetDlgItem(hDlg, IDC_SSTART),
964 g_lpMsg[IDS_MSG_SSTART - IDS_MSG_FIRST]);
965 SetWindowText(GetDlgItem(hDlg, IDC_SSTOP),
966 g_lpMsg[IDS_MSG_SSTOP - IDS_MSG_FIRST]);
967 SetWindowText(GetDlgItem(hDlg, IDC_SRESTART),
968 g_lpMsg[IDS_MSG_SRESTART - IDS_MSG_FIRST]);
969 SetWindowText(GetDlgItem(hDlg, IDC_SMANAGER),
970 g_lpMsg[IDS_MSG_SERVICES - IDS_MSG_FIRST]);
971 SetWindowText(GetDlgItem(hDlg, IDC_SCONNECT),
972 g_lpMsg[IDS_MSG_CONNECT - IDS_MSG_FIRST]);
973 SetWindowText(GetDlgItem(hDlg, IDCANCEL),
974 g_lpMsg[IDS_MSG_OK - IDS_MSG_FIRST]);
975 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
976 g_hwndStdoutList = GetDlgItem(hDlg, IDL_STDOUT);
977 hStatusBar = CreateStatusWindow(0x0800 /* SBT_TOOLTIPS */
978 | WS_CHILD | WS_VISIBLE,
979 _T(""), hDlg, IDC_STATBAR);
980 if (GetApacheServicesStatus())
983 while (g_stServices[i].szServiceName != NULL)
985 addListBoxItem(hListBox, g_stServices[i].szDisplayName,
986 g_stServices[i].dwPid == 0 ? g_hBmpStop
992 ShowWindow(hDlg, SW_SHOW);
994 SendMessage(hListBox, LB_SETCURSEL, 0, 0);
998 case WM_MANAGEMESSAGE:
999 ApacheManageService(g_stServices[LOWORD(wParam)].szServiceName,
1000 g_stServices[LOWORD(wParam)].szImagePath,
1001 g_stServices[LOWORD(wParam)].szComputerName,
1007 case WM_UPDATEMESSAGE:
1008 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1009 SendMessage(hListBox, LB_RESETCONTENT, 0, 0);
1010 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)_T(""));
1011 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1012 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1013 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1014 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1016 while (g_stServices[i].szServiceName != NULL)
1018 addListBoxItem(hListBox, g_stServices[i].szDisplayName,
1019 g_stServices[i].dwPid == 0 ? g_hBmpStop : g_hBmpStart);
1022 SendMessage(hListBox, LB_SETCURSEL, 0, 0);
1023 /* Dirty hack to bring the window to the foreground */
1024 SetWindowPos(hDlg, HWND_TOPMOST, 0, 0, 0, 0,
1025 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1026 SetWindowPos(hDlg, HWND_NOTOPMOST, 0, 0, 0, 0,
1027 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1032 case WM_MEASUREITEM:
1033 lpmis = (LPMEASUREITEMSTRUCT) lParam;
1034 lpmis->itemHeight = YBITMAP;
1038 if (g_bConsoleRun) {
1039 SetCursor(g_hCursorHourglass);
1042 SetCursor(g_hCursorArrow);
1047 lpdis = (LPDRAWITEMSTRUCT) lParam;
1048 if (lpdis->itemID == -1) {
1051 switch (lpdis->itemAction)
1055 case ODA_DRAWENTIRE:
1056 g_hBmpPicture = (HBITMAP)SendMessage(lpdis->hwndItem,
1058 lpdis->itemID, (LPARAM) 0);
1060 hdcMem = CreateCompatibleDC(lpdis->hDC);
1061 g_hBmpOld = SelectObject(hdcMem, g_hBmpPicture);
1063 BitBlt(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
1064 lpdis->rcItem.right - lpdis->rcItem.left,
1065 lpdis->rcItem.bottom - lpdis->rcItem.top,
1066 hdcMem, 0, 0, SRCCOPY);
1067 SendMessage(lpdis->hwndItem, LB_GETTEXT,
1068 lpdis->itemID, (LPARAM) szBuf);
1070 GetTextMetrics(lpdis->hDC, &tm);
1071 y = (lpdis->rcItem.bottom + lpdis->rcItem.top - tm.tmHeight) / 2;
1073 SelectObject(hdcMem, g_hBmpOld);
1076 rcBitmap.left = lpdis->rcItem.left + XBITMAP + 2;
1077 rcBitmap.top = lpdis->rcItem.top;
1078 rcBitmap.right = lpdis->rcItem.right;
1079 rcBitmap.bottom = lpdis->rcItem.top + YBITMAP;
1081 if (lpdis->itemState & ODS_SELECTED)
1083 if (g_hBmpPicture == g_hBmpStop)
1085 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), TRUE);
1086 Button_SetStyle(GetDlgItem(hDlg, IDC_SSTART), BS_DEFPUSHBUTTON, TRUE);
1087 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1088 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1090 else if (g_hBmpPicture == g_hBmpStart)
1092 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1093 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), TRUE);
1094 Button_SetStyle(GetDlgItem(hDlg, IDC_SSTOP), BS_DEFPUSHBUTTON, TRUE);
1095 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), TRUE);
1098 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1099 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1100 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1102 if (_tcscmp(g_stServices[lpdis->itemID].szComputerName,
1103 g_szLocalHost) == 0) {
1104 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), FALSE);
1107 Button_Enable(GetDlgItem(hDlg, IDC_SDISCONN), TRUE);
1110 if (g_stServices[lpdis->itemID].szDescription) {
1111 SendMessage(hStatusBar, SB_SETTEXT, 0,
1112 (LPARAM)g_stServices[lpdis->itemID].szDescription);
1115 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)_T(""));
1117 if (lpdis->itemState & ODS_FOCUS) {
1118 SetTextColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
1119 SetBkColor(lpdis->hDC, GetSysColor(COLOR_HIGHLIGHT));
1120 FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_HIGHLIGHT+1));
1123 SetTextColor(lpdis->hDC, GetSysColor(COLOR_INACTIVECAPTIONTEXT));
1124 SetBkColor(lpdis->hDC, GetSysColor(COLOR_INACTIVECAPTION));
1125 FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_INACTIVECAPTION+1));
1130 SetTextColor(lpdis->hDC, GetSysColor(COLOR_MENUTEXT));
1131 SetBkColor(lpdis->hDC, GetSysColor(COLOR_WINDOW));
1132 FillRect(lpdis->hDC, &rcBitmap, (HBRUSH)(COLOR_WINDOW+1));
1134 TextOut(lpdis->hDC, XBITMAP + 6, y, szBuf, (int)_tcslen(szBuf));
1139 switch (LOWORD(wParam))
1142 switch (HIWORD(wParam))
1145 /* if started then stop, if stopped then start */
1146 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1147 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1148 if (nItem != LB_ERR)
1150 g_hBmpPicture = (HBITMAP)SendMessage(hListBox,
1153 if (g_hBmpPicture == g_hBmpStop) {
1154 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1155 SERVICE_CONTROL_CONTINUE);
1158 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1159 SERVICE_CONTROL_STOP);
1168 EndDialog(hDlg, TRUE);
1172 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), FALSE);
1173 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1174 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1175 if (nItem != LB_ERR) {
1176 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1177 SERVICE_CONTROL_CONTINUE);
1179 Button_Enable(GetDlgItem(hDlg, IDC_SSTART), TRUE);
1183 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), FALSE);
1184 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1185 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1186 if (nItem != LB_ERR) {
1187 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1188 SERVICE_CONTROL_STOP);
1190 Button_Enable(GetDlgItem(hDlg, IDC_SSTOP), TRUE);
1194 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), FALSE);
1195 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1196 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1197 if (nItem != LB_ERR) {
1198 SendMessage(hDlg, WM_MANAGEMESSAGE, nItem,
1199 SERVICE_APACHE_RESTART);
1201 Button_Enable(GetDlgItem(hDlg, IDC_SRESTART), TRUE);
1205 if (g_dwOSVersion >= OS_VERSION_WIN2K) {
1206 ShellExecute(hDlg, _T("open"), _T("services.msc"), _T("/s"),
1210 WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL);
1215 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGCONNECT),
1216 hDlg, (DLGPROC)ConnectDlgProc);
1220 hListBox = GetDlgItem(hDlg, IDL_SERVICES);
1221 nItem = SendMessage(hListBox, LB_GETCURSEL, 0, 0);
1222 if (nItem != LB_ERR) {
1223 am_DisconnectComputer(g_stServices[nItem].szComputerName);
1224 SendMessage(g_hwndMain, WM_TIMER, WM_TIMER_RESCAN, 0);
1231 switch (LOWORD(wParam))
1233 case SIZE_MINIMIZED:
1234 EndDialog(hDlg, TRUE);
1242 EndDialog(hDlg, TRUE);
1252 LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
1253 WPARAM wParam, LPARAM lParam)
1255 if (message == g_bUiTaskbarCreated)
1257 /* restore the tray icon on shell restart */
1258 ShowNotifyIcon(hWnd, NIM_ADD);
1259 return DefWindowProc(hWnd, message, wParam, lParam);
1264 GetApacheServicesStatus();
1265 ShowNotifyIcon(hWnd, NIM_ADD);
1266 SetTimer(hWnd, WM_TIMER_REFRESH, REFRESH_TIME, NULL);
1267 SetTimer(hWnd, WM_TIMER_RESCAN, RESCAN_TIME, NULL);
1273 case WM_TIMER_RESCAN:
1275 int nPrev = 0, nNew = 0;
1276 EnterCriticalSection(&g_stcSection);
1277 if (FindRunningServices() || g_bRescanServices)
1279 ShowNotifyIcon(hWnd, NIM_MODIFY);
1280 if (g_hwndServiceDlg)
1281 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1283 /* check if services list changed */
1284 while (g_stServices[nPrev].szServiceName != NULL)
1286 GetApacheServicesStatus();
1287 while (g_stServices[nNew].szServiceName != NULL)
1291 ShowNotifyIcon(hWnd, NIM_MODIFY);
1292 if (g_hwndServiceDlg) {
1293 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1296 LeaveCriticalSection(&g_stcSection);
1300 case WM_TIMER_REFRESH:
1302 int nPrev = 0, nNew = 0;
1303 EnterCriticalSection(&g_stcSection);
1304 if (g_bRescanServices)
1306 GetApacheServicesStatus();
1307 ShowNotifyIcon(hWnd, NIM_MODIFY);
1308 if (g_hwndServiceDlg) {
1309 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1312 else if (FindRunningServices())
1314 ShowNotifyIcon(hWnd, NIM_MODIFY);
1315 if (g_hwndServiceDlg) {
1316 PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0);
1319 LeaveCriticalSection(&g_stcSection);
1326 ShowNotifyIcon(hWnd, NIM_DELETE);
1329 case WM_TRAYMESSAGE:
1332 case WM_LBUTTONDBLCLK:
1333 if (!g_bDlgServiceOn)
1335 g_bDlgServiceOn = TRUE;
1336 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES),
1337 hWnd, (DLGPROC)ServiceDlgProc);
1338 g_bDlgServiceOn = FALSE;
1339 g_hwndServiceDlg = NULL;
1341 else if (IsWindow(g_hwndServiceDlg))
1343 /* Dirty hack to bring the window to the foreground */
1344 SetWindowPos(g_hwndServiceDlg, HWND_TOPMOST, 0, 0, 0, 0,
1345 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1346 SetWindowPos(g_hwndServiceDlg, HWND_NOTOPMOST, 0, 0, 0, 0,
1347 SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);
1348 SetFocus(g_hwndServiceDlg);
1353 ShowTryServicesMenu(hWnd);
1357 ShowTryPopupMenu(hWnd);
1363 if ((LOWORD(wParam) & IDM_SM_START) == IDM_SM_START)
1365 ApacheManageService(g_stServices[LOWORD(wParam)
1366 - IDM_SM_START].szServiceName,
1367 g_stServices[LOWORD(wParam)
1368 - IDM_SM_START].szImagePath,
1369 g_stServices[LOWORD(wParam)
1370 - IDM_SM_START].szComputerName,
1371 SERVICE_CONTROL_CONTINUE);
1374 else if ((LOWORD(wParam) & IDM_SM_STOP) == IDM_SM_STOP)
1376 ApacheManageService(g_stServices[LOWORD(wParam)
1377 - IDM_SM_STOP].szServiceName,
1378 g_stServices[LOWORD(wParam)
1379 - IDM_SM_STOP].szImagePath,
1380 g_stServices[LOWORD(wParam)
1381 - IDM_SM_STOP].szComputerName,
1382 SERVICE_CONTROL_STOP);
1385 else if ((LOWORD(wParam) & IDM_SM_RESTART) == IDM_SM_RESTART)
1387 ApacheManageService(g_stServices[LOWORD(wParam)
1388 - IDM_SM_RESTART].szServiceName,
1389 g_stServices[LOWORD(wParam)
1390 - IDM_SM_RESTART].szImagePath,
1391 g_stServices[LOWORD(wParam)
1392 - IDM_SM_RESTART].szComputerName,
1393 SERVICE_APACHE_RESTART);
1396 switch (LOWORD(wParam))
1399 if (!g_bDlgServiceOn)
1401 g_bDlgServiceOn = TRUE;
1402 DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES),
1403 hWnd, (DLGPROC)ServiceDlgProc);
1404 g_bDlgServiceOn = FALSE;
1405 g_hwndServiceDlg = NULL;
1407 else if (IsWindow(g_hwndServiceDlg)) {
1408 SetFocus(g_hwndServiceDlg);
1413 if (g_dwOSVersion >= OS_VERSION_WIN2K) {
1414 ShellExecute(NULL, _T("open"), _T("services.msc"), _T("/s"),
1418 WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL);
1423 ShowNotifyIcon(hWnd, NIM_DELETE);
1429 return DefWindowProc(hWnd, message, wParam, lParam);
1436 static int KillAWindow(HWND appwindow)
1443 GetWindowThreadProcessId(appwindow, &procid);
1447 appproc = OpenProcess(SYNCHRONIZE, 0, procid);
1448 postres = PostMessage(appwindow, WM_COMMAND, IDM_EXIT, 0);
1449 if (appproc && postres) {
1450 if (WaitForSingleObject(appproc, 10 /* seconds */ * 1000)
1452 CloseHandle(appproc);
1457 CloseHandle(appproc);
1459 if ((appproc = OpenProcess(PROCESS_TERMINATE, 0, procid)) != NULL) {
1460 if (TerminateProcess(appproc, 0)) {
1461 CloseHandle(appproc);
1464 CloseHandle(appproc);
1467 /* Perhaps we were short of permissions? */
1472 static int KillAllMonitors(void)
1476 PWTS_PROCESS_INFO tsProcs;
1477 DWORD tsProcCount, i;
1480 /* This is graceful, close our own Window, clearing the icon */
1481 if ((appwindow = FindWindow(g_szWindowClass, g_szTitle)) != NULL)
1482 exitcode = KillAWindow(appwindow);
1484 if (g_dwOSVersion < OS_VERSION_WIN2K)
1487 thisProcId = GetCurrentProcessId();
1489 if (!WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1,
1490 &tsProcs, &tsProcCount))
1493 /* This is ungraceful; close other Windows, with a lingering icon.
1494 * Since on terminal server it's not possible to post the message
1495 * to exit across sessions, we have to suffer this side effect
1496 * of a taskbar 'icon' which will evaporate the next time that
1497 * the user hovers over it or when the taskbar area is updated.
1499 for (i = 0; i < tsProcCount; ++i) {
1500 if (_tcscmp(tsProcs[i].pProcessName, _T(AM_STRINGIFY(BIN_NAME))) == 0
1501 && tsProcs[i].ProcessId != thisProcId)
1502 WTSTerminateProcess(WTS_CURRENT_SERVER_HANDLE,
1503 tsProcs[i].ProcessId, 1);
1505 WTSFreeMemory(tsProcs);
1510 /* Create main invisible window */
1511 HWND CreateMainWindow(HINSTANCE hInstance)
1516 wcex.cbSize = sizeof(WNDCLASSEX);
1518 wcex.style = CS_HREDRAW | CS_VREDRAW;
1519 wcex.lpfnWndProc = (WNDPROC)WndProc;
1520 wcex.cbClsExtra = 0;
1521 wcex.cbWndExtra = 0;
1522 wcex.hInstance = hInstance;
1523 wcex.hIcon = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON),
1524 IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
1525 wcex.hCursor = g_hCursorArrow;
1526 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
1527 wcex.lpszMenuName = 0;
1528 wcex.lpszClassName = g_szWindowClass;
1529 wcex.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON),
1530 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1532 if (RegisterClassEx(&wcex)) {
1533 hWnd = CreateWindow(g_szWindowClass, g_szTitle,
1535 NULL, NULL, hInstance, NULL);
1542 /* Borrowed from CRT internal.h for _MBCS argc/argv parsing in this GUI app */
1543 int __cdecl _setargv(void);
1546 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
1547 LPSTR lpCmdLine, int nCmdShow)
1549 TCHAR szTmp[MAX_LOADSTRING];
1550 TCHAR szCmp[MAX_COMPUTERNAME_LENGTH+4];
1552 /* existing window */
1558 if (!GetSystemOSVersion(&g_dwOSVersion))
1560 ErrorMessage(NULL, TRUE);
1564 g_LangID = GetUserDefaultLangID();
1565 if ((g_LangID & 0xFF) != LANG_ENGLISH) {
1566 g_LangID = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL);
1568 for (i = IDS_MSG_FIRST; i <= IDS_MSG_LAST; ++i) {
1569 LoadString(hInstance, i, szTmp, MAX_LOADSTRING);
1570 g_lpMsg[i - IDS_MSG_FIRST] = _tcsdup(szTmp);
1572 LoadString(hInstance, IDS_APMONITORTITLE, szTmp, MAX_LOADSTRING);
1573 d = MAX_COMPUTERNAME_LENGTH+1;
1574 _tcscpy(szCmp, _T("\\\\"));
1575 GetComputerName(szCmp + 2, &d);
1577 g_szLocalHost = _tcsdup(szCmp);
1579 memset(g_stComputers, 0, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);
1580 g_stComputers[0].szComputerName = _tcsdup(szCmp);
1581 g_stComputers[0].hRegistry = HKEY_LOCAL_MACHINE;
1582 g_szTitle = _tcsdup(szTmp);
1583 LoadString(hInstance, IDS_APMONITORCLASS, szTmp, MAX_LOADSTRING);
1584 g_szWindowClass = _tcsdup(szTmp);
1586 appwindow = FindWindow(g_szWindowClass, g_szTitle);
1589 __wargv = CommandLineToArgvW(GetCommandLineW(), &__argc);
1594 if ((__argc == 2) && (_tcscmp(__targv[1], _T("--kill")) == 0))
1596 /* Off to chase and close up every ApacheMonitor taskbar window */
1597 return KillAllMonitors();
1599 else if ((__argc == 4) && (g_dwOSVersion >= OS_VERSION_WIN2K))
1601 dwControl = _ttoi(__targv[1]);
1602 if ((dwControl != SERVICE_CONTROL_CONTINUE) &&
1603 (dwControl != SERVICE_APACHE_RESTART) &&
1604 (dwControl != SERVICE_CONTROL_STOP))
1609 /* Chase down and close up our session's previous window */
1610 if ((appwindow) != NULL)
1611 KillAWindow(appwindow);
1613 else if (__argc != 1) {
1618 ErrorMessage(g_lpMsg[IDS_MSG_APPRUNNING - IDS_MSG_FIRST], FALSE);
1622 g_icoStop = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICOSTOP),
1623 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1624 g_icoRun = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICORUN),
1625 IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
1626 g_hCursorHourglass = LoadImage(NULL, MAKEINTRESOURCE(OCR_WAIT),
1627 IMAGE_CURSOR, LR_DEFAULTSIZE,
1628 LR_DEFAULTSIZE, LR_SHARED);
1629 g_hCursorArrow = LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL),
1630 IMAGE_CURSOR, LR_DEFAULTSIZE,
1631 LR_DEFAULTSIZE, LR_SHARED);
1632 g_hBmpStart = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPRUN),
1633 IMAGE_BITMAP, XBITMAP, YBITMAP,
1635 g_hBmpStop = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPSTOP),
1636 IMAGE_BITMAP, XBITMAP, YBITMAP,
1639 memset(g_stServices, 0, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);
1641 InitCommonControls();
1642 g_hInstance = hInstance;
1643 g_hwndMain = CreateMainWindow(hInstance);
1644 g_bUiTaskbarCreated = RegisterWindowMessage(_T("TaskbarCreated"));
1645 InitializeCriticalSection(&g_stcSection);
1646 g_hwndServiceDlg = NULL;
1647 if (g_hwndMain != NULL)
1649 /* To avoid recursion, pass ImagePath NULL (a noop on NT and later) */
1650 if ((__argc == 4) && (g_dwOSVersion >= OS_VERSION_WIN2K))
1651 ApacheManageService(__targv[2], NULL, __targv[3], dwControl);
1653 while (GetMessage(&msg, NULL, 0, 0) == TRUE)
1655 TranslateMessage(&msg);
1656 DispatchMessage(&msg);
1658 am_ClearServicesSt();
1660 am_ClearComputersSt();
1661 DeleteCriticalSection(&g_stcSection);
1662 DestroyIcon(g_icoStop);
1663 DestroyIcon(g_icoRun);
1664 DestroyCursor(g_hCursorHourglass);
1665 DestroyCursor(g_hCursorArrow);
1666 DeleteObject(g_hBmpStart);
1667 DeleteObject(g_hBmpStop);