Mega Code Archive

 
Categories / Delphi / Examples
 

How to start and stop a service (or get status)

Title: How to start and stop a service (or get status) Question: How to use the API functions OpenSCManager, OpenService etc. Answer: Here are the functions ServiceStart and ServiceStop: function ServiceStart(aMachine, aServiceName : string ) : boolean; // aMachine is UNC path or local machine if left empty var h_manager,h_svc: SC_Handle; svc_status: TServiceStatus; Temp: PChar; dwCheckPoint: DWord; 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 while (SERVICE_RUNNING svc_status.dwCurrentState) do begin dwCheckPoint := svc_status.dwCheckPoint; Sleep(svc_status.dwWaitHint); if (not QueryServiceStatus(h_svc,svc_status)) then break; if (svc_status.dwCheckPoint begin // QueryServiceStatus didn't increment dwCheckPoint break; end; end; end; CloseServiceHandle(h_svc); end; CloseServiceHandle(h_manager); end; Result := SERVICE_RUNNING = svc_status.dwCurrentState; end; function ServiceStop(aMachine,aServiceName : string ) : boolean; // aMachine is UNC path or local machine if left empty var h_manager,h_svc : SC_Handle; svc_status : TServiceStatus; dwCheckPoint : DWord; 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 while(SERVICE_STOPPED svc_status.dwCurrentState)do begin dwCheckPoint := svc_status.dwCheckPoint; Sleep(svc_status.dwWaitHint); if(not QueryServiceStatus(h_svc,svc_status))then begin // couldn't check status break; end; if(svc_status.dwCheckPoint break; end; end; end; CloseServiceHandle(h_svc); end; CloseServiceHandle(h_manager); end; Result := SERVICE_STOPPED = svc_status.dwCurrentState; end; To get the status you need: function ServiceGetStatus(sMachine, sService: string ): DWord; var h_manager,h_service: 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; CloseServiceHandle(h_svc); end; CloseServiceHandle(h_manager); end; Result := hStat; end; It's returning one of the following constant's: SERVICE_STOPPED SERVICE_RUNNING SERVICE_PAUSED SERVICE_START_PENDING SERVICE_STOP_PENDING SERVICE_CONTINUE_PENDING or SERVICE_PAUSE_PENDING What you need is the unit WinSvc !