get status of service in delphi

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.
Who is Participating?
HypoConnect With a Mentor Commented:
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

WebWolf1Author Commented:
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).
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.

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.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.