📄 apachemonitor.c
字号:
while (g_stServices[nNew].szServiceName != NULL) ++nNew; if (nPrev != nNew) { ShowNotifyIcon(hWnd, NIM_MODIFY); if (g_hwndServiceDlg) { PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0); } } LeaveCriticalSection(&g_stcSection); break; } case WM_TIMER_REFRESH: { int nPrev = 0, nNew = 0; EnterCriticalSection(&g_stcSection); if (g_bRescanServices) { GetApacheServicesStatus(); ShowNotifyIcon(hWnd, NIM_MODIFY); if (g_hwndServiceDlg) { PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0); } } else if (FindRunningServices()) { ShowNotifyIcon(hWnd, NIM_MODIFY); if (g_hwndServiceDlg) { PostMessage(g_hwndServiceDlg, WM_UPDATEMESSAGE, 0, 0); } } LeaveCriticalSection(&g_stcSection); break; } } break; case WM_QUIT: ShowNotifyIcon(hWnd, NIM_DELETE); break; case WM_TRAYMESSAGE: switch (lParam) { case WM_LBUTTONDBLCLK: if (!g_bDlgServiceOn) { g_bDlgServiceOn = TRUE; DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES), hWnd, (DLGPROC)ServiceDlgProc); g_bDlgServiceOn = FALSE; g_hwndServiceDlg = NULL; } else if (IsWindow(g_hwndServiceDlg)) { /* Dirty hack to bring the window to the foreground */ SetWindowPos(g_hwndServiceDlg, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW); SetWindowPos(g_hwndServiceDlg, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW); SetFocus(g_hwndServiceDlg); } break; case WM_LBUTTONUP: ShowTryServicesMenu(hWnd); break; case WM_RBUTTONUP: ShowTryPopupMenu(hWnd); break; } break; case WM_COMMAND: if ((LOWORD(wParam) & IDM_SM_START) == IDM_SM_START) { ApacheManageService(g_stServices[LOWORD(wParam) - IDM_SM_START].szServiceName, g_stServices[LOWORD(wParam) - IDM_SM_START].szImagePath, g_stServices[LOWORD(wParam) - IDM_SM_START].szComputerName, SERVICE_CONTROL_CONTINUE); return TRUE; } else if ((LOWORD(wParam) & IDM_SM_STOP) == IDM_SM_STOP) { ApacheManageService(g_stServices[LOWORD(wParam) - IDM_SM_STOP].szServiceName, g_stServices[LOWORD(wParam) - IDM_SM_STOP].szImagePath, g_stServices[LOWORD(wParam) - IDM_SM_STOP].szComputerName, SERVICE_CONTROL_STOP); return TRUE; } else if ((LOWORD(wParam) & IDM_SM_RESTART) == IDM_SM_RESTART) { ApacheManageService(g_stServices[LOWORD(wParam) - IDM_SM_RESTART].szServiceName, g_stServices[LOWORD(wParam) - IDM_SM_RESTART].szImagePath, g_stServices[LOWORD(wParam) - IDM_SM_RESTART].szComputerName, SERVICE_APACHE_RESTART); return TRUE; } switch (LOWORD(wParam)) { case IDM_RESTORE: if (!g_bDlgServiceOn) { g_bDlgServiceOn = TRUE; DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_DLGSERVICES), hWnd, (DLGPROC)ServiceDlgProc); g_bDlgServiceOn = FALSE; g_hwndServiceDlg = NULL; } else if (IsWindow(g_hwndServiceDlg)) { SetFocus(g_hwndServiceDlg); } break; case IDC_SMANAGER: if (g_dwOSVersion >= OS_VERSION_WIN2K) { ShellExecute(NULL, _T("open"), _T("services.msc"), _T("/s"), NULL, SW_NORMAL); } else { WinExec("Control.exe SrvMgr.cpl Services", SW_NORMAL); } return TRUE; case IDM_EXIT: ShowNotifyIcon(hWnd, NIM_DELETE); PostQuitMessage(0); return TRUE; } default: return DefWindowProc(hWnd, message, wParam, lParam); } return FALSE;}static int KillAWindow(HWND appwindow){ HANDLE appproc; DWORD procid; BOOL postres; SetLastError(0); GetWindowThreadProcessId(appwindow, &procid); if (GetLastError()) return(2); appproc = OpenProcess(SYNCHRONIZE, 0, procid); postres = PostMessage(appwindow, WM_COMMAND, IDM_EXIT, 0); if (appproc && postres) { if (WaitForSingleObject(appproc, 10 /* seconds */ * 1000) == WAIT_OBJECT_0) { CloseHandle(appproc); return (0); } } if (appproc) CloseHandle(appproc); if ((appproc = OpenProcess(PROCESS_TERMINATE, 0, procid)) != NULL) { if (TerminateProcess(appproc, 0)) { CloseHandle(appproc); return (0); } CloseHandle(appproc); } /* Perhaps we were short of permissions? */ return (2);}static int KillAllMonitors(void){ HWND appwindow; int exitcode = 0; PWTS_PROCESS_INFO tsProcs; DWORD tsProcCount, i; DWORD thisProcId; /* This is graceful, close our own Window, clearing the icon */ if ((appwindow = FindWindow(g_szWindowClass, g_szTitle)) != NULL) exitcode = KillAWindow(appwindow); if (g_dwOSVersion < OS_VERSION_WIN2K) return exitcode; thisProcId = GetCurrentProcessId(); if (!WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &tsProcs, &tsProcCount)) return exitcode; /* This is ungraceful; close other Windows, with a lingering icon. * Since on terminal server it's not possible to post the message * to exit across sessions, we have to suffer this side effect * of a taskbar 'icon' which will evaporate the next time that * the user hovers over it or when the taskbar area is updated. */ for (i = 0; i < tsProcCount; ++i) { if (_tcscmp(tsProcs[i].pProcessName, _T(AM_STRINGIFY(BIN_NAME))) == 0 && tsProcs[i].ProcessId != thisProcId) WTSTerminateProcess(WTS_CURRENT_SERVER_HANDLE, tsProcs[i].ProcessId, 1); } WTSFreeMemory(tsProcs); return exitcode;}/* Create main invisible window */HWND CreateMainWindow(HINSTANCE hInstance){ HWND hWnd = NULL; WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR); wcex.hCursor = g_hCursorArrow; wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = 0; wcex.lpszClassName = g_szWindowClass; wcex.hIconSm = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_APSRVMON), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); if (RegisterClassEx(&wcex)) { hWnd = CreateWindow(g_szWindowClass, g_szTitle, 0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL); } return hWnd;}#ifndef UNICODE/* Borrowed from CRT internal.h for _MBCS argc/argv parsing in this GUI app */int __cdecl _setargv(void);#endifint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ TCHAR szTmp[MAX_LOADSTRING]; TCHAR szCmp[MAX_COMPUTERNAME_LENGTH+4]; MSG msg; /* existing window */ HWND appwindow; DWORD dwControl; int i; DWORD d; if (!GetSystemOSVersion(&g_dwOSVersion)) { ErrorMessage(NULL, TRUE); return 1; } g_LangID = GetUserDefaultLangID(); if ((g_LangID & 0xFF) != LANG_ENGLISH) { g_LangID = MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL); } for (i = IDS_MSG_FIRST; i <= IDS_MSG_LAST; ++i) { LoadString(hInstance, i, szTmp, MAX_LOADSTRING); g_lpMsg[i - IDS_MSG_FIRST] = _tcsdup(szTmp); } LoadString(hInstance, IDS_APMONITORTITLE, szTmp, MAX_LOADSTRING); d = MAX_COMPUTERNAME_LENGTH+1; _tcscpy(szCmp, _T("\\\\")); GetComputerName(szCmp + 2, &d); _tcsupr(szCmp); g_szLocalHost = _tcsdup(szCmp); memset(g_stComputers, 0, sizeof(ST_MONITORED_COMP) * MAX_APACHE_COMPUTERS); g_stComputers[0].szComputerName = _tcsdup(szCmp); g_stComputers[0].hRegistry = HKEY_LOCAL_MACHINE; g_szTitle = _tcsdup(szTmp); LoadString(hInstance, IDS_APMONITORCLASS, szTmp, MAX_LOADSTRING); g_szWindowClass = _tcsdup(szTmp); appwindow = FindWindow(g_szWindowClass, g_szTitle);#ifdef UNICODE __wargv = CommandLineToArgvW(GetCommandLineW(), &__argc);#else _setargv();#endif if ((__argc == 2) && (_tcscmp(__targv[1], _T("--kill")) == 0)) { /* Off to chase and close up every ApacheMonitor taskbar window */ return KillAllMonitors(); } else if ((__argc == 4) && (g_dwOSVersion >= OS_VERSION_WIN2K)) { dwControl = _ttoi(__targv[1]); if ((dwControl != SERVICE_CONTROL_CONTINUE) && (dwControl != SERVICE_APACHE_RESTART) && (dwControl != SERVICE_CONTROL_STOP)) { return 1; } /* Chase down and close up our session's previous window */ if ((appwindow) != NULL) KillAWindow(appwindow); } else if (__argc != 1) { return 1; } else if (appwindow) { ErrorMessage(g_lpMsg[IDS_MSG_APPRUNNING - IDS_MSG_FIRST], FALSE); return 0; } g_icoStop = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICOSTOP), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); g_icoRun = LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICORUN), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR); g_hCursorHourglass = LoadImage(NULL, MAKEINTRESOURCE(OCR_WAIT), IMAGE_CURSOR, LR_DEFAULTSIZE, LR_DEFAULTSIZE, LR_SHARED); g_hCursorArrow = LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR, LR_DEFAULTSIZE, LR_DEFAULTSIZE, LR_SHARED); g_hBmpStart = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPRUN), IMAGE_BITMAP, XBITMAP, YBITMAP, LR_DEFAULTCOLOR); g_hBmpStop = LoadImage(hInstance, MAKEINTRESOURCE(IDB_BMPSTOP), IMAGE_BITMAP, XBITMAP, YBITMAP, LR_DEFAULTCOLOR); memset(g_stServices, 0, sizeof(ST_APACHE_SERVICE) * MAX_APACHE_SERVICES); CoInitialize(NULL); InitCommonControls(); g_hInstance = hInstance; g_hwndMain = CreateMainWindow(hInstance); g_bUiTaskbarCreated = RegisterWindowMessage(_T("TaskbarCreated")); InitializeCriticalSection(&g_stcSection); g_hwndServiceDlg = NULL; if (g_hwndMain != NULL) { /* To avoid recursion, pass ImagePath NULL (a noop on NT and later) */ if ((__argc == 4) && (g_dwOSVersion >= OS_VERSION_WIN2K)) ApacheManageService(__targv[2], NULL, __targv[3], dwControl); while (GetMessage(&msg, NULL, 0, 0) == TRUE) { TranslateMessage(&msg); DispatchMessage(&msg); } am_ClearServicesSt(); } am_ClearComputersSt(); DeleteCriticalSection(&g_stcSection); DestroyIcon(g_icoStop); DestroyIcon(g_icoRun); DestroyCursor(g_hCursorHourglass); DestroyCursor(g_hCursorArrow); DeleteObject(g_hBmpStart); DeleteObject(g_hBmpStop); CoUninitialize(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -