Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to start / stop service from within delphi application ?

2,973 views
Skip to first unread message

Samson Fu

unread,
Nov 22, 2001, 2:12:43 AM11/22/01
to
Here's example I made. Hope it helps. "ServerServiceName" is the Name of
service to control.

procedure StartServiceExecute(Sender: TObject);
Var
SCH: SC_HANDLE;
SvcSCH: SC_HANDLE;
arg: PChar;
begin
SCH:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
If SCH=0 then
Begin
MessageDlg('Error: OpenSCManager', mtError, [mbOK], 0);
Exit;
End;

Screen.Cursor:= crHourGlass;
try
SvcSCH:= OpenService(SCH, PChar(ServerServiceName), SERVICE_ALL_ACCESS);
If SvcSCH=0 then
Begin
MessageDlg('Error: OpenService', mtError, [mbOK], 0);
Exit;
End;

try
arg:= nil;
If Not StartService(SvcSCH, 0, arg) then
Begin
Case GetLastError of
ERROR_ACCESS_DENIED :MessageDlg('The specified handle
was not opened with SERVICE_START access.', mtError, [mbOK], 0);
ERROR_INVALID_HANDLE :MessageDlg('The specified handle
is invalid.', mtError, [mbOK], 0);
ERROR_PATH_NOT_FOUND :MessageDlg('The service binary
file could not be found.', mtError, [mbOK], 0);
ERROR_SERVICE_ALREADY_RUNNING :MessageDlg('An instance of the
service is already running.', mtError, [mbOK], 0);
ERROR_SERVICE_DATABASE_LOCKED :MessageDlg('The database is
locked.', mtError, [mbOK], 0);
ERROR_SERVICE_DEPENDENCY_DELETED :MessageDlg('The service depends
on a service that does not exist or has been marked for deletion.', mtError,
[mbOK], 0);
ERROR_SERVICE_DEPENDENCY_FAIL :MessageDlg('The service depends
on another service that has failed to start.', mtError, [mbOK], 0);
ERROR_SERVICE_DISABLED :MessageDlg('The service has been
disabled.', mtError, [mbOK], 0);
ERROR_SERVICE_LOGON_FAILED :MessageDlg('The service could
not be logged on. This error occurs if the service was started from an
account that does not have the "Log on as a service" right.', mtError,
[mbOK], 0);
ERROR_SERVICE_MARKED_FOR_DELETE :MessageDlg('The service has been
marked for deletion.', mtError, [mbOK], 0);
ERROR_SERVICE_NO_THREAD :MessageDlg('A thread could not
be created for the service.', mtError, [mbOK], 0);
ERROR_SERVICE_REQUEST_TIMEOUT :MessageDlg('The process for the
service was started, but it did not call StartServiceCtrlDispatcher, or the
thread that called StartServiceCtrlDispatcher may be blocked in a control
handler function.', mtError, [mbOK], 0);
Else MessageDlg('Error:
StartService', mtError, [mbOK], 0);
End;{CASE}
End;
CheckServiceState;
finally
CloseServiceHandle(SvcSCH);
end;
finally
Screen.Cursor:= crDefault;
CloseServiceHandle(SCH);
end;
end;

procedure StopServiceExecute(Sender: TObject);
Var
SCH: SC_HANDLE;
SvcSCH: SC_HANDLE;
ss: TServiceStatus;
begin
SCH:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
If SCH=0 then
Begin
MessageDlg('Error: OpenSCManager', mtError, [mbOK], 0);
Exit;
End;

Screen.Cursor:= crHourGlass;
try
SvcSCH:= OpenService(SCH, PChar(ServerServiceName), SERVICE_ALL_ACCESS);
If SvcSCH=0 then
Begin
MessageDlg('Error: OpenService', mtError, [mbOK], 0);
Exit;
End;

try
If Not ControlService(SvcSCH, SERVICE_CONTROL_STOP, ss) then
Begin
Case GetLastError of
ERROR_ACCESS_DENIED :MessageDlg('The specified
handle was not opened with the necessary access.', mtError, [mbOK], 0);
ERROR_DEPENDENT_SERVICES_RUNNING :MessageDlg('The service cannot
be stopped because other running services are dependent on it.', mtError,
[mbOK], 0);
ERROR_INVALID_HANDLE :MessageDlg('The specified
handle was not obtained using CreateService or OpenService, or the handle is
no longer valid.', mtError, [mbOK], 0);
ERROR_INVALID_PARAMETER :MessageDlg('The requested
control code is undefined.', mtError, [mbOK], 0);
ERROR_INVALID_SERVICE_CONTROL :MessageDlg('The requested
control code is not valid, or it is unacceptable to the service.', mtError,
[mbOK], 0);
ERROR_SERVICE_CANNOT_ACCEPT_CTRL :MessageDlg('The requested
control code cannot be sent to the service because the state of the service
is SERVICE_STOPPED, SERVICE_START_PENDING, or SERVICE_STOP_PENDING.',
mtError, [mbOK], 0);
ERROR_SERVICE_NOT_ACTIVE :MessageDlg('The service has not
been started.', mtError, [mbOK], 0);
ERROR_SERVICE_REQUEST_TIMEOUT :MessageDlg('The process for the
service was started, but it did not call StartServiceCtrlDispatcher, or the
thread that called StartServiceCtrlDispatcher may be blocked in a control
handler function.', mtError, [mbOK], 0);
ERROR_SHUTDOWN_IN_PROGRESS :MessageDlg('The system is
shutting down.', mtError, [mbOK], 0);
Else MessageDlg('Error:
ControlService', mtError, [mbOK], 0);
End;
End;
CheckServiceState;
finally
CloseServiceHandle(SvcSCH);
end;
finally
Screen.Cursor:= crDefault;
CloseServiceHandle(SCH);
end;
end;


--
Samson Fu
"Sven Sonderegger" <s.sondereg...@xpedite.ch> wrote in message
news:3bfca49a$1_2@dnews...
> I developed a NT service - which works fine.
> Now I need to develop a GUI whcih has also the ability to start and stop
the
> service.
>
> How can I do this??
> (I read something about a COM API for WMI but couldn't really understand!)
>
> Thanks for any hints / help!
>
> Sven
>
>
>
>


Sven Sonderegger

unread,
Nov 22, 2001, 2:37:09 AM11/22/01
to
I get several compiling errors - is there a cpecial unit I have to include
in the uses??

errors:

[Error] Unit1.pas(29): Undeclared identifier: 'SC_HANDLE'
[Error] Unit1.pas(33): Undeclared identifier: 'OpenSCManager'
[Error] Unit1.pas(33): Undeclared identifier: 'SC_MANAGER_ALL_ACCESS'
[Warning] Unit1.pas(34): Comparing signed and unsigned types - widened both
operands
[Error] Unit1.pas(42): Undeclared identifier: 'OpenService'
[Error] Unit1.pas(42): Undeclared identifier: 'ServerServiceName'
[Error] Unit1.pas(42): Undeclared identifier: 'SERVICE_ALL_ACCESS'
[Warning] Unit1.pas(43): Comparing signed and unsigned types - widened both
operands
[Error] Unit1.pas(51): Undeclared identifier: 'StartService'
[Error] Unit1.pas(69): Undeclared identifier: 'CheckServiceState'
[Error] Unit1.pas(71): Undeclared identifier: 'CloseServiceHandle'
[Error] Unit1.pas(81): Undeclared identifier: 'SC_HANDLE'
[Error] Unit1.pas(83): Undeclared identifier: 'TServiceStatus'
[Error] Unit1.pas(85): Undeclared identifier: 'OpenSCManager'
[Error] Unit1.pas(85): Undeclared identifier: 'SC_MANAGER_ALL_ACCESS'
[Warning] Unit1.pas(86): Comparing signed and unsigned types - widened both
operands
[Error] Unit1.pas(94): Undeclared identifier: 'OpenService'
[Error] Unit1.pas(94): Undeclared identifier: 'ServerServiceName'
[Error] Unit1.pas(94): Undeclared identifier: 'SERVICE_ALL_ACCESS'
[Warning] Unit1.pas(95): Comparing signed and unsigned types - widened both
operands
[Error] Unit1.pas(102): Undeclared identifier: 'ControlService'
[Error] Unit1.pas(102): Undeclared identifier: 'SERVICE_CONTROL_STOP'
[Error] Unit1.pas(117): Undeclared identifier: 'CheckServiceState'
[Error] Unit1.pas(119): Undeclared identifier: 'CloseServiceHandle'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'

Sven

"Samson Fu" <sams...@hotmail.com> schrieb im Newsbeitrag
news:3bfca655_1@dnews...

Sven Sonderegger

unread,
Nov 22, 2001, 2:49:25 AM11/22/01
to
Forget about that - I added WinSvc and SvcMgr to the uses...
but there is still one identifier not declared: CheckServiceState
From where can I get this??

Sven


"Sven Sonderegger" <s.sondereg...@xpedite.ch> schrieb im Newsbeitrag
news:3bfcab22_2@dnews...

Sven Sonderegger

unread,
Nov 22, 2001, 2:09:18 AM11/22/01
to

Samson Fu

unread,
Nov 22, 2001, 4:16:34 AM11/22/01
to
You can just remove this function call..
It's no use for u...
:-)

--
Samson Fu
"Sven Sonderegger" <s.sondereg...@xpedite.ch> wrote in message

news:3bfcae02_1@dnews...

Sven Sonderegger

unread,
Nov 22, 2001, 4:38:40 AM11/22/01
to
It works now - thanks a lot!

Sven

"Samson Fu" <sams...@hotmail.com> schrieb im Newsbeitrag

news:3bfcc35f_2@dnews...

0 new messages