Win32 サービスの構成パラメータを取得する QueryServiceConfig

Windowsサービスの構成パラメータを取得するには、QueryServiceConfig関数を用いる。


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

BOOL QueryServiceConfig(
  SC_HANDLE hService,                     // サービスのハンドル
  LPQUERY_SERVICE_CONFIG lpServiceConfig, // バッファ
  DWORD cbBufSize,                        // バッファのサイズ
  LPDWORD pcbBytesNeeded                  // 必要なバイト数
);


  • Netlogonサービスの構成パラメータを取得するサンプルプログラム
  • #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
        SC_HANDLE hSCM;
        SC_HANDLE hService;
        LPQUERY_SERVICE_CONFIG lpServiceConfig;
        DWORD dwBytesNeeded;
        LPCTSTR lpszServiceName = TEXT("Netlogon");
    
        hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
        if (hSCM == NULL) {
            return 1;
        }
    
        hService = OpenService(hSCM, lpszServiceName, SERVICE_QUERY_CONFIG);
        if (hService == NULL) {
            return 1;//goto
        }
    
        //構成パラメータを格納するのに必要なメモリサイズを取得する
        QueryServiceConfig(hService, NULL, 0, &dwBytesNeeded);
    
        lpServiceConfig = (LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR, dwBytesNeeded);
        if (lpServiceConfig == NULL) {
            return 1;//goto
        }
    
        QueryServiceConfig(hService, lpServiceConfig, dwBytesNeeded, &dwBytesNeeded);
    
        wprintf(L"BinaryPathName: %s\n", lpServiceConfig->lpBinaryPathName);
        wprintf(L"DisplayName: %s\n", lpServiceConfig->lpDisplayName);
        wprintf(L"ServiceStartName: %s\n", lpServiceConfig->lpServiceStartName);
    
        LocalFree(lpServiceConfig);
        CloseServiceHandle(hService);
        CloseServiceHandle(hSCM);
    
        return 0;
    }


  • 実行結果
  • BinaryPathName: C:\Windows\system32\lsass.exe
    DisplayName: Netlogon
    ServiceStartName: LocalSystem



  • 参考

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