Solved

Creating a shortcut to a application

Posted on 1998-11-12
6
314 Views
Last Modified: 2010-04-04
Hi

how do i create a shortcut (programmatically in delphi) for my application in the Startup folder so that the application will start automatically with windows?

Regards
Keith
0
Comment
Question by:keithcsl
  • 3
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
BoRiS earned 50 total points
ID: 1346629
keithcsl

run this piece of code on the onCreate event or so...you could also add to a button click and ask the user if they want to add it to startup...


 uses
   Registry, //add to uses clause



   const MAX_PATH = 144;


  var
   reg: TRegistry;
 begin
   reg := TRegistry.Create;
   reg.RootKey := HKEY_LOCAL_MACHINE;
   reg.LazyWrite := false;
   reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',
               false);
   reg.WriteString('YourAppname', Application.ExeName);
   reg.CloseKey;
   reg.free;
 end;

this will place your app in the reg key above then ever time windows starts your app starts too...

Later
BoRiS
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1346630
keithcsl

here is the code to add a shortcut to destop or startup folder


uses

  ShlObj, ActiveX, ComObj, Registry; //add to uses clause



procedure TForm1.Button1Click(Sender: TObject);
var
  MyObject  : IUnknown;
  MySLink   : IShellLink;
  MyPFile   : IPersistFile;
  FileName  : String;
  Directory : String;
  WFileName : WideString;
  MyReg     : TRegIniFile;
begin
  MyObject := CreateComObject(CLSID_ShellLink);
  MySLink := MyObject as IShellLink;
  MyPFile := MyObject as IPersistFile;
  FileName := 'NOTEPAD.EXE';
  with MySLink do begin
    SetArguments('C:\AUTOEXEC.BAT');
    SetPath(PChar(FileName));
    SetWorkingDirectory(PChar(ExtractFilePath(FileName)));
  end;
  MyReg := TRegIniFile.Create(
    'Software\MicroSoft\Windows\CurrentVersion\Explorer');

// Use the next line of code to put the shortcut on your desktop
  Directory := MyReg.ReadString('Shell Folders','Desktop','');

// Use the next three lines to put the shortcut on your start menu
//  Directory := MyReg.ReadString('Shell Folders','Start Menu','')+
//      '\Whoa!';

//  CreateDir(Directory);
  WFileName := Directory+'\FooBar.lnk';
  MyPFile.Save(PWChar(WFileName),False);
  MyReg.Free;
end;


Later
BoRiS
0
 
LVL 2

Expert Comment

by:freter
ID: 1346631
this is boris' code tweaked up into an object:

>>>>>>>>>>>
unit Shortcut;

interface

uses ShlObj, ActiveX, ComObj, Windows, WinInfo;

// TARGET is the file that the shortcut will point to.
// ARGS is the command-line arguments you wish to pass to the Target app.
// WORKDIR is the directory that will be current when the link is launched.
// SHORTCUTNAME is the name of the shortcut link file.
// LOC is the location the file should go.

type
  TSpecialLocations = (slNone,slDesktop,slFavorites,slFonts,slNetHood,slPersonal,
                       slPrograms,slRecent,slSendTo,slStartMenu,slStartup,slTemplates);

procedure CreateLink(Target,Args,WorkDir,ShortCutName: String; Loc: TSpecialLocations);

implementation

procedure CreateLink(Target,Args,WorkDir,ShortCutName: String; Loc: TSpecialLocations);
var
  IObj    : IUnknown;
  Link    : IShellLink;
  IPFile  : IPersistFile;
  TargetW : WideString;
begin
  IObj := CreateComObject(CLSID_ShellLink);
  Link := IObj as IShellLink;
  IPFile  := IObj as IPersistFile;
  with Link do
    begin
      SetPath(PChar(Target));
      SetArguments(PChar(Args));
      SetWorkingDirectory(PChar(WorkDir));
    end;
  case Loc of
    slDesktop   : TargetW := WindowsInfo.DesktopDir+'\'+ShortCutName;
    slFavorites : TargetW := WindowsInfo.FavoritesDir+'\'+ShortCutName;
    slFonts     : TargetW := WindowsInfo.FontsDir+'\'+ShortCutName;
    slNetHood   : TargetW := WindowsInfo.NetHoodDir+'\'+ShortCutName;
    slPersonal  : TargetW := WindowsInfo.PersonalDir+'\'+ShortCutName;
    slPrograms  : TargetW := WindowsInfo.ProgramsDir+'\'+ShortCutName;
    slRecent    : TargetW := WindowsInfo.RecentDir+'\'+ShortCutName;
    slSendTo    : TargetW := WindowsInfo.SendToDir+'\'+ShortCutName;
    slStartMenu : TargetW := WindowsInfo.StartMenuDir+'\'+ShortCutName;
    slStartup   : TargetW := WindowsInfo.StartupDir+'\'+ShortCutName;
    slTemplates : TargetW := WindowsInfo.TemplatesDir+'\'+ShortCutName;
  else            TargetW := ShortCutName;
  end;
  IPFile.Save(PWChar(TargetW),False);
end;

end.

<<<<<<<<<<<
(taken from UNDU)

rgds, freter
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:keithcsl
ID: 1346632
Thanks Boris

if it's not too much to ask, how do i find out if there is already a shortcut in the startup menu/desktop ? don't want to create 2 or more of them...

regards
keith
0
 
LVL 1

Author Comment

by:keithcsl
ID: 1346633
Boris

I norealised that it only creates one instance of the link, which is great.

can i use the Reset procedure to tell if the shortcut is already there and use Erase to delete the link? is there a better way?

Regards
Keith
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1346634
keithcsl

You can check if it there by using if FileExists and then delete it if it does, with Delete like so... this is the easest way to do it as the registry will only allow one instance of the shortcut so check for the shortcut in the StartMenu Directory...

procedure TForm1.Button1Click(Sender: TObject);
var
Path: array [0..MAX_PATH] of char;
S: string;
begin
   GetWindowsDirectory(Path, Sizeof(Path));
    S := Path + '\Start Menu\Programs\StartUp\GetRight Monitor.lnk';
     if FileExists(S) then begin
      if MessageDlg('Yip, its there' + #13 + 'Would you Like to delete it.', mtConfirmation, [mbYES, mbNO],0) = mrYes then
        DeleteFile(S)
        else
          Exit;
     end;
end;

Later
BoRiS
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
code issue 8 100
Delphi: how to implement a User Shortcut mapper? 1 95
tidtcpserver connection lost handle 2 71
how to change, disabled button color FMX ? 1 18
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now