获取Windows服务的配置信息

procedure QuerySvcConfig(Service: String; out config: TQueryServiceConfig);
var
  schm,
  schs: SC_Handle;
  ss: TServiceStatus;
  dwChkP: DWord;
  sConfig: PQueryServiceConfigA;
  sNeeded: Cardinal;
begin
  schm :
= OpenSCManager(nilnil, SC_MANAGER_CONNECT);
  
if (schm > 0then
  
begin
    schs :
= OpenService(schm, PChar(Service), SERVICE_QUERY_CONFIG);
    
if (schs > 0then
    
begin
      
//Call the QueryServiceConfig function to get the required buffer size
      
if QueryServiceConfig(schs,sConfig,0,sNeeded) <> TRUE then
      
begin
        
if GetLastError = ERROR_INSUFFICIENT_BUFFER then
        
begin
          
//Get the memory required by using the Bytes needed var
          GetMem(sConfig, sNeeded);
          try
            
if QueryServiceConfig(schs,sConfig,sNeeded,sNeeded) <> TRUE then
            
begin
              MessageBox(Application.Handle, PChar(SysErrorMessage(GetLastError)),
                PChar(Application.Title), MB_OK 
+ MB_ICONEXCLAMATION);
            
end
            
else
            
begin
              
//Play with Config Info here
              config :
= sConfig^;
            
end;
          finally
            FreeMem(sConfig, sNeeded);
          
end;
        
end;
      
end;
      CloseServiceHandle(schs);
    
end;
    CloseServiceHandle(schm);
  
end;
end;
posted @ 2009-10-09 17:59  静候良机  阅读(576)  评论(0编辑  收藏  举报