📄 apachemonitor.c
字号:
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, LPTSTR 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, _T(""), 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; if (hMenu) { while (g_stServices[i].szServiceName != NULL) { appendServiceMenu(hMenu, i, g_stServices[i].szDisplayName, g_stServices[i].dwPid != 0); ++i; } if (i) { if (!SetForegroundWindow(hWnd)) { SetForegroundWindow(NULL); } GetCursorPos(&pt); TrackPopupMenu(hMenu, TPM_LEFTALIGN|TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, NULL); DestroyMenu(hMenu); } }}BOOL CenterWindow(HWND hwndChild){ RECT rChild, rWorkArea; int wChild, hChild; int xNew, yNew; BOOL bResult; /* Get the Height and Width of the child window */ GetWindowRect(hwndChild, &rChild); wChild = rChild.right - rChild.left; hChild = rChild.bottom - rChild.top; /* Get the limits of the 'workarea' */ bResult = SystemParametersInfo(SPI_GETWORKAREA, sizeof(RECT), &rWorkArea, 0); if (!bResult) { rWorkArea.left = rWorkArea.top = 0; rWorkArea.right = GetSystemMetrics(SM_CXSCREEN); rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN); } /* Calculate new X and Y position*/ xNew = (rWorkArea.right - wChild) / 2; yNew = (rWorkArea.bottom - hChild) / 2; return SetWindowPos(hwndChild, HWND_TOP, xNew, yNew, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);}static void addListBoxItem(HWND hDlg, LPTSTR lpStr, HBITMAP hBmp){ LRESULT nItem; nItem = SendMessage(hDlg, LB_ADDSTRING, 0, (LPARAM)lpStr); SendMessage(hDlg, LB_SETITEMDATA, nItem, (LPARAM)hBmp);}static void addListBoxString(HWND hListBox, LPTSTR lpStr){ static int nItems = 0; if (!g_bDlgServiceOn) { return; } ++nItems; if (nItems > MAX_LOADSTRING) { SendMessage(hListBox, LB_RESETCONTENT, 0, 0); nItems = 1; } ListBox_SetCurSel(hListBox, ListBox_AddString(hListBox, lpStr));}#ifndef UNICODE#define addListBoxStringA addListBoxString#elsestatic void addListBoxStringA(HWND hListBox, LPSTR lpStr){ static int nItems = 0; TCHAR WStr[16384]; if (!g_bDlgServiceOn) { return; } if (!MultiByteToWideChar(CP_ACP, 0, lpStr, (int)strlen(lpStr) + 1, WStr, (int) (sizeof(WStr) / sizeof(TCHAR)))) return; ++nItems; if (nItems > MAX_LOADSTRING) { SendMessage(hListBox, LB_RESETCONTENT, 0, 0); nItems = 1; } ListBox_SetCurSel(hListBox, ListBox_AddString(hListBox, WStr));}#endifstatic DWORD WINAPI ConsoleOutputThread(LPVOID lpThreadParameter){ static BYTE lpBuffer[MAX_PATH+1]; int nPtr = 0; BYTE ch; DWORD dwReaded; while (ReadFile(g_hpipeOutRead, &ch, 1, &dwReaded, NULL) == TRUE) { if (dwReaded > 0) { if (ch == '\n' || nPtr >= MAX_PATH) { lpBuffer[nPtr] = '\0'; addListBoxStringA(g_hwndStdoutList, lpBuffer); nPtr = 0; } else if (ch == '\t' && nPtr < (MAX_PATH - 4)) { int i; for (i = 0; i < 4; ++i) { lpBuffer[nPtr++] = ' '; } } else if (ch != '\r') { lpBuffer[nPtr++] = ch; } } } CloseHandle(g_hpipeInWrite); CloseHandle(g_hpipeOutRead); CloseHandle(g_hpipeStdError); return 0;}DWORD WINAPI ConsoleWaitingThread(LPVOID lpThreadParameter){ WaitForSingleObject(g_lpRedirectProc.hThread, INFINITE); CloseHandle(g_lpRedirectProc.hThread); MessageBeep(100); g_bConsoleRun = FALSE; SetCursor(g_hCursorArrow); return 0;}BOOL RunRedirectedConsole(LPTSTR szCmdLine){ DWORD dwThreadId; HANDLE hProc; STARTUPINFO stInfo; BOOL bResult; memset(&stInfo, 0, sizeof(stInfo)); stInfo.cb = sizeof(stInfo); stInfo.dwFlags = STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW; stInfo.wShowWindow = SW_HIDE; hProc = GetCurrentProcess(); if (!CreatePipe(&g_hpipeInRead, &g_hpipeInWrite, NULL, MAX_PATH)) { ErrorMessage(NULL, TRUE); } if (!CreatePipe(&g_hpipeOutRead, &g_hpipeOutWrite, NULL, MAX_PATH*8)) { ErrorMessage(NULL, TRUE); } DuplicateHandle(hProc, g_hpipeInRead, hProc, &g_hpipeInRead, 0, TRUE, DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS); DuplicateHandle(hProc, g_hpipeOutWrite, hProc, &g_hpipeOutWrite, 0, TRUE, DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS); DuplicateHandle(hProc, g_hpipeOutWrite, hProc, &g_hpipeStdError, 0, TRUE, DUPLICATE_SAME_ACCESS); if (!g_hpipeInRead && !g_hpipeOutWrite && !g_hpipeStdError) { ErrorMessage(NULL, TRUE); } stInfo.hStdInput = g_hpipeInRead; stInfo.hStdOutput = g_hpipeOutWrite; stInfo.hStdError = g_hpipeStdError; bResult = CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, NULL, &stInfo, &g_lpRedirectProc); CloseHandle(g_hpipeInRead); CloseHandle(g_hpipeOutWrite); CloseHandle(g_hpipeStdError); if (!bResult) { CloseHandle(g_hpipeInWrite); CloseHandle(g_hpipeOutRead); CloseHandle(g_hpipeStdError); return FALSE; } CloseHandle(CreateThread(NULL, 0, ConsoleOutputThread, 0, 0, &dwThreadId)); ResumeThread(g_lpRedirectProc.hThread); CloseHandle(CreateThread(NULL, 0, ConsoleWaitingThread, 0, 0, &dwThreadId)); return TRUE;}BOOL RunAndForgetConsole(LPTSTR szCmdLine, BOOL bRedirectConsole){ STARTUPINFO stInfo; PROCESS_INFORMATION prInfo; BOOL bResult; if (bRedirectConsole) { return RunRedirectedConsole(szCmdLine); } memset(&stInfo, 0, sizeof(stInfo)); stInfo.cb = sizeof(stInfo); stInfo.dwFlags = STARTF_USESHOWWINDOW; stInfo.wShowWindow = SW_HIDE; bResult = CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &stInfo, &prInfo); if (!bResult) { return FALSE; } if (g_dwOSVersion == OS_VERSION_WIN9X) { /* give some time to rescan the status */ Sleep(2000); } CloseHandle(prInfo.hThread); CloseHandle(prInfo.hProcess); return TRUE;}BOOL ApacheManageService(LPCTSTR szServiceName, LPCTSTR szImagePath, LPTSTR szComputerName, DWORD dwCommand){ TCHAR szBuf[MAX_PATH]; TCHAR szMsg[MAX_PATH]; LPTSTR sPos; BOOL retValue; BOOL serviceFlag = TRUE; SC_HANDLE schService; SC_HANDLE schSCManager; SERVICE_STATUS schSStatus; int ticks; if (g_dwOSVersion == OS_VERSION_WIN9X) { sPos = _tcsstr(szImagePath, _T("-k start")); if (sPos) { _tcsncpy(szBuf, szImagePath, (int)(sPos - szImagePath)); switch (dwCommand) { case SERVICE_CONTROL_STOP: _tcscat(szBuf, _T(" -k shutdown -n ")); break; case SERVICE_CONTROL_CONTINUE: _sntprintf(szMsg, sizeof(szMsg) / sizeof(TCHAR), g_lpMsg[IDS_MSG_SRVSTART - IDS_MSG_FIRST], szServiceName); addListBoxString(g_hwndStdoutList, szMsg); _tcscat(szBuf, _T(" -k start -n ")); serviceFlag = FALSE; break; case SERVICE_APACHE_RESTART: _tcscat(szBuf, _T(" -k restart -n ")); break; default: return FALSE; } _tcscat(szBuf, szServiceName); } else { return FALSE; } g_bConsoleRun = TRUE; SetCursor(g_hCursorHourglass); if (!RunAndForgetConsole(szBuf, serviceFlag)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -