get status of service in delphi

Posted on 2009-04-28
Last Modified: 2012-05-06
I wrote a service application in delphi. is there any easy way to query the status of service (if its running or not)? I need way inside the service application.
Question by:WebWolf1
    LVL 12

    Accepted Solution

    when you work with services programatically, you can use the Win32 API "Service Control Manager" to install, remove, start, stop config and check for status on services (read all about it in the Win32 SDK). What you do is that you first get a handle to the Service Control Manager, from which you then can query handles to the actual services that are installed on your system. When you have a service handle, you can use the Win32 API services functions to do all the things you want. I've put together an example for you where the status of a service is read.

    In the example, you just enter the name of the service you want to test in the Edit-field, and then press the Button to execute the code that reads the service status. I think this example will start you off in the right direction.

    unit Unit1;
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, WinSVC, StdCtrls;
      TForm1 = class(TForm)
        Edit1: TEdit;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        { Private declarations }
        { Public declarations }
      Form1: TForm1;
    {$R *.dfm}
    Function GetServiceStatus(ServiceName : AnsiString) : TServiceStatus;
    var aServiceControl : SC_Handle;
        aService : SC_Handle;
      aService := 0;
      // Get a handle to the service manager...
      aServiceControl := WinSVC.OpenSCManager(nil, nil, SC_MANAGER_CONNECT);
      if aServiceControl = 0 then RaiseLastOSError;
        // Get a handle to the service...
        aService := WinSVC.OpenService(aServiceControl, PAnsiChar(ServiceName), SERVICE_QUERY_STATUS);
        if aService = 0 then RaiseLastOSError;
        // Query the service
        if not WinSVC.QueryServiceStatus(aService, Result)
          then RaiseLastOSError;
        if aService <> 0 then WinSVC.CloseServiceHandle(aService);
        if aServiceControl <> 0 then WinSVC.CloseServiceHandle(aServiceControl)
    procedure TForm1.Button1Click(Sender: TObject);
      case GetServiceStatus(Edit1.Text).dwCurrentState of
        SERVICE_STOPPED: ShowMessage('Service is stopped');
        SERVICE_RUNNING: ShowMessage('Service is running');
        SERVICE_PAUSED:  ShowMessage('Service is paused');
        // There are more status codes as well that can be added here, but these are the most common... 

    Open in new window


    Author Comment

    please read again my post. i want a function or a way to get service status from the service application. inside the service application.

    example you provided is to query service status from any other application (desktop app).
    LVL 12

    Expert Comment

    Ok, sorry,
    that wasn't really clear to me by just reading your question.

    Anyway, you could still use the service control manager and the same function to do that test, even if you did the test, on your own service, from your own service. But maybe that is just not the way you want to do it? If it's not, then the TService class has a Status property of type TCurrentStatus, from which you could read the status from instead.

    If this is not what you are after, then I think you need to explain a bit more of what you are trying to acheive.

    LVL 14

    Expert Comment

    I dont believe it is possible to check the status of a service application that is not running or paused as there is no event upon which to run a function that checks the status. If you are able to check the status of the service, then by definition the status is "Running"
    Nonetheless, you could keep track of the status by maintaining a status variable in the various events, OnStart, OnPause,OnContinue etc.
    LVL 18

    Expert Comment

    SteveBay is 100% correct.

    If your service is stopped, paused or hung, there is no way within that service app that it can possibly query itself to see if it is running.

    You would have to do this from some other watchdog service or application.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now