Function StartService (AServName: string): Boolean; // use WinSvc
Var
SCManager, hService: SC _HANDLE;
LpServiceArgVectors: PChar;
Begin
SCManager: = OpenSCManager (nil, nil, SC _MANAGER_ALL_ACCESS );
Result: = SCManager <> 0;
If Result then
Try
HService: = OpenService (SCManager, PChar (AServName), SERVICE_ALL_ACCESS );
Result: = hService <> 0;
If (hService = 0) and (GetLastError = ERROR_SERVICE_DOES_NOT_EXIST) then
Exception. Create ('the specified service does not exist ');
If hService <> 0 then
Try
LpServiceArgVectors: = nil;
Result: = WinSvc. StartService (hService, 0, PChar (lpServiceArgVectors ));
If not Result and (GetLastError = ERROR_SERVICE_ALREADY_RUNNING) then
Result: = True;
Finally
CloseServiceHandle (hService );
End;
Finally
CloseServiceHandle (SCManager );
End;
End;
// If you want to publish this article, please indicate the exit, no outgoing copyright notice, I do not like to see this kind of website. My work website does not indicate the person who is serving Seven {See7di # Gmail.com}
Function StopService (AServName: string): Boolean;
Var
SCManager, hService: SC _HANDLE;
SvcStatus: TServiceStatus;
Begin
SCManager: = OpenSCManager (nil, nil, SC _MANAGER_ALL_ACCESS );
Result: = SCManager <> 0;
If Result then
Try
HService: = OpenService (SCManager, PChar (AServName), SERVICE_ALL_ACCESS );
Result: = hService <> 0;
If Result then
Try // stop and uninstall the service;
Result: = ControlService (hService, SERVICE_CONTROL_STOP, SvcStatus );
// Delete the service. This sentence is optional;
// DeleteService (hService );
Finally
CloseServiceHandle (hService );
End;
Finally
CloseServiceHandle (SCManager );
End;
End;