Mirror

Launching a Program at Windows Startup (Views: 101)


Problem/Question/Abstract:

There are a few programs installed on my system that launch when Windows starts, but don't have shortcuts in my Startup folder. Is there some trick that I'm missing here?

Answer:

Solve 1:

Are you kiddin'? That capability isn't clearly documented anywhere? Okay, okay, enough of the facetiousness... In actuality, it is a bit of a trick to get a program to do this (primarily because it's not something that's very well-documented), but it's not trickery of any sort that would prevent you from being able to program this yourself. All it involves is writing to one of two paths in the Windows registry under the HKEY_LOCAL_MACHINE root key:

Software\Microsoft\Windows\CurrentVersion\RunOnce
-or-
Software\Microsoft\Windows\CurrentVersion\Run

As you've probably surmised, writing an entry to the "RunOnce" key will make it so your program only launches after the next shutdown and startup of Windows. Writing an entry to the "Run" key will make your program launch each time Windows is started.

Here's a quick procedure that'll do either action for you. We'll discuss it after I list the code:

{=====================================================================
The following procedure instructs Windows at startup to execute your
program. Here's a summary of the formal params:

WindowTitle  : Title of the Window of your program. Note that this is
                actually a superfluous parameter, and can be any value
                you want. But for convention's sake, and because the
                registry entry expects a value, you have to provide it.

CommandLn    : This is the fully qualified path and executable name of
                your program (e.g. 'C:\MyProgams\MyProgam.exe.' If you
                have any command line parameters, you include them in
                this string as well.

RunOnlyOnce  : Setting this to true makes the program only launch just
                once after you write to the registry. Once it's launched,
                Windows will delete its entry from the Run path in the
                registry. Set it to False if you want your program to
                always launch when Windows starts up.
=====================================================================}

procedure RunOnStartup(WindowTitle,
  CommandLn: string;
  RunOnlyOnce: Boolean);
var
  RegIniFile: TRegIniFile;
begin
  RegIniFile := TRegIniFile.Create('');
  with RegIniFile do
  begin
    RootKey := HKEY_LOCAL_MACHINE;
    if RunOnlyOnce then
      RegIniFile.WriteString('Software\Microsoft\Windows\' +
        'CurrentVersion\RunOnce'#0,
        WindowTitle, CommandLn)
    else
      RegIniFile.WriteString('Software\Microsoft\Windows\' +
        'CurrentVersion\Run'#0,
        WindowTitle, CommandLn);
    Free;
  end;
end;

Notice that the RegIniFile instance variable above is of type TRegIniFile, as opposed to TRegistry. TRegIniFile is a descendant class of TRegistry and inherits all its methods and properties. And in addition to all that, it adds comparable methods of TIniFile, the tried and true Windows 3.1 class. This allows us to treat the registry like an INI file, which is far easier to work with than accessing the registry through TRegistry. This is one of the things I just love about Delphi! Want to make it simple? Subclass and extend a class' functionality!

Employing the Procedure

So where should you employ this? The most likely place is to use the procedure with system tray applications that you always want to run when Windows starts up. Personally, I make a call to the function in the OnClose  event of the main form of my application. That way, I always know that even if Windows is shutdown, my program will make sure that its Run or RunOnce entry is written to the Registry.

Another place you might want to use this procedure is with readme or help files that accompany a program that you install on another computer, ala Microsoft Intellipoint Mouse help...


Solve 2:

uses Registry;

procedure SetAutoStart(AppName, AppTitle: string; registerit: boolean);
const
  RegKey = '\Software\Microsoft\Windows\CurrentVersion\Run';
  // or: RegKey = '\Software\Microsoft\Windows\CurrentVersion\RunOnce';
var
  Registry: TRegistry;
begin
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_CURRENT_USER;
    if Registry.OpenKey(RegKey, False) then
    begin
      if registerit = false then
        Registry.DeleteValue(AppTitle)
      else
        Registry.WriteString(AppTitle, AppName);
    end;
  finally
    Registry.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetAutoStart('Your Application Title', ParamStr(0), false);
end;


Solve 3:

One way is placing a direct access to the application in the Startup folder of Windows Start Menu. Alternatively, you can add a value under the appropriate key in the Windows Registry, as shown below:

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetRegistryData(HKEY_LOCAL_MACHINE,
    'Software\Microsoft\Windows\CurrentVersion\Run',
    Application.Title, rdString, Application.ExeName);
end;

NOTE: SetRegistryData has been featured in the article "Accessing the Windows Registry".

Instead of Application.Title you can write a string with a unique name for the application, and instead of Application.ExeName you can write the full path name of the application (as well as its command-line parameters if they are needed).

<< Back to main page