枚舉Windows系統服務,通過進程ID取服務名,枚舉系統服務
//枚舉Windows系統服務
//使用到的函數以及MSDN的說明如下:
//1、OpenSCManager說明
//http://msdn.microsoft.com/en-us/library/windows/desktop/ms684323(v=vs.85).aspx
//2、EnumServicesStatusEx說明
//http://msdn.microsoft.com/en-us/library/windows/desktop/ms682640(v=vs.85).aspx
//3、CloseServiceHandle說明
//http://msdn.microsoft.com/en-us/library/windows/desktop/ms682028(v=vs.85).aspx
// 測試代碼:
#include <stdio.h>#include <stdlib.h>#include <tchar.h>#include <windows.h>int main(int argc, char *argv[]){LONG lRet = 0;BOOL bRet = FALSE;SC_HANDLE hSCM = NULL; // 服務資料庫控制代碼char *pBuf = NULL; // 緩衝區指標DWORD dwBufSize = 0; // 傳入的緩衝長度DWORD dwBufNeed = 0; // 需要的緩衝長度DWORD dwNumberOfService = 0; // 返回的服務個數ENUM_SERVICE_STATUS_PROCESS *pServiceInfo = NULL; // 服務資訊// 建立了一個到服務控制管理員的串連,並開啟指定的資料庫hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE | SC_MANAGER_CONNECT);if(NULL == hSCM){printf("OpenSCManager error.\n");return -1;}// 擷取需要的緩衝區大小EnumServicesStatusEx(hSCM, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, dwBufSize, &dwBufNeed, &dwNumberOfService, NULL, NULL);// 多設定存放1個服務資訊的長度dwBufSize = dwBufNeed + sizeof(ENUM_SERVICE_STATUS_PROCESS);pBuf = (char *)malloc(dwBufSize);if(NULL == pBuf){printf("malloc error.\n");return -1;}memset(pBuf, 0, dwBufSize);// 擷取服務資訊bRet = EnumServicesStatusEx(hSCM, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, (LPBYTE)pBuf, dwBufSize, &dwBufNeed, &dwNumberOfService, NULL, NULL);if(bRet == FALSE){printf("EnumServicesStatusEx error.\n");::CloseServiceHandle(hSCM);free(pBuf);return -1;}// 關閉開啟的服務控制代碼bRet = ::CloseServiceHandle(hSCM);if(bRet == FALSE){printf("CloseServiceHandle error.\n");}printf("Service Num:%d\n", dwNumberOfService);pServiceInfo = (LPENUM_SERVICE_STATUS_PROCESS)pBuf;// 列印取得的服務資訊for(unsigned int i = 0; i < dwNumberOfService; i++){printf("----------%d----------\n", i);printf("DisplayName \t\t : %s \n", pServiceInfo[i].lpDisplayName);printf("ServiceName \t\t : %s \n", pServiceInfo[i].lpServiceName);printf("ServiceType \t\t : %d \n", pServiceInfo[i].ServiceStatusProcess.dwServiceType);printf("CurrentState \t\t : %d \n", pServiceInfo[i].ServiceStatusProcess.dwCurrentState);printf("ControlsAccepted \t : %d \n", pServiceInfo[i].ServiceStatusProcess.dwControlsAccepted);printf("Win32ExitCode \t\t : %d \n", pServiceInfo[i].ServiceStatusProcess.dwWin32ExitCode);printf("ServiceSpecificExitCode : %d \n", pServiceInfo[i].ServiceStatusProcess.dwServiceSpecificExitCode);printf("CheckPoint \t\t : %d \n", pServiceInfo[i].ServiceStatusProcess.dwCheckPoint);printf("WaitHint \t\t : %d \n", pServiceInfo[i].ServiceStatusProcess.dwWaitHint);printf("Process Id \t\t : %d \n", pServiceInfo[i].ServiceStatusProcess.dwProcessId);printf("ServiceFlags \t\t : %d \n", pServiceInfo[i].ServiceStatusProcess.dwServiceFlags);}free(pBuf);system("PAUSE");return 0;}
// 既然能夠擷取到所有的服務資訊,
// 那麼根據進程ID查詢該進程是否為服務,取得服務名等一系列的操作就可以按自己的需求來完成了
// 擷取進程ID GetCurrentProcessId()
怎擷取windows系統服務的ID
我的電腦右擊屬性——上面有電腦名稱!
windows2003在命令列下怎簡單枚舉所有系統服務?
net start
----------------------------
我只看見你說“是否有其它命令”,沒想到你還是在說SC。
很簡單!
REM==========================================
REM 啟動狀態為RUNNING
@echo off
sc query |find "DISPLAY_NAME" > 1.txt
for /f "tokens=2,* delims= " %%i in (1.txt) do echo %%i %%j
del 1.txt
pause
REM==========================================
如果說是你想要列出所有狀態,就用下面的替換
sc query state= all |find "DISPLAY_NAME"