?
Solved

Manually Starting Automatic Services.

Posted on 2001-07-20
11
Medium Priority
?
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

741 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