How to disable windows update in Delphi

Asked

Viewed 287 times

1

I needed to be able to somehow disable windows update with a Delphi project. I’ve searched the Internet but I haven’t been able to find anything for the purpose.

Could someone give me a help or an idea of how to do it?

  • Have you ever thought about doing this by changing the record? You create a . reg and run by Delphi.

  • what is your idea has some example?

  • because instead of you worrying about a code for it, you would only run a . reg, you should easily find one. reg on the internet to disable updates and also some code to run . reg in Delphi and then if anything happens just change the reg instead of your code.

2 answers

4

windows update is a windows service, named after wuauserv. By default, it gets Stopped, and with manual boot type. To disable, you have to change the boot type to desativado and for that, just execute the command:

sc config wuauserv start= disabled

Passing it to Delhi, it would look like this:

WinExec(PAnsiChar('cmd.exe /c sc config wuauserv start= disabled'), sw_hide );
  • 1

    thanks for the attention the tmc code worked for me

3


Try this code I used before, to disable or enable Winodws update, I use the command line to get it. In a new project create a button and a memo in the button call the procedure ChangeWindowsUpdate.

I leave the example:

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    procedure ChangeWindowsUpdate;
    function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
  public
    { Public declarations }
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ChangeWindowsUpdate;
end;

Function TForm1.GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
var SA: TSecurityAttributes;
    SI: TStartupInfo;
    PI: TProcessInformation;
    StdOutPipeRead, StdOutPipeWrite: THandle;
    WasOK: Boolean;
    Buffer: array[0..255] of AnsiChar;
    BytesRead: Cardinal;
    WorkDir: string;
    Handle: Boolean;
begin
  Result := '';
  with SA do
    begin
      nLength := SizeOf(SA);
      bInheritHandle := True;
      lpSecurityDescriptor := nil;
    end;
  CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
  try
    with SI do
      begin
        FillChar(SI, SizeOf(SI), 0);
        cb := SizeOf(SI);
        dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
        wShowWindow := SW_HIDE;
        hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
        hStdOutput := StdOutPipeWrite;
        hStdError := StdOutPipeWrite;
      end;
    WorkDir := Work;
    Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine), nil, nil, True, 0, nil, PChar(WorkDir), SI, PI);
    CloseHandle(StdOutPipeWrite);
    if Handle then
      try
        repeat
          WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
          if BytesRead > 0 then
            begin
              Buffer[BytesRead] := #0;
              Result := Result + Buffer;
            end;
        until not WasOK or (BytesRead = 0);
        WaitForSingleObject(PI.hProcess, INFINITE);
      finally
        CloseHandle(PI.hThread);
        CloseHandle(PI.hProcess);
      end;
  finally
    CloseHandle(StdOutPipeRead);
  end;
end;

procedure TForm1.ChangeWindowsUpdate;
var APos: Integer;
    ALine, AState: String;
begin
  Memo1.Clear;
  //sc query wuauserv vai pegar o estado do windows update (parado,correr...)
  Memo1.Text := trim(GetDosOutput('sc query wuauserv'));
  Application.ProcessMessages;

  //vai ler a linha correspondente ao status
  if Memo1.Lines[2] <> '' then
    Begin
      ALine := Trim(AnsiUpperCase(Memo1.Lines[2]));    // exemplo da linha: "STATE              : 1  STOPPED"

      APos := Pos(': ', ALine);
      if (APos > 0) then Delete(ALine, 1, APos);
      APos := Pos('  ', ALine);
      if (APos > 0) then Delete(ALine, 1, APos);

      //vai ler o estado 
      AState := Trim(ALine);
      if (AState = 'RUNNING') then
        Begin
          //para o serviço
          Memo1.Lines.Add(GetDosOutput('sc stop wuauserv'));
          Sleep(5000);
          //desabilita o windows update
          Memo1.Lines.Add(GetDosOutput('sc config wuauserv start= disabled'));
          memo1.Lines.Add('--AutoUpdates Service Stopped--');
        End
      else if (AState = 'STOPPED')then
        Begin
          //habilita o windows update
          Memo1.Lines.Add(GetDosOutput('sc config wuauserv start= demand'));
          Sleep(1000);
          //inicia o serviço
          Memo1.Lines.Add(GetDosOutput('sc start wuauserv'));
          memo1.Lines.Add('--AutoUpdates Service Started--');
        End
      else memo1.Lines.Add('AutoUpdates Service Not Found');
    End;
end;

Any questions warn.

  • Thanks worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.