Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Creating a shortcut to a application

Posted on 1998-11-12
6
Medium Priority
?
325 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

810 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