Difference between revisions of "ServiceManager"

From Lazarus wiki
Jump to navigationJump to search
(code highlight)
Line 5: Line 5:
 
Note: I could only get this running using an elevated command prompt on Vista, which shouldn't be necessary.
 
Note: I could only get this running using an elevated command prompt on Vista, which shouldn't be necessary.
  
--[[User:BigChimp|BigChimp]] 10:27, 10 June 2011 (CEST)
 
  
program ServiceTest;
+
<delphi>program ServiceTest;
// Check if a certain process is running.
+
// Check if a certain process is running.
{$mode objfpc}{$H+}
+
{$mode objfpc}{$H+}
uses
+
uses
  Classes,
+
  Classes,
 
   SysUtils,
 
   SysUtils,
 
   ServiceManager,
 
   ServiceManager,
 
   JwaWinSvc {for services declarations};
 
   JwaWinSvc {for services declarations};
  function IsServiceRunning(ServiceName: string): boolean;
+
 
    {description Checks if a Windows service is running}
+
function IsServiceRunning(ServiceName: string): boolean;
  var
+
  {description Checks if a Windows service is running}
    Services: TServiceManager;
+
var
    ServiceStatus: TServiceStatus;
+
  Services: TServiceManager;
  begin
+
  ServiceStatus: TServiceStatus;
    //Check for existing services
+
begin
    //equivalent to sc query <servicename>
+
  //Check for existing services
    Services := TServiceManager.Create(nil);
+
  //equivalent to sc query <servicename>
 +
  Services := TServiceManager.Create(nil);
 +
  try
 
     try
 
     try
       try
+
       Services.Connect;
        Services.Connect;
+
      Services.Acces := SC_MANAGER_CONNECT; //Note typo in property.
        Services.Acces := SC_MANAGER_CONNECT; //Note typo in property.
+
      //We don't need more access permissions than this; by default
        //We don't need more access permissions than this; by default
+
      //the servicemanager is trying to get all access
        //the servicemanager is trying to get all access
+
      Services.GetServiceStatus(ServiceName, ServiceStatus);
        Services.GetServiceStatus(ServiceName, ServiceStatus);
+
      if ServiceStatus.dwCurrentState = SERVICE_RUNNING then
        if ServiceStatus.dwCurrentState = SERVICE_RUNNING then
+
      begin
        begin
+
        Result := True;
          Result := True;
+
      end
        end
+
      else
        else
+
      begin
        begin
+
        Result := False;
          Result := False;
+
      end;
        end;
+
      Services.Disconnect;
        Services.Disconnect;
+
    except
      except
+
      on E: EServiceManager do
        on E: EServiceManager do
+
      begin
        begin
+
        // A missing service might throw a missing handle exception? No?
          // A missing service might throw a missing handle exception? No?
+
      {LogOutput('Error getting service information for ' + ServiceName +
        {LogOutput('Error getting service information for ' + ServiceName +
+
        '. Technical details: ' + E.ClassName + '/' + E.Message);}
          '. Technical details: ' + E.ClassName + '/' + E.Message);}
+
        Result := False;
          Result := False;
+
        raise; //rethrow original exception
          raise; //rethrow original exception
+
      end;
        end;
+
      on E: Exception do
        on E: Exception do
+
      begin
        begin
+
      {LogOutput('Error getting service information for ' + ServiceName +
        {LogOutput('Error getting service information for ' + ServiceName +
+
        '. Technical details: ' + E.ClassName + '/' + E.Message);
          '. Technical details: ' + E.ClassName + '/' + E.Message);
+
        }
          }
+
        Result := False;
          Result := False;
+
        raise; //rethrow original exception
          raise; //rethrow original exception
 
        end;
 
 
       end;
 
       end;
    finally
 
      Services.Free;
 
 
     end;
 
     end;
 +
  finally
 +
    Services.Free;
 
   end;
 
   end;
const
+
end;
 +
 
 +
const
 
   ServiceToTest = 'SamSs';
 
   ServiceToTest = 'SamSs';
  //Security Accounts Manager, should be running, at least on Vista
+
 
begin
+
//Security Accounts Manager, should be running, at least on Vista
 +
egin
 
   WriteLn('Starting test for ' + ServiceToTest + ' service.');
 
   WriteLn('Starting test for ' + ServiceToTest + ' service.');
 
   if IsServiceRunning(ServiceToTest) then
 
   if IsServiceRunning(ServiceToTest) then
Line 75: Line 77:
 
     WriteLn('The ' + ServiceToTest + ' service is not running');
 
     WriteLn('The ' + ServiceToTest + ' service is not running');
 
   end;
 
   end;
end.
+
end.</delphi>

Revision as of 08:22, 9 August 2011

For Windows, the fcl-extra package contains a unit called ServiceManager.

Example that checks whether the SamSs (Security Accounts Manager; should always be running) service is running is below. (Command line equivalent sc query samss) Note: I could only get this running using an elevated command prompt on Vista, which shouldn't be necessary.


<delphi>program ServiceTest; // Check if a certain process is running. {$mode objfpc}{$H+} uses

 Classes,
 SysUtils,
 ServiceManager,
 JwaWinSvc {for services declarations};

function IsServiceRunning(ServiceName: string): boolean;

 {description Checks if a Windows service is running}

var

 Services: TServiceManager;
 ServiceStatus: TServiceStatus;

begin

 //Check for existing services
 //equivalent to sc query <servicename>
 Services := TServiceManager.Create(nil);
 try
   try
     Services.Connect;
     Services.Acces := SC_MANAGER_CONNECT; //Note typo in property.
     //We don't need more access permissions than this; by default
     //the servicemanager is trying to get all access
     Services.GetServiceStatus(ServiceName, ServiceStatus);
     if ServiceStatus.dwCurrentState = SERVICE_RUNNING then
     begin
       Result := True;
     end
     else
     begin
       Result := False;
     end;
     Services.Disconnect;
   except
     on E: EServiceManager do
     begin
       // A missing service might throw a missing handle exception? No?
     {LogOutput('Error getting service information for ' + ServiceName +
       '. Technical details: ' + E.ClassName + '/' + E.Message);}
       Result := False;
       raise; //rethrow original exception
     end;
     on E: Exception do
     begin
     {LogOutput('Error getting service information for ' + ServiceName +
       '. Technical details: ' + E.ClassName + '/' + E.Message);
       }
       Result := False;
       raise; //rethrow original exception
     end;
   end;
 finally
   Services.Free;
 end;

end;

const

 ServiceToTest = 'SamSs';

//Security Accounts Manager, should be running, at least on Vista egin

 WriteLn('Starting test for ' + ServiceToTest + ' service.');
 if IsServiceRunning(ServiceToTest) then
 begin
   WriteLn('The ' + ServiceToTest + ' service is running');
 end
 else
 begin
   WriteLn('The ' + ServiceToTest + ' service is not running');
 end;

end.</delphi>