1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
   |  
// retourne le type de démarrage du service
function ServiceGetStartType( sMachineName, sServiceName : string) : DWord;
var
 schm   : SC_Handle;
 schs   : SC_Handle;
 qsc    : TQueryServiceConfig;
 dwNeededBytes : DWord;
begin
  // initialisation
  Result := 0;
  schs := 0;
 
  // se connecte au service control manager
  schm := OpenSCManager( PChar(sMachineName), Nil, SC_MANAGER_CONNECT);
 
  try
    // si la connection a réussie
    if schm > 0 then
    begin
      // ouvre un handle sur le service en question afin de recuperer sa config
      schs := OpenService( schm, PChar(sServiceName), SERVICE_QUERY_CONFIG);
 
      // si la connection au service a réussie
      if schs > 0 then
      begin
        // recupere le type de démarrage du service
        dwNeededBytes := 0;
        if QueryServiceConfig( schs, @qsc, sizeof(qsc), dwNeededBytes) then
        begin
          Result := qsc.dwStartType;
        end
        else
        begin
          // si échec, envoie une exception
          case GetLastError of
            ERROR_ACCESS_DENIED : Raise Exception.Create('ERROR_ACCESS_DENIED');
            ERROR_INSUFFICIENT_BUFFER : Raise Exception.Create('ERROR_INSUFFICIENT_BUFFER : ' + IntToStr(dwNeededBytes));
            ERROR_INVALID_HANDLE : Raise Exception.Create('ERROR_INVALID_HANDLE');
          else
            Raise Exception.Create( Format(rsUnableToGetServiceStartType, [sServiceName]));
          end;
        end;
      end
      else
      begin
        // envoie une exception
        Raise Exception.Create( Format(rsUnableToOpenService, [sServiceName]));
      end;
    end
    else
    begin
      // envoie une exception
      if sMachineName = '' then
      begin
        Raise Exception.Create( Format(rsUnableToConnectToSCM, [rsLocalMachine]));
      end
      else
      begin
        Raise Exception.Create( Format(rsUnableToConnectToSCM, [sMachineName]));
      end;
    end;
  finally
    if schs > 0 then
    begin
      // ferme le handle du service
      CloseServiceHandle(schs);
    end;
 
    if schm > 0 then
    begin
      // ferme le handle du control manager
      CloseServiceHandle(schm);
    end;
  end;
end; | 
Partager