Solved

Manually Starting Automatic Services.

Posted on 2001-07-20
11
250 Views
Last Modified: 2010-04-06
Guys..And Gals..

I have an interesting problem with one of my apps.
Within my app I oftern look to see if a specific service is running.
( I should state at this point that the service is set to start automatically on boot.But for some reason doesn't always do so or can sometimes be shut down)

I need some code/functions to allow me to restart the service if it has stopped.

I'm under the impression that you cant manually start a service if its defined as automatic and so I may need to change the service status temporaily in order to restart. But I'm not sure and thats the reason I'm putting it to you guys.

I have a lot more points that what is being offered and I will be 'upping' them if the code works..

Thanks Madmarlin.. ;?)
0
Comment
Question by:Madmarlin
  • 5
  • 5
11 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 6302158
Hi,
You can stop and then start a service which is defined as automatic. And it will remain automatic. The following code works fine under NT4.

unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 StdCtrls,WinSvc;

type
 TForm1 = class(TForm)
   Button1: TButton;
   Button2: TButton;
   procedure Button1Click(Sender: TObject);
   procedure Button2Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}

function ServiceStart(sMachine, sService: String) : Boolean;
var
  schm,   schs: SC_Handle;
  ss: TServiceStatus;
  psTemp: PChar;
  dwChkP: DWord;
begin
  ss.dwCurrentState := 0;
 schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
  if (schm>0) then
  begin
   schs := OpenService(schm, PChar(sService), SERVICE_START or SERVICE_QUERY_STATUS);
    if (schs>0) then
   begin
     psTemp := nil;
      if (StartService(schs, 0, psTemp)) then
        if (QueryServiceStatus(schs, ss)) then
         while (SERVICE_RUNNING<>ss.dwCurrentState) do
          begin
           dwChkP := ss.dwCheckPoint;
            Sleep(ss.dwWaitHint);
           if (not QueryServiceStatus(schs, ss)) then    
            Break;
            if ss.dwCheckPoint <> 0 then  
              Break;
          end;
      CloseServiceHandle(schs);
    end;
   CloseServiceHandle(schm);
  end;
 Result := SERVICE_RUNNING=ss.dwCurrentState;
end;

function ServiceStop(sMachine, sService: String) : Boolean;
var   schm,   schs: SC_Handle;
  ss: TServiceStatus;
 dwChkP: DWord;
begin  
schm := OpenSCManager(PChar(sMachine), nil, SC_MANAGER_CONNECT);
 if (schm>0) then   begin
    schs := OpenService(schm, PChar(sService), SERVICE_STOP or SERVICE_QUERY_STATUS);
    if (schs>0) then
    begin
     if (ControlService(schs, SERVICE_CONTROL_STOP, ss)) then
        if (QueryServiceStatus(schs, ss)) then
         while (SERVICE_STOPPED<>ss.dwCurrentState) do
         begin  
          dwChkP := ss.dwCheckPoint;
            Sleep(ss.dwWaitHint);
            if (not QueryServiceStatus(schs, ss)) then
              Break;
            if ss.dwCheckPoint <> 0 then
              Break;
          end;
      CloseServiceHandle(schs);
    end;
   CloseServiceHandle(schm);
  end;
  Result := SERVICE_STOPPED=ss.dwCurrentState;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
ServiceStart('','Alerter');
//ServiceStart('\\computername','Alerter');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ServiceStop('','Alerter');
end;

end.

Regards, Geo
0
 
LVL 1

Author Comment

by:Madmarlin
ID: 6302291
Sorry Geo Doesn;t seemto work for Automatic Services.

Maybe its a win2000 problem but I can stop and start services marked as automatic.
I've tried passing the Display Name, Full Path location and the service name but neither works unless its a manual service.
0
 
LVL 1

Author Comment

by:Madmarlin
ID: 6302296
Sorry Geo Doesn;t seemto work for Automatic Services.

Maybe its a win2000 problem but I can stop and start services marked as automatic.
I've tried passing the Display Name, Full Path location and the service name but neither works unless its a manual service.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Expert Comment

by:geobul
ID: 6302385
I've just tested the code above on Win2000 and it also works fine there. There could be a security reason at your side. Check the code as is (Alerter service) and you will see that it's OK.

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 6302401
O, and yes, it is the service name to be sent as a parameter (not display name).
0
 
LVL 1

Author Comment

by:Madmarlin
ID: 6302448
Yes Alerter works fine but its startup type is manual not automatic and thats what the problems seems to be. I can also start and stop the other Manual services but its doesn't work for automatic.
Maybe your right and there are some security issues here but the Winsvc functions dont contain username or password parameters so I cant see away around this if that is the case..

Arrgghhh this is turning out to be a very bad day..!!
;?(
0
 
LVL 17

Expert Comment

by:geobul
ID: 6302515
No, no. I've tested the Alerter service and one my service which both are automatic. The Alerter logons under system account and my service logons using my account. They both always remain automatic. 'Automatic' is used only at startup. You can stop any automatic service manually (Settings- Control Panel- Administrative Tools- Services) and its startup profile won't change.

How are you testing your app? Is it on the server where the service is running, or it is on a station? Are you able to you stop/start that service from the server as described above? I still think that it is a security rights problem.

Regards, Geo
0
 
LVL 1

Author Comment

by:Madmarlin
ID: 6302546

Wow interesting.

My test enviroment is at present just on one station.
But 'Alerter' is listed as Manual as it is on all the other win2000 machines here.

Are you able to run your code on any other 'Automatic services'??

I am able to change the service statuses manually through Admin tools-> services but its just when i come to change them through code..


0
 
LVL 17

Accepted Solution

by:
geobul earned 130 total points
ID: 6302632
Yes, I can. Only the service name is required. I've just tried with the Spooler. My account is a member of the Administrators group.

You can make the alerter automatic and to try again.

Regards, Geo
0
 
LVL 2

Expert Comment

by:egono
ID: 6302970
listening ...
0
 
LVL 1

Author Comment

by:Madmarlin
ID: 6307764
Thanks very much for the code, although I cant actually get it working at present. I know the code is correct and after testing have proved that the outstanding problem is a security issue.
Thanks again
Madmarlin
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Print Graphic and Text to Epson TM-T88v 12 378
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 71
Convert MS Word document to a PDF file 9 90
shape, triangle, dbctrlgrid 3 17
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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