Solved

Manually Starting Automatic Services.

Posted on 2001-07-20
11
245 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now