In Delphi, you can add your application to startup in several ways. One way is to create a Windows shortcut to your application and then add that shortcut to the Startup folder in the Start menu. Here are the steps in detail:
- Locate the executable file of your Delphi application. This is typically located in the project folder, in a subfolder called “Win32” or “Win64” depending on your build configuration.
- Right-click on the executable file and select “Create Shortcut” from the context menu. A new shortcut will be created in the same folder.
- Right-click on the newly created shortcut and select “Cut” from the context menu.
- Press the Windows key + R on your keyboard to open the Run dialog. Type “shell:startup” (without quotes) and press Enter. This will open the Startup folder in the Start menu.
- Right-click on an empty space in the Startup folder and select “Paste” from the context menu. This will add the shortcut to your application to the Startup folder.
- Close the Startup folder and restart your computer. Your application should now run automatically when Windows starts.
Another way is to add the following code in the FormCreate event of the mainform of your application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
procedure TForm1.FormCreate(Sender: TObject); begin with TRegistry.Create do try RootKey := HKEY_CURRENT_USER; if OpenKey(‘\Software\Microsoft\Windows\CurrentVersion\Run’, True) then begin WriteString(‘YourApp’, Application.ExeName); CloseKey; end; finally Free; end; end; |
This will add the application to the windows registry and the application will start automatically whenever the user logs in.
Note: Make sure you have the necessary permissions and your application will be compatible with the system requirements.
Another way to add your Delphi application to startup is by using the Windows API function “ShellExecute” to create a scheduled task that runs your application at startup. Here are the steps in detail:
- In your Delphi project, add the “Windows” unit to the uses clause of your main form’s unit.
- In the FormCreate event of your main form, add the following code:
1 2 3 4 5 6 7 8 |
var AppPath: string; AppName: string; begin AppPath := ExtractFilePath(Application.ExeName); AppName := ExtractFileName(Application.ExeName); ShellExecute(0, ‘open’, ‘schtasks.exe’, ‘/create /tn “‘ + AppName + ‘” /tr “‘ + AppPath + AppName + ‘” /sc onstart’, nil, SW_HIDE); end; |
This code retrieves the path and name of your application’s executable file, and then uses the ShellExecute function to create a scheduled task that runs your application at startup. The “/create” parameter creates a new task, the “/tn” parameter specifies the name of the task, the “/tr” parameter specifies the path to the application’s executable file, and the “/sc onstart” parameter specifies that the task should run at startup.
- After you run the application once, it will create the scheduled task in windows task scheduler, and it will start automatically every time the user logs in.
It’s important to note that in order to use the Windows API function “ShellExecute” to create a scheduled task, the user must have administrative privileges.
You can also use the third party software like nssm, to install your application as a windows service.
In summary, there are several ways to add your Delphi application to startup. You can create a Windows shortcut and add it to the Startup folder, add registry keys or use the Windows API to create a scheduled task. Each method has its own advantages and disadvantages, so you should choose the one that best suits your needs.
Leave a Reply