Wednesday, January 5, 2011

Make Your Programs Run on Windows Startup

To easiest way to make your programs run on Windows startup is to make an entry in the system registry.
To make you program run on all user profiles you have to change the RootKey variable to HKEY_LOCAL_MACHINE else don’t change it, HKEY_CURRENT_USER is set by default.
If you want your program to run only once at Windows startup then choose the key with the RunOnce subkey else choose the Run subkey.
The valid keys to make the entry in are:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
Here’s the code:

uses
Registry;

with TRegistry.Create do
try

{ If you want your programs to run on all user profiles include this }
RootKey := HKEY_LOCAL_MACHINE;

{ If you want you program to run only once include this }
OpenKey('SoftwareMicrosoftWindowsCurrentVersionRunOnce', True);
{ Else include this }
OpenKey('SoftwareMicrosoftWindowsCurrentVersionRun', True);

(* sProgramName - (string) The name of your program. Could be anything you want.
* sCommandLine - (string) The full path to your program executable with
* commandline options (if any).
*)

WriteString(sProgramName, sCommandLine);
CloseKey;
finally
Free;
end;
 
 
 
 
It's
easy enough to drag and drop your application to the Windows Startup
group to make it run on Windows startup. But, if you wanted to do this
from your program, at the end of your setup program for example, or if
you wanted to make your program run just once the next time Windows
start, following function might come in handy:

 
procedure RunOnStartup(
  sProgTitle,
  sCmdLine    : string;
  bRunOnce    : boolean );
var
  sKey : string;
  reg  : TRegIniFile;
begin
  if( bRunOnce )then
    sKey := 'Once'
  else
    sKey := '';

  reg := TRegIniFile.Create( '' );
  reg.RootKey := HKEY_LOCAL_MACHINE;
  reg.WriteString(
    'SoftwareMicrosoft'
    + 'WindowsCurrentVersionRun'
    + sKey + #0,
    sProgTitle,
    sCmdLine );
  reg.Free;
end;
 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum