I needed to somehow disable windows update with a project in Delphi. I've searched the internet but I still can not find anything for that purpose.
Can anyone give me a hand or an idea how to do it?
I needed to somehow disable windows update with a project in Delphi. I've searched the internet but I still can not find anything for that purpose.
Can anyone give me a hand or an idea how to do it?
Try this code that I used in the past, to disable or enable Winodws update, I use the command line to get it. In a new project create a button and a memo on the button call the ChangeWindowsUpdate
procedure.
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 please let me know.
Windows update is a windows service, named wuauserv
. By default, it gets stuck, and with the manual startup type. To deactivate, you have to change the startup type to desativado
and for this, just run the command:
sc config wuauserv start= disabled
Passing this to delphi would look like this:
WinExec(PAnsiChar('cmd.exe /c sc config wuauserv start= disabled'), sw_hide );