delphi关闭和禁用Windows服务

function StopServices(const SvrName: string): Boolean;
var
  SCH, SvcSCH: SC_HANDLE;
  SS: TServiceStatus;
begin
  Result := False;
  SCH := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);
  if SCH <= 0 then Exit;

  SvcSCH := OpenService(SCH, PChar(SvrName), SERVICE_ALL_ACCESS);
  if SvcSCH <= 0 then Exit;

  try
    Result := ControlService(SvcSCH, SERVICE_CONTROL_STOP, SS);

    if Result then
    begin
      Result := ChangeServiceConfig(SvcSCH, SERVICE_NO_CHANGE, SERVICE_DISABLED,
        SERVICE_NO_CHANGE, nil, nil, nil, nil, nil, nil, nil);
    end;
  finally
    CloseServiceHandle(SCH);
    CloseServiceHandle(SvcSCH);
  end;
end;

调用:

StopServices('WSearch')

猜你喜欢

转载自www.cnblogs.com/onlyou13/p/12041146.html
今日推荐