Ads 468x60px


Kamis, 07 Juli 2011

Launching a Program at Windows Startup

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?

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:

1. Software\Microsoft\Windows\CurrentVersion\RunOnce -or-
2. 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);
RegIniFile : TRegIniFile;
RegIniFile := TRegIniFile.Create('');
with RegIniFile do begin
if RunOnlyOnce then
RegIniFile.WriteString('Software\Microsoft\Windows\' +
WindowTitle, CommandLn)
RegIniFile.WriteString('Software\Microsoft\Windows\' +
WindowTitle, CommandLn);

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...

In any case, I'm sure you'll find a good use for it.

0 komentar:

Posting Komentar