Author: Ernesto De Spirito
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:
1
2 {=====================================================================
3 The following procedure instructs Windows at startup to execute your
4 program. Here's a summary of the formal params:
5
6 WindowTitle : Title of the Window of your program. Note that this is
7 actually a superfluous parameter, and can be any value
8 you want. But for convention's sake, and because the
9 registry entry expects a value, you have to provide it.
10
11 CommandLn : This is the fully qualified path and executable name of
12 your program (e.g. 'C:\MyProgams\MyProgam.exe.' If you
13 have any command line parameters, you include them in
14 this string as well.
15
16 RunOnlyOnce : Setting this to true makes the program only launch just
17 once after you write to the registry. Once it's launched,
18 Windows will delete its entry from the Run path in the
19 registry. Set it to False if you want your program to
20 always launch when Windows starts up.
21 =====================================================================}
22
23 procedure RunOnStartup(WindowTitle,
24 CommandLn: string;
25 RunOnlyOnce: Boolean);
26 var
27 RegIniFile: TRegIniFile;
28 begin
29 RegIniFile := TRegIniFile.Create('');
30 with RegIniFile do
31 begin
32 RootKey := HKEY_LOCAL_MACHINE;
33 if RunOnlyOnce then
34 RegIniFile.WriteString('Software\Microsoft\Windows\' +
35 'CurrentVersion\RunOnce'#0,
36 WindowTitle, CommandLn)
37 else
38 RegIniFile.WriteString('Software\Microsoft\Windows\' +
39 'CurrentVersion\Run'#0,
40 WindowTitle, CommandLn);
41 Free;
42 end;
43 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:
44 uses Registry;
45
46 procedure SetAutoStart(AppName, AppTitle: string; registerit: boolean);
47 const
48 RegKey = '\Software\Microsoft\Windows\CurrentVersion\Run';
49 // or: RegKey = '\Software\Microsoft\Windows\CurrentVersion\RunOnce';
50 var
51 Registry: TRegistry;
52 begin
53 Registry := TRegistry.Create;
54 try
55 Registry.RootKey := HKEY_CURRENT_USER;
56 if Registry.OpenKey(RegKey, False) then
57 begin
58 if registerit = false then
59 Registry.DeleteValue(AppTitle)
60 else
61 Registry.WriteString(AppTitle, AppName);
62 end;
63 finally
64 Registry.Free;
65 end;
66 end;
67
68 procedure TForm1.Button1Click(Sender: TObject);
69 begin
70 SetAutoStart('Your Application Title', ParamStr(0), false);
71 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:
72 procedure TForm1.Button1Click(Sender: TObject);
73 begin
74 SetRegistryData(HKEY_LOCAL_MACHINE,
75 'Software\Microsoft\Windows\CurrentVersion\Run',
76 Application.Title, rdString, Application.ExeName);
77 end;
NOTE: SetRegistryData has been featured in the article "Accessing the Windows
Registrydkb://312979072".
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).
|