Я рекомендую метод, данный doofledorfer.
Если вы действительно хотите сделать это с помощью прямого вызова API, посмотрите на функцию OpenSCManager . Ниже приведены примеры функций для получения имени и службы компьютера, а также их остановки или запуска.
function ServiceStart(sMachine, sService : string) : boolean; //start service, return TRUE if successful
var schm, schs : SC_Handle;
ss : TServiceStatus;
psTemp : PChar;
dwChkP : DWord;
begin
ss.dwCurrentState := 0;
schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT); //connect to the service control manager
if(schm > 0)then begin // if successful...
schs := OpenService( schm,PChar(sService),SERVICE_START or SERVICE_QUERY_STATUS); // open service handle, start and query status
if(schs > 0)then begin // if successful...
psTemp := nil;
if (StartService(schs,0,psTemp)) and (QueryServiceStatus(schs,ss)) then
while(SERVICE_RUNNING <> ss.dwCurrentState)do begin
dwChkP := ss.dwCheckPoint; //dwCheckPoint contains a value incremented periodically to report progress of a long operation. Store it.
Sleep(ss.dwWaitHint); //Sleep for recommended time before checking status again
if(not QueryServiceStatus(schs,ss))then
break; //couldn't check status
if(ss.dwCheckPoint < dwChkP)then
Break; //if QueryServiceStatus didn't work for some reason, avoid infinite loop
end; //while not running
CloseServiceHandle(schs);
end; //if able to get service handle
CloseServiceHandle(schm);
end; //if able to get svc mgr handle
Result := SERVICE_RUNNING = ss.dwCurrentState; //if we were able to start it, return true
end;
function ServiceStop(sMachine, sService : string) : boolean; //stop service, return TRUE if successful
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)) and (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 < dwChkP)then
Break;
end; //while
CloseServiceHandle(schs);
end; //if able to get svc handle
CloseServiceHandle(schm);
end; //if able to get svc mgr handle
Result := SERVICE_STOPPED = ss.dwCurrentState;
end;