Ask the Delphi Pro 10-Minute Solutions
 |
Running a Program at Startup, Sans The Shortcuts
By Brendan Delumpa
Here's the scenario: You download a program off the Internet (or get a disk from someone). The program installs fine, but every time you start up Windows, the program launches as well, with an annoying splash screen or dialog box saying that the program is unregistered. And the worst part is that its shortcut isn't even in the Startup folder. How in the world is the program loading at Windows startup without its shortcut being in your Startup folder? The answer to that question lies in the Registry.
You may already know this, but the Registry, in both Windows NT and Windows 9x, contains two subkey entries under the \HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion key called Run and RunOnce, that are used to automatically launch programs when Windows starts. As their names imply, Run denotes running a program every time Windows starts, and RunOnce denotes running a program automatically only the next time Windows starts, and never after that. Once you master these two, it will be a snap to set which programs you launch at startup, and which you want to call up manually. Below, you'll find the code listing that will give you the ability to insert your program's command line in either Run or RunOnce.
The following procedure instructs Windows at startup to execute your program. Here's a summary of the formal parameters:
| KeyTitle: |
Title of the key value for your program. Note that this is actually a superfluous parameter, and can be any value. 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 launch just once after you write to the registry. After 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(KeyTitle,
Key Title can be anything you want
CommandLn : String;
Use only a fully-qualified file name (path/ExeName)
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,
KeyTitle, CommandLn)
else
RegIniFile.WriteString('Software\Microsoft\Windows
\CurrentVersion\Run'#0,
KeyTitle, CommandLn);
Free;
end;
end;
As you can see, all I'm using is the TregIniFile object to insert entries into the registry. I prefer using this object because it wraps a lot of the complexities of TRegistry into a familiar interface carried over from Win16. To use the procedure, simply supply a title for the key, the command line for your program, and specify whether or not you want to only run your program once.
That's it! And now you have a way for to run your own insidious splash screens and registration reminders like other people do by inserting entries into the Run and RunOnce keys. (NOTE: I am not promoting this practice - it's just a point of humor).
|
|
|