C/C++ Windows API——枚举服务

// EnumServiceDemo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 

int main()
{
    /*
    函数建立了一个到服务控制管理器的连接,并打开指定的数据库。
    _Must_inspect_result_ WINADVAPI SC_HANDLE WINAPI OpenSCManagerW(
        _In_opt_ LPCWSTR    lpMachineName,  //指向零终止字符串,指定目标计算机的名称。如果该指针为NULL ,或者它指向一个空字符串,那么该函数连接到本地计算机上的服务控制管理器。
        _In_opt_ LPCWSTR    lpDatabaseName, //指向零终止字符串,指定将要打开的服务控制管理数据库的名称。此字符串应被置为 SERVICES_ACTIVE_DATABASE。如果该指针为NULL ,则打开默认的 SERVICES_ACTIVE_DATABASE数据库。
        _In_     DWORD      dwDesiredAccess //指定服务访问控制管理器的权限。在授予要求的权限前,系统会检查调用进程的权限令牌,该令牌针对与服务控制管理器相关的安全描述符的权限控制列表。此外,该函数的调用将隐式地指定SC_MANAGER_CONNECT 的访问权限。
    );
    如果函数成功,返回值是一个指定的服务控制管理器数据库的句柄。
    如果函数失败,返回值为NULL 。
    要获得更详细的错误信息,可以使用GetLastError 获得错误代码。

    dwDesiredAccess可以是下列值
    // Service Control Manager object specific access types
    #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,          //返回哪种服务属性。目前只定义了一种SC_ENUM_PROCESS_INFO,用来获取名字和服务状态信息
            _In_            DWORD                   dwServiceType,
            _In_            DWORD                   dwServiceState,
            _Out_writes_bytes_opt_(cbBufSize)
                            LPBYTE                  lpServices,         //缓冲区指针,用于接收状态信息。数据的格式取决于第二个形参InfoLevel的值。
数组的最大大小是256K字节。为了获取实际需要的大小,我们可以将此形参赋值为NULL,同时将下一个形参cbBufSize赋值为0,pcbBytesNeeded返回的就是实际的数组大小,同时函数调用失败并返回ERROR_MORE_DATA。XP和2003下,数组的最大大小是64K 字节
            _In_            DWORD                   cbBufSize,
            _Out_           LPDWORD                 pcbBytesNeeded,     //如果缓冲区太小,该指针指向的变量存储返回剩余服务信息所需要的字节大小。
            _Out_           LPDWORD                 lpServicesReturned, //指向实际返回的服务的个数的指针
            _Inout_opt_     LPDWORD                 lpResumeHandle,     //一个指向变量的指针。在输入上,指定枚举的开始点;在第一次调用EnumServicesStatusEx函数时,指针指向的变量必须设置为0。在输出上,如果函数成功,那么指向的变量的值为0。但是,如果函数返回0,且GetLastError函数得到了ERROR_MORE_DATA错误码,则这个值表示下一个服务的开始点,可以用作下次枚举lpResumeHandle的值。
            _In_opt_        LPCWSTR                 pszGroupName        //服务组名字。如果该形参是一个字符串,那么只枚举指定服务组的服务。如果形参为空字符串"",那么只枚举不属于任何服务组的服务。如果形参为NULL,那么枚举所有服务。
        );

        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);
        //由于此时空间不足,因此实际返回的服务数为0(即servicesReturned=0)
        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;
}

C/C++ Windows API——枚举服务_第1张图片

你可能感兴趣的:(——,Windows,API)