Solved

Creating a shortcut to a application

Posted on 1998-11-12
6
317 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Path  to current project in Delphi. 2 90
Best Firemonkey component pack 1 116
SUM 2 INTEGER ARRAYS INTO 1 10 108
scroll down TListBox component in Delphi 1 19
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

789 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