📄 apachemonitor.c
字号:
/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* ==================================================================== * ApacheMonitor.c Simple program to manage and monitor Apache services. * * Contributed by Mladen Turk <mturk mappingsoft.com> * * 05 Aug 2001 * ==================================================================== */#define _WIN32_WINNT 0x0500#ifndef STRICT#define STRICT#endif#ifndef OEMRESOURCE#define OEMRESOURCE#endif#if defined(_MSC_VER) && _MSC_VER >= 1400#define _CRT_SECURE_NO_DEPRECATE#endif#include <windows.h>#include <windowsx.h>#include <commctrl.h>#include <objbase.h>#include <shlobj.h>#include <stdlib.h>#include <stdio.h>#include <WtsApi32.h>#include <tchar.h>#include "ApacheMonitor.h"#ifndef AM_STRINGIFY/** Properly quote a value as a string in the C preprocessor */#define AM_STRINGIFY(n) AM_STRINGIFY_HELPER(n)/** Helper macro for AM_STRINGIFY */#define AM_STRINGIFY_HELPER(n) #n#endif#define OS_VERSION_WIN9X 1#define OS_VERSION_WINNT 2#define OS_VERSION_WIN2K 3/* Should be enough */#define MAX_APACHE_SERVICES 128#define MAX_APACHE_COMPUTERS 32#define WM_TRAYMESSAGE (WM_APP+1)#define WM_UPDATEMESSAGE (WM_USER+1)#define WM_MANAGEMESSAGE (WM_USER+2)#define WM_TIMER_REFRESH 10#define WM_TIMER_RESCAN 11#define SERVICE_APACHE_RESTART 128#define XBITMAP 16#define YBITMAP 16#define MAX_LOADSTRING 100#define REFRESH_TIME 2000 /* service refresh time (ms) */#define RESCAN_TIME 20000 /* registry rescan time (ms) */typedef struct _st_APACHE_SERVICE{ LPTSTR szServiceName; LPTSTR szDisplayName; LPTSTR szDescription; LPTSTR szImagePath; LPTSTR szComputerName; DWORD dwPid;} ST_APACHE_SERVICE;typedef struct _st_MONITORED_COMPUTERS{ LPTSTR szComputerName; HKEY hRegistry;} ST_MONITORED_COMP;/* Global variables */HINSTANCE g_hInstance = NULL;TCHAR *g_szTitle; /* The title bar text */TCHAR *g_szWindowClass; /* Window Class Name */HICON g_icoStop;HICON g_icoRun;UINT g_bUiTaskbarCreated;DWORD g_dwOSVersion;BOOL g_bDlgServiceOn = FALSE;BOOL g_bConsoleRun = FALSE;ST_APACHE_SERVICE g_stServices[MAX_APACHE_SERVICES];ST_MONITORED_COMP g_stComputers[MAX_APACHE_COMPUTERS];HBITMAP g_hBmpStart, g_hBmpStop;HBITMAP g_hBmpPicture, g_hBmpOld;BOOL g_bRescanServices;HWND g_hwndServiceDlg;HWND g_hwndMain;HWND g_hwndStdoutList;HWND g_hwndConnectDlg;HCURSOR g_hCursorHourglass;HCURSOR g_hCursorArrow;HANDLE g_hpipeOutRead;HANDLE g_hpipeOutWrite;HANDLE g_hpipeInRead;HANDLE g_hpipeInWrite;HANDLE g_hpipeStdError;LANGID g_LangID;PROCESS_INFORMATION g_lpRedirectProc;CRITICAL_SECTION g_stcSection;LPTSTR g_szLocalHost;/* locale language support */static TCHAR *g_lpMsg[IDS_MSG_LAST - IDS_MSG_FIRST + 1];void am_ClearServicesSt(){ int i; for (i = 0; i < MAX_APACHE_SERVICES; i++) { if (g_stServices[i].szServiceName) { free(g_stServices[i].szServiceName); } if (g_stServices[i].szDisplayName) { free(g_stServices[i].szDisplayName); } if (g_stServices[i].szDescription) { free(g_stServices[i].szDescription); } if (g_stServices[i].szImagePath) { free(g_stServices[i].szImagePath); } if (g_stServices[i].szComputerName) { free(g_stServices[i].szComputerName); } } memset(g_stServices, 0, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES);}void am_ClearComputersSt(){ int i; for (i = 0; i < MAX_APACHE_COMPUTERS; i++) { if (g_stComputers[i].szComputerName) { free(g_stComputers[i].szComputerName); RegCloseKey(g_stComputers[i].hRegistry); } } memset(g_stComputers, 0, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS);}BOOL am_IsComputerConnected(LPTSTR szComputerName){ int i = 0; while (g_stComputers[i].szComputerName != NULL) { if (_tcscmp(g_stComputers[i].szComputerName, szComputerName) == 0) { return TRUE; } ++i; } return FALSE;}void am_DisconnectComputer(LPTSTR szComputerName){ int i = 0, j; while (g_stComputers[i].szComputerName != NULL) { if (_tcscmp(g_stComputers[i].szComputerName, szComputerName) == 0) { break; } ++i; } if (g_stComputers[i].szComputerName != NULL) { free(g_stComputers[i].szComputerName); RegCloseKey(g_stComputers[i].hRegistry); for (j = i; j < MAX_APACHE_COMPUTERS - 1; j++) { g_stComputers[j].szComputerName= g_stComputers[j+1].szComputerName; g_stComputers[j].hRegistry = g_stComputers[j+1].hRegistry; } g_stComputers[j].szComputerName = NULL; g_stComputers[j].hRegistry = NULL; }}void ErrorMessage(LPCTSTR szError, BOOL bFatal){ LPVOID lpMsgBuf = NULL; if (szError) { MessageBox(NULL, szError, g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST], MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION)); } else { FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), g_LangID, (LPTSTR) &lpMsgBuf, 0, NULL); MessageBox(NULL, (LPCTSTR)lpMsgBuf, g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST], MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION)); LocalFree(lpMsgBuf); } if (bFatal) { PostQuitMessage(0); }}int am_RespawnAsUserAdmin(HWND hwnd, DWORD op, LPCTSTR szService, LPCTSTR szComputerName){ TCHAR args[MAX_PATH + MAX_COMPUTERNAME_LENGTH + 12]; if (g_dwOSVersion < OS_VERSION_WIN2K) { ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED - IDS_MSG_FIRST], FALSE); return 0; } _sntprintf(args, sizeof(args) / sizeof(TCHAR), _T("%d \"%s\" \"%s\""), op, szService, szComputerName ? szComputerName : _T("")); if (!ShellExecute(hwnd, _T("runas"), __targv[0], args, NULL, SW_NORMAL)) { ErrorMessage(g_lpMsg[IDS_MSG_SRVFAILED - IDS_MSG_FIRST], FALSE); return 0; } return 1;}BOOL am_ConnectComputer(LPTSTR szComputerName){ int i = 0; HKEY hKeyRemote; TCHAR szTmp[MAX_PATH]; while (g_stComputers[i].szComputerName != NULL) { if (_tcscmp(g_stComputers[i].szComputerName, szComputerName) == 0) { return FALSE; } ++i; } if (i > MAX_APACHE_COMPUTERS - 1) { return FALSE; } if (RegConnectRegistry(szComputerName, HKEY_LOCAL_MACHINE, &hKeyRemote) != ERROR_SUCCESS) { _sntprintf(szTmp, sizeof(szTmp) / sizeof(TCHAR), g_lpMsg[IDS_MSG_ECONNECT - IDS_MSG_FIRST], szComputerName); ErrorMessage(szTmp, FALSE); return FALSE; } else { g_stComputers[i].szComputerName = _tcsdup(szComputerName); g_stComputers[i].hRegistry = hKeyRemote; return TRUE; }}LPTSTR GetStringRes(int id){ static TCHAR buffer[MAX_PATH]; buffer[0] = 0; LoadString(GetModuleHandle(NULL), id, buffer, MAX_PATH); return buffer;}BOOL GetSystemOSVersion(LPDWORD dwVersion){ OSVERSIONINFO osvi; /* Try calling GetVersionEx using the OSVERSIONINFOEX structure. If that fails, try using the OSVERSIONINFO structure. */ memset(&osvi, 0, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if (!GetVersionEx(&osvi)) { return FALSE; } switch (osvi.dwPlatformId) { case VER_PLATFORM_WIN32_NT: if (osvi.dwMajorVersion >= 5) *dwVersion = OS_VERSION_WIN2K; else *dwVersion = OS_VERSION_WINNT; break; case VER_PLATFORM_WIN32_WINDOWS: *dwVersion = OS_VERSION_WIN9X; break; case VER_PLATFORM_WIN32s: default: *dwVersion = 0; return FALSE; } return TRUE;}static VOID ShowNotifyIcon(HWND hWnd, DWORD dwMessage){ NOTIFYICONDATA nid; int i = 0, n = 0; memset(&nid, 0, sizeof(nid)); nid.cbSize = sizeof(NOTIFYICONDATA); nid.hWnd = hWnd; nid.uID = 0xFF; nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; nid.uCallbackMessage = WM_TRAYMESSAGE; while (g_stServices[i].szServiceName != NULL) { if (g_stServices[i].dwPid != 0) { ++n; } ++i; } if (dwMessage != NIM_DELETE) { if (n) { nid.hIcon = g_icoRun; } else { nid.hIcon = g_icoStop; } } else { nid.hIcon = NULL; } if (n == i && n > 0) { _tcscpy(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGALL - IDS_MSG_FIRST]); } else if (n) { _sntprintf(nid.szTip, sizeof(nid.szTip) / sizeof(TCHAR), g_lpMsg[IDS_MSG_RUNNING - IDS_MSG_FIRST], n, i); } else if (i) { _sntprintf(nid.szTip, sizeof(nid.szTip) / sizeof(TCHAR), g_lpMsg[IDS_MSG_RUNNINGNONE - IDS_MSG_FIRST], i); } else { _tcscpy(nid.szTip, g_lpMsg[IDS_MSG_NOSERVICES - IDS_MSG_FIRST]); } Shell_NotifyIcon(dwMessage, &nid);}void appendMenuItem(HMENU hMenu, UINT uMenuId, LPTSTR szName, BOOL fDefault, BOOL fEnabled){ MENUITEMINFO mii; memset(&mii, 0, sizeof(MENUITEMINFO)); mii.cbSize = sizeof(MENUITEMINFO); mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE; if (_tcslen(szName)) { mii.fType = MFT_STRING; mii.wID = uMenuId; if (fDefault) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -