Advertisement
Gov_777

Прибить службу

May 30th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.32 KB | None | 0 0
  1. function ServiceStop(aMachine, aServiceName: string): boolean;
  2. // aMachine это UNC путь, либо локальный компьютер если пусто
  3. var
  4.   h_manager, h_svc: SC_Handle;
  5.   svc_status: TServiceStatus;
  6.   dwCheckPoint: DWord;
  7. begin
  8.   h_manager := OpenSCManager(PChar(aMachine), nil, SC_MANAGER_CONNECT);
  9.   if h_manager > 0 then
  10.   begin
  11.     h_svc := OpenService(h_manager, PChar(aServiceName), SERVICE_STOP or SERVICE_QUERY_STATUS);
  12.     if h_svc > 0 then
  13.     begin
  14.       if (ControlService(h_svc, SERVICE_CONTROL_STOP, svc_status)) then
  15.       begin
  16.         if (QueryServiceStatus(h_svc, svc_status)) then
  17.         begin
  18.           while (SERVICE_STOPPED <> svc_status.dwCurrentState) do
  19.           begin
  20.             dwCheckPoint := svc_status.dwCheckPoint;
  21.             Sleep(svc_status.dwWaitHint);
  22.             if (not QueryServiceStatus(h_svc, svc_status)) then
  23.             begin// couldn't check status
  24.               break;
  25.             end;
  26.             if (svc_status.dwCheckPoint < dwCheckPoint) then
  27.               break;
  28.           end;
  29.         end;
  30.       end;
  31.       CloseServiceHandle(h_svc);
  32.     end;
  33.     CloseServiceHandle(h_manager);
  34.   end;
  35.   Result := SERVICE_STOPPED = svc_status.dwCurrentState;
  36. end;
  37. //применение
  38. //ServiceStop(string.empty, 'OkayFreedom VPN Starter Service');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement