How To Start Your Program Automatically (Using Delphi)

This article will show how to modify the Windows registry to permit your program be automatically started on startup. Have care to change the registry because an incorrect alteration may prejudice your operational system.

Step 1

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;

 

Step 2

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;

Step 3

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;

 

Step 4

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;

 


Click on the first button. When you restart your computer, the program will be executed automatically. If you don't want the program starts automatically, just click the second button.  

 

Share this article!

Follow us!

Find more helpful articles: