Implementation: We will create two procedures, WriteRegistry and DeleteRegistry, to add and remove our registry respectively.
To work with the registry we will use the class TRegistry, which we will add in clause uses. Code: uses
...Windows, StdCtrls, Registry;
Now we will create the WriteRegistry with the parameters. Procedure:
WriteRegistry(Root: HKEY; Key, Value, Address: string); Where:- Raiz (HKEY): is where the registry will be added. You may use HKEY_LOCAL_MACHINE (to affect all users) or HKEY_CURRENT_USER (to affect only the logged user). - Key (String): we will use "Software\Microsoft\Windows\CurrentVersion\Run" - Value (String): it is the value that will be added. - Address (String): it is the executable address which will be initialized.
Code:
procedure TForm1.WriteRegistry(Root: HKEY; Key, Value, Address: string);
var
Registry: TRegistry;
begin
Registry := TRegistry.Create(KEY_WRITE); // Call the object constructor Registry.RootKey := Root; //Define the root key
Registry.OpenKey(Key, True); //Create the Key
Registry.WriteString(Value, '"' + Adress + '"'); //Record the adress Registry.CloseKey; // Close the key and objectRegistry.Free;
end;
Now we will create the DeleteRegistry procedure.
DeleteRegistry(Root: HKEY; Key, Value : string); Code: procedure TForm1.DeleteRegistry(Root: HKEY; Key, Value: string);
var
Registry: TRegistry;
begin
Registry := TRegistry.Create(KEY_WRITE); // Call the object constructor Registry.RootKey := Root;
Registry.OpenKey(Key, True); //Create the key Registry.DeleteValue(Valor); //Delete the value Registry.CloseKey; // Close the key and object
Registry.Free;
end;
We will create two buttons, one to write and other to delete the registry.
Code: procedure TForm1.BitBtn1Click(Sender: TObject);
begin
try
WriteRegistry(HKEY_LOCAL_MACHINE, 'Software\Microsoft\Windows\CurrentVersion\Run',
'BeginProgram', ExtractFilePath(Application.ExeName) + 'TesteRegistro.exe');
MessageDlg('Recorded registry with sucess!', mtInformation, [mbOk], 0);
except
MessageDlg(‘Error to create registry!', mtInformation, [mbOk], 0);
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
try
DeleteRegistry(HKEY_LOCAL_MACHINE, 'Software\Microsoft\Windows\CurrentVersion\Run',
'BeginProgram');
MessageDlg('Deleted registry with sucess!', mtInformation, [mbOk], 0);
except
MessageDlg(‘Error to delete registry!', mtInformation, [mbOk], 0);
end;
end;


Delicious
Digg
Google
Yahoo