Startup with Windows

Hi,

Anyone has a good method to start application automatically when the Windows started up ?

Function that can be used in a "preference" like this for example:

[ ] Start application automatically

[ Apply ] [ Cancel ]

That works with at least Windows 98/XP/2000.

Thanks.
kapotAsked:
Who is Participating?
 
Scay7Connect With a Mentor Commented:
just to add onto what the others said... :P great comments

I suggest a procedure like "doauto(query : boolean;user : string)"
where query is, is a true/false... true being set it false being remove it
where user is, is to which group... either current user or all users


put a 3 radiobuttonts somewhere on you form eg > settings > automatic startup or what ever

radiobutton1 = AutoRun - All Users
radiobutton2 = AutoRun - Current User
radiobutton3 = Disable

 ### code  ###
first put the procedure in :

type
  TForm1 = class(TForm)
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
    procedure RadioButton3Click(Sender: TObject);
    procedure doauto(query : boolean;user : string);  // $#$ <---- like this over here $#$
  private
    { Private declarations }
  public
    { Public declarations }
  end;


Then you have :

procedure Tform1.doauto(query : boolean;user : string);
var
  Reg : TRegistry;
begin
if query = true then
begin
  Reg := TRegistry.Create;
  try
      if user = 'single' then Reg.RootKey := HKEY_CURRENT_USER;
      if user = 'all' then Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', True) then
    begin
    Reg.WriteString('AppName','AppPath');
    //appname - what ever you want the key to be called
    //apppath - FULL dir of where you app is installed eg c:\program files\test\test.exe
    Reg.CloseKey;
    end;
  finally
    Reg.Free;
    inherited;
  end;
end;
if query = false then
begin
  Reg := TRegistry.Create;
  try
    if user = 'alsi' then Reg.RootKey := HKEY_CURRENT_USER;
    Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', false);
      if Reg.DeleteValue('AppName') then showmessage('The Automatic Start-Up, has been Disabled for the current user...');
    //appname - the name of the key you specified above
    if user = 'alsi' then Reg.RootKey := HKEY_LOCAL_MACHINE;
    Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', false);
      if Reg.DeleteValue('AppName') then showmessage('The Automatic Start-Up, has been Disabled for all users...');
    //appname - the name of the key you specified above
  finally
    reg.CloseKey;
    Reg.Free;
    inherited;
  end;
end;

end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
doauto(true,'all'); // set auto for all users :p
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
doauto(true,'single'); // set auto for current user :P
end;

procedure TForm1.RadioButton3Click(Sender: TObject);
begin
doauto(false,'alsi'); // disable auto for both groups
end;

### CODE ###


Peace Scay7

0
 
Tomas Helgi JohannssonCommented:
   Hi!
The simplest way and works great in all Windows environments.
   In the Setup of your application put a shortcut in the Program Files\Startup folder.

Regards,
  Tomas Helgi
0
 
calinutzCommented:
Other way is to add to the registry in HKEY_Local_Machine/SOFTWARE/Microsoft/Windows/CurrentVersion/Run (or RunOnce) the path to your executable.

Regards
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Mohammed NasmanSoftware DeveloperCommented:
Hello,

Put your application in the Windows Registry, there are some keys that will make your application run with windows each time its started
Here's an example to put your application name

uses
  Registry;

procedure TForm1.Button1Click(Sender: TObject);
var
  Reg : TRegistry;
begin
  Reg := TRegistry.Create();
  Reg.RootKey := HKEY_CURRENT_USER;
  Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false);
  Reg.WriteString('AppName','AppPath');
  Reg.CloseKey;
  Reg.Free;
end;
if you need your application to run for one time, replace the key with this one

'\Software\Microsoft\Windows\CurrentVersion\RunOnce'

HTH

Regards,
Mohammed
0
 
calinutzCommented:
For this you can also use ready made delphi components that enables you to autorun on windows startup. Very easy to use.
Check out this component free for non comercial use:
http://www.abf-dev.com/abf-components.shtml

Or you can easily use the Registry to write your code into the registry so that your program start on Windows startup.
0
 
calinutzCommented:
:)
0
 
TheRealLokiSenior DeveloperCommented:
you could also turn your application into a service. If you have the startmode as automatic, it will start when windows starts, even if nobody logs on.
Depending on your verison of delphi, you can make a service by going alt-new-and choosing service application from teh list

f you want a real easy way to turn your application into a service, you can just use SVRANY.exe to run your app as a service.
This is available in the windows resource toolkit, and probably a million places online.
0
 
CodedKCommented:
Hi.

A smaller code similar to mnasman code :
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
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( 'Software\Microsoft'   + '\Windows\CurrentVersion\Run'  + sKey + #0, sProgTitle, sCmdLine );
  reg.Free;
end;
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
sProgTitle:
Name of your program.

sCmdLine:
This is the full path name to your executable program.

bRunOnce:
Set this to True if you want to run your program just once. If this parameter is False, specified program will be executed every time Windows starts up.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Hope this helps.
0
 
kapotAuthor Commented:
Scay7, your code looks promising. I will try it tonight and let you know the outcome.
0
 
Scay7Commented:
and ?
0
 
kapotAuthor Commented:
It works :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.