WIn32API Windowsサービスの依存関係を調査する EnumDependentServices

指定したWindowsサービスに依存しているWindowsサービス(システムコンポーネント)を列挙するには、EnumDependentServices関数を用いる。


EnumDependentServices関数のプロトタイプは、以下のとおり。

BOOL EnumDependentServices(
  SC_HANDLE hService,               // サービスのハンドル
  DWORD dwServiceState,             // サービスの状態
  LPENUM_SERVICE_STATUS lpServices, // ステータスバッファ
  DWORD cbBufSize,                  // ステータスバッファのサイズ
  LPDWORD pcbBytesNeeded,           // 必要なバッファサイズ
  LPDWORD lpServicesReturned        // 返されたエントリの数
);


EnumDependentServices関数は、「コントロールパネル」→「サービス」に列挙されている各Windowsサービスの「プロパティ」ダイアログ→「依存関係」タブに表示されている「このサービスに依存しているシステムコンポーネント」のリストを取得する。以下のプロパティダイアログは、「EventSystem」サービスのもの。


  • EnumDependentServices関数で「EventSystem」サービスに依存しているWindowsサービス(システムコンポーネント)を列挙する
  • #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
        SC_HANDLE hSCM;
        SC_HANDLE hService;
        LPENUM_SERVICE_STATUS lpEnumServiceStatus;
        DWORD dwBytesNeeded;
        DWORD dwServicesReturned;
        DWORD i;
        LPCTSTR lpszServiceName = TEXT("LanmanWorkstation");
    
        hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE);
        if (hSCM == NULL) {
            return 1;
        }
    
        hService = OpenService(hSCM, lpszServiceName, SERVICE_ENUMERATE_DEPENDENTS);
        if (hService == NULL) {
            return 1;//goto
        }
    
        
        EnumDependentServices(
              hService,
              SERVICE_STATE_ALL,
              NULL,
              0,
              &dwBytesNeeded,
              &dwServicesReturned);
    
    
        lpEnumServiceStatus = (LPENUM_SERVICE_STATUS)LocalAlloc(LPTR, dwBytesNeeded);
        if (lpEnumServiceStatus == NULL) {
            return 1;//goto
        }
    
        EnumDependentServices(
              hService,
              SERVICE_STATE_ALL,
              lpEnumServiceStatus,
              dwBytesNeeded,
              &dwBytesNeeded,
              &dwServicesReturned);
    
        _tprintf(TEXT("Dependent services of the %s\n\n"), lpszServiceName);
    
        for (i = 0; i < dwServicesReturned; i++) {
    
            WCHAR *lpCurrentState;
            switch (lpEnumServiceStatus[i].ServiceStatus.dwCurrentState) {
                case SERVICE_STOPPED:
                    lpCurrentState = L"SERVICE_STOPPED";
                    break;
    
                case SERVICE_START_PENDING:
                    lpCurrentState = L"SERVICE_START_PENDING";
                    break;
    
                case SERVICE_STOP_PENDING:
                    lpCurrentState = L"SERVICE_STOP_PENDING";
                    break;
    
                case SERVICE_RUNNING:
                    lpCurrentState = L"SERVICE_RUNNING";
                    break;
    
                case SERVICE_CONTINUE_PENDING:
                    lpCurrentState = L"SERVICE_CONTINUE_PENDING";
                    break;
    
                case SERVICE_PAUSE_PENDING:
                    lpCurrentState = L"SERVICE_PAUSE_PENDING";
                    break;
    
                case SERVICE_PAUSED:
                    lpCurrentState = L"SERVICE_PAUSED";
                    break;
    
                default:
                    lpCurrentState = L"Unkown State";
                    break;
            }
    
            wprintf(L"------------------------------------------\n");
            wprintf(L"DisplayName: %s\n", lpEnumServiceStatus[i].lpDisplayName);
            wprintf(L"ServiceName: %s\n", lpEnumServiceStatus[i].lpServiceName);
            wprintf(L"CurrentState: %s\n", lpCurrentState);
            wprintf(L"\n");
    
        }
    
        LocalFree(lpEnumServiceStatus);
        CloseServiceHandle(hService);
        CloseServiceHandle(hSCM);
    
        return 0;
    }


  • 実行結果
  • Dependent services of the EventSystem
    
    ------------------------------------------
    DisplayName: SPP Notification Service
    ServiceName: sppuinotify
    CurrentState: SERVICE_STOPPED
    
    ------------------------------------------
    DisplayName: COM+ System Application
    ServiceName: COMSysApp
    CurrentState: SERVICE_STOPPED
    
    ------------------------------------------
    DisplayName: System Event Notification Service
    ServiceName: SENS
    CurrentState: SERVICE_RUNNING
    
    ------------------------------------------
    DisplayName: Background Intelligent Transfer Service
    ServiceName: BITS
    CurrentState: SERVICE_RUNNING
    



  • 参考

  • http://msdn.microsoft.com/ja-jp/library/cc429096.aspx