delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
492 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (12)


Access and control a NT serviceFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 6.x (or higher)
Category:
System
Skill Level:
Scoring:
Last Update:
01/04/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet NT-Service Service OpenSCManager OpenService CloseServiceHandle QueryServiceConfig StartService QueryServiceStatus ControlService
Times Scored:
7
Visits:
4976
Uploader: Bertrand Goetzmann
Company:
Reference: http://www.object-everywhere.com
 
Question/Problem/Abstract:
The unit described in this article show how we can start or stop a NT service by programming, or getting the location of its binary implentation.
Answer:



unit SvcUtils;

// Written by Bertrand Goetzmann (http://www.object-everywhere.com)
// Keywords : Service, OpenSCManager, OpenService, CloseServiceHandle, QueryServiceConfig, StartService, QueryServiceStatus, ControlService

interface

// This function returns the entire path location of the implementation of the given name service
function GetBinaryPathName(const ServiceName: string): string;

// This function starts the service with the given service name
procedure StartService(const ServiceName: string);

// This function stops the service with the given service name
procedure StopService(const ServiceName: string);


implementation

uses SysUtils, WinSvc;

function GetBinaryPathName(const ServiceName: string): string;
var
  SvcMgr, Svc: Integer;
  QuerySvc: TQueryServiceConfig;
  BytesNeeded: Cardinal;

  Buffer: PQueryServiceConfig;
begin
  // Establish a connection to the service control manager
  SvcMgr := OpenSCManager(nil (*MachineName*), nil (*DatabaseName*), SC_MANAGER_ALL_ACCESS);
  try
    if SvcMgr = 0 then RaiseLastOSError;

    Svc := OpenService(SvcMgr, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if Svc = 0 then RaiseLastOSError;
    try
      // Make a call to know the number of bytes needed
      QueryServiceConfig(Svc, @QuerySvc, 0, BytesNeeded);

      GetMem(Buffer, BytesNeeded);
      try
        if not QueryServiceConfig(Svc, Buffer, BytesNeeded, BytesNeeded) then
          RaiseLastOSError;

        Result := PQueryServiceConfig(Buffer).lpBinaryPathName;
      finally
        FreeMem(Buffer);
      end;
    finally
      CloseServiceHandle(Svc);
    end;
  finally
    CloseServiceHandle(SvcMgr);
  end;
end;

procedure StartService(const ServiceName: string);
var
  SvcMgr, Svc: Integer;
  ServiceArgVectors: PChar;
begin
  // Establish a connection to the service control manager
  SvcMgr := OpenSCManager(nil (*MachineName*), nil (*DatabaseName*), SC_MANAGER_ALL_ACCESS);
  try
    if SvcMgr = 0 then RaiseLastOSError;

    Svc := OpenService(SvcMgr, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if Svc = 0 then RaiseLastOSError;
    try
      if not WinSvc.StartService(Svc, 0 (*NumServiceArgs*), ServiceArgVectors) then
        RaiseLastOSError;
    finally
      CloseServiceHandle(Svc);
    end;
  finally
    CloseServiceHandle(SvcMgr);
  end;
end;

procedure StopService(const ServiceName: string);
var
  SvcMgr, Svc: Integer;
  ServiceStatus: _SERVICE_STATUS;
begin
  // Establish a connection to the service control manager
  SvcMgr := OpenSCManager(nil (*MachineName*), nil (*DatabaseName*), SC_MANAGER_ALL_ACCESS);
  try
    if SvcMgr = 0 then RaiseLastOSError;

    Svc := OpenService(SvcMgr, PChar(ServiceName), SERVICE_ALL_ACCESS);
    if Svc = 0 then RaiseLastOSError;
    try
      // if not QueryServiceStatus(Svc, ServiceStatus) then
      //  RaiseLastOSError;
      // You can test the ServiceStatus.dwCurrentState field

      if not ControlService(Svc, SERVICE_CONTROL_STOP, ServiceStatus) then
        RaiseLastOSError;
    finally
      CloseServiceHandle(Svc);
    end;
  finally
    CloseServiceHandle(SvcMgr);
  end;
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
prompt before close
    isaac sam (Oct 21 2007 12:26AM)

I made a service application, and I need to display prompt message when a user try to manually close (from task manager) or stop (from services) this service
Respond

Registering the Application
    Rene Melo (Apr 12 2002 7:42PM)

As do to register my application as a service
Respond

RE: Registering the Application
Bertrand Goetzmann (Apr 15 2002 11:24AM)

You can register your service application simply by executing it with the /INSTALL option.
Respond

a question
    Yuncheng li (Apr 11 2002 5:50AM)

How to pass multi-arguments to StartService?
Respond

RE: a question
Bertrand Goetzmann (Apr 15 2002 12:44PM)

You can pass some argument with the starting of a service with the call of the StartService function of the Win32 API.
The StartService procedure of the SvcUtils unit makes a such call with 0 argument :
WinSvc.StartService(Svc, 0 (*NumServiceArgs*), ServiceArgVectors)
Respond

RE: RE: a question
Yuncheng li (Apr 17 2002 9:15AM)

Pass 0 or 1 arguments just as above is OK. but how to pass two arguments? give me an example.
Respond

RaiseLastOSError?
    Luis (Jan 5 2002 12:29AM)

what is RaiseLastOSError?
Respond

RE: RaiseLastOSError?
Bertrand Goetzmann (Jan 5 2002 5:51PM)

RaiseLastOSError is a function from the SysUtils unit that permits to raise an exception for the last operating system error or library system error.
Respond

I did know, but do you know...
    Christian Cristofori (Jan 4 2002 8:17PM)

I did know how to manage this, but I'm looking for a way to have the list of all installed services. Do you have a solution for that?
Thank you very much.
Respond

RE: I did know, but do you know...
Bertrand Goetzmann (Jan 10 2002 10:43AM)

I think that the EnumServicesStatus function from the Windows API is the solution.
The function enumerates services in the specified service control manager database. The name and status of each service are provided.
Respond

What does it actually do though?
    Jonathon Hibbard (Jan 4 2002 4:54PM)

Hi, the code looks very nice, but one problem; what is it suposed to do?? Any insight on what this program can do to benefit us would be greatly apprecaited! Thanks again!
Respond

RE: What does it actually do though?
Bertrand Goetzmann (Jan 4 2002 6:00PM)

On the Windows NT plateform you can look all the installed services by executing MMC (Microsoft Managment Console). All these services can be handled by this GUI application (start, stop, suspend, etc.).
This article show the use of some of the Service Functions from the Window API to do the same things by programming.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Kleiner
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)