?
Solved

Manually Starting Automatic Services.

Posted on 2001-07-20
11
Medium Priority
?
261 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 520 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

809 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