Managing Windows Services using a Delphi Application
Windows services and knowing the status of a particular service are a often repeated process during application development as well as smoothing running of an application that require these services.
This code snippet is an example of how to get the status of a particular service in window in your program so that you may use the status for efficient and successful run of your program.
We will be using a Win 32 api QueryServiceStatus() in delphi coding to get the status in the following way .
if( ServiceRunning( '\ComputerName','\servicename' ) )then begin // Here you may mention the code if this particlar service is running. end; if( ServiceRunning( '', '\servicename' ) )then begin // If machine is a local computer no need to mention computer name end; if( ServiceStopped( '', '\servicename' ) )then begin // Put here code if the particular service is not running end;
In order to use the above code we need to define the functions like servicerunning and service stopped.
Here is how these functions are defined.
We start by including winsvc which is the delphi interface to interact with windows services.
uses WinSvc;
function WhatisServiceStatus( sMachine, sService : string ) : DWord;
//Here sMachine is the name of your machine in a network and blank for a standalone machine.
var
schm, schs : SC_Handle; //service handles
ss : TServiceStatus; //service status
dwStat : DWord; //current status
begin
dwStat := -1;
schm := OpenSCManager( PChar(sMachine), Nil, SC_MANAGER_CONNECT);
if(schm > 0)then //if connection to service manager is successful.
begin
schs := OpenService( schm, PChar(sService), SERVICE_QUERY_STATUS);
// we open a handle to the desired service and ask for the staus.
if(schs > 0)then //if status is returned
begin
if(QueryServiceStatus( schs, ss))then //ask for current status
begin
dwStat := ss.dwCurrentState;
end;
CloseServiceHandle(schs); //we have the staus so close handle.
end;
CloseServiceHandle(schm); //close connection to service manager.
end;
Result := dwStat; //Result have the current status of the service.
end;
Now we have the function that get us the current status of the service
and determining the status we can infer if service is running ,stopped,or service are pending.This function will get return true if service is running and will get false is either service is not running or in a pending state like service_start_pending or service_stop_pending.
function ServiceRunning( sMachine, sService : string ) : boolean; begin Result := SERVICE_RUNNING = ServiceGetStatus(sMachine, sService ); end;
function ServiceStopped( sMachine, sService : string ) : boolean; begin Result := SERVICE_STOPPED = ServiceGetStatus( sMachine, sService ); end;
This will return if the service is stopped.
So now you do have some niftly code so that you can ascertain in your program the current status of a particular service just by adding some lines of codes.
I derive strength from myteam,myfamily and GOD.I believe teamwork elevates a task to
perfection and everyone is contented with their contribution to qualify for the sweet
joy of success.I value commitment,dedication and above all a zeal to do something right
and proper.
LeBron James shoes
20 Apr, 2010
A good article Thank you! LeBron
James shoes
RFID Reader
20 Apr, 2010
thanks for this great post wow… it’s very
wonderful RFID Reader
Free Download Games
20 Apr, 2010
This guy is really lucky ! Your site is very useful for me .I bookmarked your site!
Wholesale polo shirts
22 Apr, 2010
I really enjoyed this post, especially the “examples in this post” portion which made it really easy for me to SEE what you were talking about without even having to leave the article. Thanks
cheap authentic nfl jerseys
24 Apr, 2010
Thanks for sharing this.
Tiffany Jewelry
24 Apr, 2010
Thank you for Posting & I got to read nice information on your site.
Bad Credit Car Loans
27 Apr, 2010
Thanks for good information that comes out to
read.