The typical way for letting an application start on Windows' startup, is to create a shortcut to its exe-file in the Startup folder of the Start menu. But you also can control this behaviour by making changes to the registry from within a Delphi program.
You can write a value to one of two keys in the Windows Registry under the HKEY_LOCAL_MACHINE root key:
Here's the Delphi source code for such a procedure:
procedure RunOnWinStart(ApTitle, ApPathFile: string; RunOnce: Boolean); var Reg: TRegistry; TheKey: string; begin Reg := TRegistry.Create; Reg.RootKey := HKEY_LOCAL_MACHINE; TheKey := 'Software\Microsoft\Windows\CurrentVersion\Run'; if RunOnce then TheKey := TheKey + 'Once'; // Open key, or create it if it doesn't exist Reg.OpenKey(TheKey, True); Reg.WriteString(ApTitle, ApPathFile); Reg.CloseKey; Reg.Free; end;The meaning of the parameters:
RunOnWinStart('TestProgram', 'c:\test\testprog.exe', True); RunOnWinStart('Calculator', 'calc.exe', False);
Note: in the second example, we don't have to give the full path of calc.exe, because Windows knows where to find this program.
When Windows starts up, it launches all the applications listed in the RunOnce key and all those in the Run key. Afterwards, the entries of the RunOnce key are deleted by Windows, but these in the Run key are left untouched. As a result, the "RunOnce" applications run just once, while the others -- you've got the picture ;)
But what if you want to remove entries from the Run key, safely and under
program control? Here's how:
© Copyright 1999-2019