#include "stdafx.h"
#include
int main()
{
/*
函数建立了一个到服务控制管理器的连接,并打开指定的数据库。
_Must_inspect_result_ WINADVAPI SC_HANDLE WINAPI OpenSCManagerW(
_In_opt_ LPCWSTR lpMachineName,
_In_opt_ LPCWSTR lpDatabaseName,
_In_ DWORD dwDesiredAccess
);
如果函数成功,返回值是一个指定的服务控制管理器数据库的句柄。
如果函数失败,返回值为NULL 。
要获得更详细的错误信息,可以使用GetLastError 获得错误代码。
dwDesiredAccess可以是下列值
#define SC_MANAGER_CONNECT 0x0001
#define SC_MANAGER_CREATE_SERVICE 0x0002
#define SC_MANAGER_ENUMERATE_SERVICE 0x0004
#define SC_MANAGER_LOCK 0x0008
#define SC_MANAGER_QUERY_LOCK_STATUS 0x0010
#define SC_MANAGER_MODIFY_BOOT_CONFIG 0x0020
#define SC_MANAGER_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | \
SC_MANAGER_CONNECT | \
SC_MANAGER_CREATE_SERVICE | \
SC_MANAGER_ENUMERATE_SERVICE | \
SC_MANAGER_LOCK | \
SC_MANAGER_QUERY_LOCK_STATUS | \
SC_MANAGER_MODIFY_BOOT_CONFIG)
*/
SC_HANDLE scHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
if (scHandle == NULL) {
printf("OpenSCManager fail(%ld)", GetLastError());
}
else {
printf("OpenSCManager -> scHandle=%p\n", scHandle);
/*
枚举当前系统服务
_Must_inspect_result_
WINADVAPI BOOL WINAPI EnumServicesStatusExW(
_In_ SC_HANDLE hSCManager,
_In_ SC_ENUM_TYPE InfoLevel,
_In_ DWORD dwServiceType,
_In_ DWORD dwServiceState,
_Out_writes_bytes_opt_(cbBufSize)
LPBYTE lpServices,
数组的最大大小是256K字节。为了获取实际需要的大小,我们可以将此形参赋值为NULL,同时将下一个形参cbBufSize赋值为0,pcbBytesNeeded返回的就是实际的数组大小,同时函数调用失败并返回ERROR_MORE_DATA。XP和2003下,数组的最大大小是64K 字节
_In_ DWORD cbBufSize,
_Out_ LPDWORD pcbBytesNeeded,
_Out_ LPDWORD lpServicesReturned,
_Inout_opt_ LPDWORD lpResumeHandle,
_In_opt_ LPCWSTR pszGroupName
);
InfoLevel只能是如下该值
typedef enum _SC_ENUM_TYPE {
SC_ENUM_PROCESS_INFO = 0
} SC_ENUM_TYPE;
dwServiceType可以取如下值
#define SERVICE_KERNEL_DRIVER 0x00000001
#define SERVICE_FILE_SYSTEM_DRIVER 0x00000002
#define SERVICE_ADAPTER 0x00000004
#define SERVICE_RECOGNIZER_DRIVER 0x00000008
#define SERVICE_DRIVER (SERVICE_KERNEL_DRIVER | \
SERVICE_FILE_SYSTEM_DRIVER | \
SERVICE_RECOGNIZER_DRIVER)
#define SERVICE_WIN32_OWN_PROCESS 0x00000010
#define SERVICE_WIN32_SHARE_PROCESS 0x00000020
#define SERVICE_WIN32 (SERVICE_WIN32_OWN_PROCESS | \
SERVICE_WIN32_SHARE_PROCESS)
#define SERVICE_INTERACTIVE_PROCESS 0x00000100
#define SERVICE_TYPE_ALL (SERVICE_WIN32 | \
SERVICE_ADAPTER | \
SERVICE_DRIVER | \
SERVICE_INTERACTIVE_PROCESS)
dwServiceState可以取如下值
#define SERVICE_ACTIVE 0x00000001
#define SERVICE_INACTIVE 0x00000002
#define SERVICE_STATE_ALL (SERVICE_ACTIVE | SERVICE_INACTIVE)
成功返回非0,失败返回0
*/
SC_ENUM_TYPE infoLevel = SC_ENUM_PROCESS_INFO;
DWORD dwServiceType = SERVICE_WIN32;
DWORD dwServiceState = SERVICE_STATE_ALL;
LPBYTE lpServices = NULL;
DWORD cbBufSize = 0;
DWORD pcbBytesNeeded;
DWORD servicesReturned;
LPDWORD lpResumeHandle = NULL;
LPCWSTR pszGroupName = NULL;
BOOL ret = EnumServicesStatusEx(scHandle, infoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, &pcbBytesNeeded, &servicesReturned, lpResumeHandle, pszGroupName);
printf("EnumServicesStatusEx scHandle=%p -> ret=%d, pcbBytesNeeded=%ld, servicesReturned=%ld\n", scHandle, ret, pcbBytesNeeded, servicesReturned);
cbBufSize = pcbBytesNeeded;
lpServices = new BYTE[cbBufSize];
if (NULL == lpServices)
{
printf("lpServices = new BYTE[%ld] -> fail(%ld)\n", cbBufSize, GetLastError());
}
else {
ret = EnumServicesStatusEx(scHandle, infoLevel, dwServiceType, dwServiceState, lpServices, cbBufSize, &pcbBytesNeeded, &servicesReturned, lpResumeHandle, pszGroupName);
printf("EnumServicesStatusEx scHandle=%p, cbBufSize=%ld -> ret=%d, pcbBytesNeeded=%ld, servicesReturned=%ld\n", scHandle, cbBufSize, ret, pcbBytesNeeded, servicesReturned);
/*
typedef struct _ENUM_SERVICE_STATUS_PROCESSW {
LPWSTR lpServiceName;
LPWSTR lpDisplayName;
SERVICE_STATUS_PROCESS ServiceStatusProcess;
} ENUM_SERVICE_STATUS_PROCESSW, *LPENUM_SERVICE_STATUS_PROCESSW;
typedef struct _SERVICE_STATUS_PROCESS {
DWORD dwServiceType;
DWORD dwCurrentState;
DWORD dwControlsAccepted;
DWORD dwWin32ExitCode;
DWORD dwServiceSpecificExitCode;
DWORD dwCheckPoint;
DWORD dwWaitHint;
DWORD dwProcessId;
DWORD dwServiceFlags;
} SERVICE_STATUS_PROCESS, *LPSERVICE_STATUS_PROCESS;
dwCurrentState可以是如下值
#define SERVICE_STOPPED 0x00000001
#define SERVICE_START_PENDING 0x00000002
#define SERVICE_STOP_PENDING 0x00000003
#define SERVICE_RUNNING 0x00000004
#define SERVICE_CONTINUE_PENDING 0x00000005
#define SERVICE_PAUSE_PENDING 0x00000006
#define SERVICE_PAUSED 0x00000007
dwServiceFlags可以是0或下列值
#define SERVICE_RUNS_IN_SYSTEM_PROCESS 0x00000001
*/
LPENUM_SERVICE_STATUS_PROCESS lpServiceStatusProcess = (LPENUM_SERVICE_STATUS_PROCESS)lpServices;
for (DWORD i = 0; i < servicesReturned; i++) {
_tprintf(_T("service.lpServiceName=%s, lpDisplayName=%s\n"), lpServiceStatusProcess[i].lpDisplayName, lpServiceStatusProcess[i].lpServiceName);
printf("service.ServiceStatusProcess.dwServiceType=%ld, dwCurrentState=%ld, dwControlsAccepted=%ld, dwWin32ExitCode=%ld, dwServiceSpecificExitCode=%ld, dwCheckPoint=%ld, dwWaitHint=%ld, dwProcessId=%ld, dwServiceFlags=%ld\n",
lpServiceStatusProcess[i].ServiceStatusProcess.dwServiceType,
lpServiceStatusProcess[i].ServiceStatusProcess.dwCurrentState,
lpServiceStatusProcess[i].ServiceStatusProcess.dwControlsAccepted,
lpServiceStatusProcess[i].ServiceStatusProcess.dwWin32ExitCode,
lpServiceStatusProcess[i].ServiceStatusProcess.dwServiceSpecificExitCode,
lpServiceStatusProcess[i].ServiceStatusProcess.dwCheckPoint,
lpServiceStatusProcess[i].ServiceStatusProcess.dwWaitHint,
lpServiceStatusProcess[i].ServiceStatusProcess.dwProcessId,
lpServiceStatusProcess[i].ServiceStatusProcess.dwServiceFlags);
}
delete[] lpServices;
}
CloseServiceHandle(scHandle);
}
system("pause");
return 0;
}