Link to home
Start Free TrialLog in
Avatar of xy15973
xy15973

asked on

Stop Start NT Service from Delphi App

I wrote a the following in Delphi 5
1) NT Service.
2) Control Panel application that contains one Delphi form.
    I need to stop and start my NT Service from the Control Panel's form.

How does one stop and start a NT Service from a delphi Application ?
Applications to be deployed to Win2K and XP platforms.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of krypto2000
krypto2000

I've maked this just two days before ;-)

There is my solution :

uses
   WinSvc, SvcMgr;

const
   SERVICE_NAME = 'myService';

{ start / pause / stop service }
procedure setServiceStatus(serviceName:string; serviceStatus:cardinal);
var svc,
    svcManager : SC_HANDLE;
    status     : TServiceStatus;
begin
   svcManager := OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);

   if serviceStatus = SERVICE_RUNNING then
      if currentServiceStatus = SERVICE_PAUSED then
         try
         svc := OpenService(svcManager,PChar(serviceName),SERVICE_PAUSE_CONTINUE);
         ControlService(svc,SERVICE_CONTROL_CONTINUE,status);
         finally
           CloseServiceHandle(svc);
         end
      else
         try
           svc := OpenService(svcManager,PChar(serviceName),SERVICE_START);
           StartService(svc,0,Pchar(nil^));
         finally
           CloseServiceHandle(svc);
         end;

   if serviceStatus = SERVICE_PAUSED then
      try
        svc := OpenService(svcManager,PChar(serviceName),SERVICE_PAUSE_CONTINUE);
        ControlService(svc,SERVICE_CONTROL_PAUSE,status);
      finally
        CloseServiceHandle(svc);
      end;

   if serviceStatus = SERVICE_STOPPED then
      try
        svc := OpenService(svcManager,PChar(serviceName),SERVICE_STOP);
        ControlService(svc,SERVICE_CONTROL_STOP,status);
      finally
        CloseServiceHandle(svc);
      end;

   CloseServiceHandle(svcManager);
end;

procedure TfrmMain.btnStartClick(Sender: TObject);
begin
   setServiceStatus(SERVICE_NAME,SERVICE_RUNNING);
end;

procedure TfrmMain.btnPauseClick(Sender: TObject);
begin
   setServiceStatus(SERVICE_NAME,SERVICE_PAUSED);
end;

procedure TfrmMain.btnStopClick(Sender: TObject);
begin
   setServiceStatus(SERVICE_NAME,SERVICE_STOPPED);
end;

and there is another function to get the service status

{ return service state }
function getServiceStatus(serviceName:string): cardinal;
var svc,
    svcManager : SC_HANDLE;
    status     : TServiceStatus;
begin
   svcManager := OpenSCManager(nil,nil,GENERIC_READ);
   try
     svc := OpenService(svcManager,PChar(serviceName),SERVICE_QUERY_STATUS);
     if svc <> 0 then
       try
         if QueryServiceStatus(svc,status) then result := status.dwCurrentState;
       finally
         CloseServiceHandle(svc);
       end;
   finally
     CloseServiceHandle(svcManager);
   end;
end;

use --> currentServiceStatus := GetServiceStatus(SERVICE_NAME);

I hope the help you ;-)