-Karamja-
asked on
Services, List | Start/Stop
Hey All,
I need a simple program to list the services on a system
and two buttons,
"Start" //Starts selected service
"Stop" //Stops the selected service
Thanks, -Karamja-
I need a simple program to list the services on a system
and two buttons,
"Start" //Starts selected service
"Stop" //Stops the selected service
Thanks, -Karamja-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks your your post,
But I cant get the code to work can you upload this to a webserver?
But I cant get the code to work can you upload this to a webserver?
procedure StartServiceExecute(Sender
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_RUNN
service is already running.', mtError, [mbOK], 0);
ERROR_SERVICE_DATABASE_LOC
locked.', mtError, [mbOK], 0);
ERROR_SERVICE_DEPENDENCY_D
on a service that does not exist or has been marked for deletion.', mtError,
[mbOK], 0);
ERROR_SERVICE_DEPENDENCY_F
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
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_D
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_TIME
service was started, but it did not call StartServiceCtrlDispatcher
thread that called StartServiceCtrlDispatcher
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:
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_R
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_CONT
control code is not valid, or it is unacceptable to the service.', mtError,
[mbOK], 0);
ERROR_SERVICE_CANNOT_ACCEP
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_TIME
service was started, but it did not call StartServiceCtrlDispatcher
thread that called StartServiceCtrlDispatcher
handler function.', mtError, [mbOK], 0);
ERROR_SHUTDOWN_IN_PROGRESS
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;