Solved

Manually Starting Automatic Services.

Posted on 2001-07-20
11
247 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 171
delphi custom sort exception 6 135
Reconfigure Delphi Install? 2 46
Dev express lookupcombo 3 11
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

15 Experts available now in Live!

Get 1:1 Help Now