1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| const
Key = 'Software\Microsoft\Windows\CurrentVersion\Run';
var
Value :string;
Params :string;
begin
with TRegistry.Create do
try
RootKey := HKEY_LOCAL_MACHINE;
Value := ExtractFileName(Application.ExeName);
//La valeur n'existe pas ou donnée incorrecte
if not OpenKeyReadOnly(Key) or
not ValueExists(Value) or
not SameText(ReadString(Value), Application.ExeName) then
begin
//Confirmer l'inscription
if MessageDlg('Privilèges administrateur requis. Continuer ?', mtConfirmation, mbYesNo, 0) = mrYes then
begin
Params := Format('add HKLM\%s /v %s /t REG_SZ /d %s /f', [Key, Value, Application.ExeName]);
//Execution avec élévation
ShellExecute(0, 'runas', 'reg.exe', PChar(Params), nil, SW_HIDE);
end;
end
else MessageDlg('Commande déjà activée !', mtInformation, [mbOk], 0);
finally
Free;
end;
end; |
Partager