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

 
Other 10-Minute Solutions
 Trapping Messages Sent to an Application
 Getting the Number of Records From a Fixed-Length ASCII File
 Performing Incremental Searches with a TListbox
 Resizing the Drop-down List of a TComboBo
 Disabling the System Keys from Your Application
 Making A Secondary Form Independent of the Main Form
 Creating a System Tray Application
 Opening and Closing a CD Tray
 Hiding an Application from Windows
 Make a List Box Track the Mouse
 Running a Program at Startup
 Creating A Skin Component
 Dynamically Load Components From Packages at Run Time
 Obtain All Values from Multivalue Input Fields in WebBroker Applications
 Migrate Your BDE Applications to Linux with dbExpress
 Create a Multiline Button Component
 Update and Maintain dbExpress's Unidirectional, Read-Only Datasets
 Use ActionBands to Enable End Users to Customize Your Delphi Applications
 Produce HTML Reporting Output with WebBroker Components
  Convert XML Documents into Different Formats with the XSL Template Language


Ask the Delphi Pro | Who is the Pro? | Usage Policies | Ask a Question | Search | Feedback


Sponsored Links


Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers