📄 apachemonitor.c
字号:
/* Copyright 2001-2005 The Apache Software Foundation or its licensors, as * applicable. * * Licensed 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 0x0400#ifndef STRICT#define STRICT#endif#ifndef OEMRESOURCE#define OEMRESOURCE#endif#include <windows.h>#include <windowsx.h>#include <commctrl.h>#include <objbase.h>#include <shlobj.h>#include <stdlib.h>#include <stdio.h>#include "ApacheMonitor.h"#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{ LPSTR szServiceName; LPSTR szDisplayName; LPSTR szDescription; LPSTR szImagePath; LPSTR szComputerName; DWORD dwPid;} ST_APACHE_SERVICE;typedef struct _st_MONITORED_COMPUTERS{ LPSTR szComputerName; HKEY hRegistry;} ST_MONITORED_COMP;/* Global variables */HINSTANCE g_hInstance = NULL;CHAR *g_szTitle; /* The title bar text */CHAR *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;LPSTR g_szLocalHost;/* locale language support */static CHAR *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(LPSTR szComputerName){ int i = 0; while (g_stComputers[i].szComputerName != NULL) { if (strcmp(g_stComputers[i].szComputerName, szComputerName) == 0) { return TRUE; } ++i; } return FALSE;}void am_DisconnectComputer(LPSTR szComputerName){ int i = 0, j; while (g_stComputers[i].szComputerName != NULL) { if (strcmp(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[i].szComputerName= g_stComputers[i+1].szComputerName; g_stComputers[i].hRegistry = g_stComputers[i+1].hRegistry; } for (i = j; i < MAX_APACHE_COMPUTERS; i++) { g_stComputers[i].szComputerName = NULL; g_stComputers[i].hRegistry = NULL; } } }void ErrorMessage(LPCSTR 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, (LPSTR) &lpMsgBuf, 0, NULL); MessageBox(NULL, (LPCSTR)lpMsgBuf, g_lpMsg[IDS_MSG_ERROR - IDS_MSG_FIRST], MB_OK | (bFatal ? MB_ICONERROR : MB_ICONEXCLAMATION)); LocalFree(lpMsgBuf); } if (bFatal) { PostQuitMessage(0); }}BOOL am_ConnectComputer(LPSTR szComputerName){ int i = 0; HKEY hKeyRemote; char szTmp[MAX_PATH]; while (g_stComputers[i].szComputerName != NULL) { if (strcmp(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) { sprintf(szTmp, g_lpMsg[IDS_MSG_ECONNECT - IDS_MSG_FIRST], szComputerName); ErrorMessage(szTmp, FALSE); return FALSE; } else { g_stComputers[i].szComputerName = strdup(szComputerName); g_stComputers[i].hRegistry = hKeyRemote; return TRUE; }} LPSTR GetStringRes(int id){ static CHAR 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 <= 4) { *dwVersion = OS_VERSION_WINNT; } else if (osvi.dwMajorVersion == 5) { *dwVersion = OS_VERSION_WIN2K; } else { return FALSE; } 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) { lstrcpy(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGALL - IDS_MSG_FIRST]); } else if (n) { sprintf(nid.szTip, g_lpMsg[IDS_MSG_RUNNING - IDS_MSG_FIRST], n, i); } else if (i) { sprintf(nid.szTip, g_lpMsg[IDS_MSG_RUNNINGNONE - IDS_MSG_FIRST], i); } else { lstrcpy(nid.szTip, g_lpMsg[IDS_MSG_NOSERVICES - IDS_MSG_FIRST]); } Shell_NotifyIcon(dwMessage, &nid);}void appendMenuItem(HMENU hMenu, UINT uMenuId, LPSTR 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 (lstrlen(szName)) { mii.fType = MFT_STRING; mii.wID = uMenuId; if (fDefault) { mii.fState = MFS_DEFAULT; } if (!fEnabled) { mii.fState |= MFS_DISABLED; } mii.dwTypeData = szName; } else { mii.fType = MFT_SEPARATOR; } InsertMenuItem(hMenu, uMenuId, FALSE, &mii);}void appendServiceMenu(HMENU hMenu, UINT uMenuId, LPSTR szServiceName, BOOL fRunning){ MENUITEMINFO mii; HMENU smh; smh = CreatePopupMenu(); appendMenuItem(smh, IDM_SM_START + uMenuId, g_lpMsg[IDS_MSG_SSTART - IDS_MSG_FIRST], FALSE, !fRunning); appendMenuItem(smh, IDM_SM_STOP + uMenuId, g_lpMsg[IDS_MSG_SSTOP - IDS_MSG_FIRST], FALSE, fRunning); appendMenuItem(smh, IDM_SM_RESTART + uMenuId, g_lpMsg[IDS_MSG_SRESTART - IDS_MSG_FIRST], FALSE, fRunning); memset(&mii, 0, sizeof(MENUITEMINFO)); mii.cbSize = sizeof(MENUITEMINFO); mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_SUBMENU | MIIM_CHECKMARKS; mii.fType = MFT_STRING; mii.wID = uMenuId; mii.hbmpChecked = g_hBmpStart; mii.hbmpUnchecked = g_hBmpStop; mii.dwTypeData = szServiceName; mii.hSubMenu = smh; mii.fState = fRunning ? MFS_CHECKED : MFS_UNCHECKED; InsertMenuItem(hMenu, IDM_SM_SERVICE + uMenuId, FALSE, &mii);}void ShowTryPopupMenu(HWND hWnd){ /* create popup menu */ HMENU hMenu = CreatePopupMenu(); POINT pt; if (hMenu) { appendMenuItem(hMenu, IDM_RESTORE, g_lpMsg[IDS_MSG_MNUSHOW - IDS_MSG_FIRST], TRUE, TRUE); if (g_dwOSVersion >= OS_VERSION_WINNT) { appendMenuItem(hMenu, IDC_SMANAGER, g_lpMsg[IDS_MSG_MNUSERVICES - IDS_MSG_FIRST], FALSE, TRUE); } appendMenuItem(hMenu, 0, "", FALSE, TRUE); appendMenuItem(hMenu, IDM_EXIT, g_lpMsg[IDS_MSG_MNUEXIT - IDS_MSG_FIRST], FALSE, TRUE); if (!SetForegroundWindow(hWnd)) { SetForegroundWindow(NULL); } GetCursorPos(&pt); TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, NULL); DestroyMenu(hMenu); }}void ShowTryServicesMenu(HWND hWnd){ /* create services list popup menu and submenus */ HMENU hMenu = CreatePopupMenu(); POINT pt; int i = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -