Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating a shortcut to a application

Posted on 1998-11-12
6
Medium Priority
?
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

636 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