Recent

Author Topic: Autostart application when window boot  (Read 13369 times)

lttung1197

  • New member
  • *
  • Posts: 9
Autostart application when window boot
« on: October 20, 2013, 03:35:25 pm »
i have googled but only found the solution on linux. I want the app to autostart itself, not by hand, and when move it to any location on pc, it autostarts too

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Autostart application when window boot
« Reply #1 on: October 20, 2013, 03:51:00 pm »
On Windows you find it on:

UserDir + \AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

You need to have a link to your program there.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Autostart application when window boot
« Reply #2 on: October 20, 2013, 03:52:50 pm »
You have found a solution on linux that will let your program autostart even after you move it somewhere else on the computer after the autostart registration with out re - registering it? I would like to know what that is.


In any case on windows there are 2 methods to do that
1) the startup folder on your start menu, add a short cut to your application there and it will autostart along with windows, if the program is move after the shortcut is created you need to update the shortcut with the new location.
2) use the registry HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\run and add a stringvalue with value equal to you full application name including the full path, after the application has been moved the value needs to updated with the new path to autostart your application.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lttung1197

  • New member
  • *
  • Posts: 9
Re: Autostart application when window boot
« Reply #3 on: October 20, 2013, 04:04:24 pm »
You have found a solution on linux that will let your program autostart even after you move it somewhere else on the computer after the autostart registration with out re - registering it? I would like to know what that is.

the solution i found is only autostart the program, not after move, but i want to move it to anywhere
About your 1st method, how to create shortcut of program to startup folder, and how to know the latest location of program.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Autostart application when window boot
« Reply #4 on: October 20, 2013, 04:15:56 pm »
1) How to create a lnk file using delphi
      http://stackoverflow.com/questions/7704265/how-to-create-a-file-shortcut-lnk-file-on-desktop-in-windows
2) How to know the file location
  a) using the installer have the installer autocreate the link for you I think inno setup has that ability.
  b) Application.ExeName has the full name of your application including the path where it started from.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

lttung1197

  • New member
  • *
  • Posts: 9
Re: Autostart application when window boot
« Reply #5 on: October 21, 2013, 03:45:24 pm »
Code: [Select]
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
   ComCtrls,  ActnList, StdCtrls, ExtCtrls,
 Menus,  RTTICtrls, crt ,
Windows,
  ShlObj, ActiveX, ComObj,
  ExtDlgs,win32proc;
//some uses are not necessary

var run:integer;
//run decides the autorunning on or off

procedure TForm1.Button7Click(Sender: TObject);
Var
   IObject : IUnknown;
   ISLink : IShellLink;
   IPFile : IPersistFile;
   TargetName : String;
   LinkName : WideString;

Begin
       
  if run=1 then
  begin
  TargetName :=application.ExeName;
    IObject := CreateComObject(CLSID_ShellLink) ;
    ISLink := IObject as IShellLink;
    IPFile := IObject as IPersistFile;

   with ISLink do
    begin
      SetPath(pChar(TargetName)) ;
      SetWorkingDirectory(pChar(ExtractFilePath(TargetName))) ;
    end;
 
 if (windowsversion=wv7) or (windowsversion=wvlater) then
     LinkName:='C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\Menkal.lnk';
  if windowsversion=wvxp then
     LinkName:='C:\Documents and Settings\All Users\Start Menu\Program\Startup\Menkal.lnk';
  IPFile.Save(PWChar(LinkName), false) ;
  end
         
 else
          begin
                 if (windowsversion=wv7) or (windowsversion=wvlater) then
     deletefile('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\Menkal.lnk');
  if windowsversion=wvxp then
     deletefile('C:\Documents and Settings\All Users\Start Menu\Program\Startup\Menkal.lnk');
      end;
 end; 

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject); 
begin
 button7.Click;
end;

Code: [Select]
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
 begin
     button7.Click;
end;

this is the code i use, it can autorun program even change location of program, thanks taazz, maybe helpful for someone
And, i found that if i make a shortcut of a program to startup folder, then i move the program to another location, and double click the shortcut again, the program still open, so i check back the shortcut, program's location of it auto changed.  :o . i work on win 7, i don't know what happen in xp
regard :))

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Autostart application when window boot
« Reply #6 on: October 21, 2013, 03:58:10 pm »
yes there is an auto search feature (since XP) when you execute a link and the file it was supposed to link to is not there it searches for it and if found corrects the path. Most of the time this will work correctly some times you will see a dialog along the lines of " the original file was not found and a new file looks like the application you are looking for. Do you want to update the link to that file?" and in some extreme cases the link is made to the wrong file and now it opens something different than expected. The last case I seen it only once or twice in my life and I assume that it was the auto search that made the error. I have tried to repeat the behavior but I never managed to do so.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

sam707

  • Guest
Re: Autostart application when window boot
« Reply #7 on: October 21, 2013, 05:30:34 pm »
Personally I use a TRegistry

changing key :

HKey_CURRENT_USER\Software\Microsoft\Windows\CurentVersion\Run

creating or updating a key with my app name and seting its value to ParamStr(0) or whatever full path to the program you want to run at windows startup will do it nicely ! (u even can add command line args hehehe)

Works on all windows without need to care about "Roaming toaster" nebulae which did not exist before vista

if the program needs to start for all user, set the same key just changing HKEY_CURRENT_USER to HKEY_LOCAL_MACHINE\Software.....etc . in that case you'll need privileges to set the key (admin)
« Last Edit: October 21, 2013, 05:40:58 pm by sam707 »

lttung1197

  • New member
  • *
  • Posts: 9
Re: Autostart application when window boot
« Reply #8 on: October 21, 2013, 07:05:54 pm »
so, when i do that, the program moving is no problem?

Works on all windows without need to care about "Roaming toaster" nebulae which did not exist before vista

i don't understand much about this sentence. do it work on xp? , what is roaming toaster nebulae?
« Last Edit: October 22, 2013, 09:05:53 am by lttung1197 »

 

TinyPortal © 2005-2018