Solved

Creating a shortcut to a application

Posted on 1998-11-12
6
313 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

14 Experts available now in Live!

Get 1:1 Help Now