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')

 

你可能感兴趣的:(delphi关闭和禁用Windows服务)