Som nævnt er der ingen tvivl om at stoney\'s fremgangsmåde virker. Men skal det gøres ordentligt (læs programmatisk) så er her hvordan man gør. Denne kode er \"off the top of my head\" og er ikke garanteret at virke. Slå selv de respektive funktioner efter, for yderligere information.
uses Windows, WinSvc;
procedure ServiceKontrol(Start : boolean); var hSCM : SC_HANDLE; hSvc : SC_HANDLE; st : SERVICE_STATUS; pArgs : pchar; bResult : boolean; begin hSCM:= 0; hSvc:= 0; FillChar(st, SizeOf(st), 0); pArgs:= nil;
try // open the Service Control Manager (SCM) hSCM:= OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS); if hSCM = 0 then begin raise Exception.Create(\'Error opening Service Control Manager\'); end; // open the service we want to control (in this case \'alerter\') hSvc:= OpenService(hSCM, \'alerter\', GENERIX_EXECUTE); if hSvc = 0 then begin raise Exception.Create(\'Error opening service \"alerter\"\'); end; if Start then ebgin // start the service. pArgs can be used to send text arguments to the service bResult:= StartService(hSvc, 0, pArgs); if not bResult then begin raise Exception.Create(\'Could not start service \"alerter\"\'); end; end else begin // Control the service. In this example we want to stop it! // You could use other SERVICE_CONTROL_* commands bResult:= ControlService(hSvc, SERVICE_CONTROL_STOP, st); if not bResult then begin raise Exception.Create(\'Could not stop service \"alerter\"\'); end; end; finally // now it is time to clean up! if hSvc <> 0 then begin CloseServiceHandle(hSvc); end; if hSCM <> 0 then begin CloseServiceHandle(hSCM); end; end; end;
Hej - her er et par nyttige funktioner til at styre services med :
//function to start a service running on a local or remote machine. //aMachine is UNC path or local machine if left empty function TForm1.ServiceStart(aMachine, aServiceName : string ) : boolean; var h_manager,h_svc: SC_Handle; svc_status: TServiceStatus; Temp: PChar; dwCheckPoint: DWord; intCount : integer; begin svc_status.dwCurrentState := 1; h_manager := OpenSCManager(PChar(aMachine), Nil, SC_MANAGER_CONNECT); if h_manager > 0 then begin h_svc := OpenService(h_manager, PChar(aServiceName),SERVICE_START or SERVICE_QUERY_STATUS); if h_svc > 0 then begin temp := nil; if (StartService(h_svc,0,temp)) then if (QueryServiceStatus(h_svc,svc_status)) then begin intCount := 0; while (SERVICE_RUNNING <> svc_status.dwCurrentState) do begin dwCheckPoint := svc_status.dwCheckPoint;
Sleep(2000); //(svc_status.dwWaitHint);
if (not QueryServiceStatus(h_svc,svc_status)) then break;
if (svc_status.dwCheckPoint < dwCheckPoint) then begin // QueryServiceStatus didn\'t increment dwCheckPoint break; end; //timeout test inc(intCount); if intCount > 30 then break; end; end; CloseServiceHandle(h_svc); end; CloseServiceHandle(h_manager); end; Result := SERVICE_RUNNING = svc_status.dwCurrentState; end;
//function to stop a service running on a local or remote machine. //aMachine is UNC path or local machine if left empty function TForm1.ServiceStop(aMachine,aServiceName : string ) : boolean; var h_manager,h_svc : SC_Handle; svc_status : TServiceStatus; dwCheckPoint : DWord; intCount : integer; begin h_manager:=OpenSCManager(PChar(aMachine),nil, SC_MANAGER_CONNECT); if h_manager > 0 then begin h_svc := OpenService(h_manager,PChar(aServiceName), SERVICE_STOP or SERVICE_QUERY_STATUS);
if h_svc > 0 then begin if(ControlService(h_svc,SERVICE_CONTROL_STOP, svc_status))then begin if(QueryServiceStatus(h_svc,svc_status))then begin intCount := 0; while(SERVICE_STOPPED <> svc_status.dwCurrentState)do begin dwCheckPoint := svc_status.dwCheckPoint; Sleep(2000); //(svc_status.dwWaitHint);
if(not QueryServiceStatus(h_svc,svc_status))then begin // couldn\'t check status break; end;
if(svc_status.dwCheckPoint < dwCheckPoint)then break; inc(intCount); if intCount > 30 then break; end; end; end; CloseServiceHandle(h_svc); end; CloseServiceHandle(h_manager); end; Result := SERVICE_STOPPED = svc_status.dwCurrentState; end;
//function to retrieve the status of a service running on a local or remote machine. //aMachine is UNC path or local machine if left empty function TForm1.ServiceGetStatus(sMachine, sService: string ): DWord; var h_manager, h_svc: SC_Handle; service_status : TServiceStatus; hStat : DWord; begin hStat := 1; h_manager := OpenSCManager(PChar(sMachine) ,Nil, SC_MANAGER_CONNECT);
if h_manager > 0 then begin h_svc := OpenService(h_manager,PChar(sService), SERVICE_QUERY_STATUS);
if h_svc > 0 then begin if(QueryServiceStatus(h_svc, service_status)) then hStat := service_status.dwCurrentState;
har ikke lige haft tid til at teste noget endnu - vender snarligt tilbage :-)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.