get status of service in delphi

Posted on 2009-04-28
Medium Priority
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

Hypo earned 1500 total points
ID: 24249385
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

ID: 24249414
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

ID: 24249505
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

ID: 24250022
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

ID: 24252329
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.


Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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