首页 > 代码库 > 系统服务

系统服务

function StopServies(sServiceName: String): boolean; // 停止某个服务;
var
  schService: SC_HANDLE;
  schSCManager: SC_HANDLE;
  ssStatus: TServiceStatus;
begin
  schSCManager := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  schService := OpenService(schSCManager, Pchar(sServiceName), SERVICE_ALL_ACCESS);
  try
    if ControlService(schService, SERVICE_CONTROL_STOP, ssStatus) then
    begin
      Sleep(1000);
      while (QueryServiceStatus(schService, ssStatus)) do
      begin
        Application.ProcessMessages;
        if ssStatus.dwCurrentState = SERVICE_STOP_PENDING then Sleep(1000)
        else break;
      end; // while
      if ssStatus.dwCurrentState = SERVICE_STOPPED then result := True
      else result := False;
    end
    else result := False;
  finally
    CloseServiceHandle(schService);
    CloseServiceHandle(schSCManager);
  end;
end;