Solved

How to create shortcuts

Posted on 1998-05-13
5
249 Views
Last Modified: 2010-04-06
How do I create a shortcut on the Win95 or NT desktop with Delphi3 and a menu item in the Start menu with Delphi coding, because I am busy writing a customized installation program for one of my systems.
0
Comment
Question by:michaue
  • 3
5 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 0 total points
ID: 1345165
Here are functions to get the Start Menu folder and the desktop folder:

function GetSpecialPath(nFolder: Integer): string;
// nFolder is one of the CSIDL_... constants in ShlObj.
var
      pidlDTop: PItemIDList;
      SpecialPath: array[0..MAX_PATH - 1] of Byte;
      pMalloc: IMalloc;
begin
      SHGetMalloc(pMalloc);
      SHGetSpecialFolderLocation(0, nFolder, pidlDTop);
      SHGetPathFromIDList(pidlDTop, @SpecialPath);
      Result := BackSlash(string(PChar(@SpecialPath)));
      pMalloc.Free(pidlDTop);
      pMalloc._Release;
end;

E.g. StartMenuPath := GetSpecialPath(CSIDL_COMMON_STARTMENU);


Here is a function to create a shortcut:
(You may need to include 1/more of the following in your uses clause.  ComObj, ShlObj, ActiveX.  I forget which are specificially necessary.)

const
      IID_IPersistFile: TGUID = (D1:$0000010B;D2:$0000;D3:$0000;D4:($C0,$00,$00,$00,$00,$00,$00,$46));


function CreateShortcut(LinkDescrip, LinkPath, LinkIcoFile, TargetFile, TargetArgs, TargetWorkDir: string; LinkIcoIx: Integer; LinkHotKey: Word): Boolean;
// Every param can be blank or zero except TargetFile.
var
      DuplCount: Integer;
      hRes: HRESULT;
      NewLinkDescrip: string;
      pShellLink: IShellLink;
      pPersistFile: IPersistFile;
      wPath: array[0..MAX_PATH - 1] of WideChar;
begin
      Result := False;
      hRes := CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IID_IShellLinkA, pShellLink);
      if (hRes = S_OK) then
            begin
                  // If shortcut path is blank, make it the Desktop Path.
                  if (LinkPath = '') then
                        LinkPath := GetSpecialPath(CSIDL_DESKTOP)
                  else
                        LinkPath := BackSlash(LinkPath);

                  // If shortcut descrip is blank, create one.
                  if (LinkDescrip = '') then
                        LinkDescrip := 'Shortcut to ' + ExtractFilename(TargetFile);

                  // If target path is blank, start in target file's path.
                  if (TargetWorkDir = '') then
                        TargetWorkDir := ExtractFilePath(TargetFile);

                  // Get unique shortcut file name.
                  NewLinkDescrip := LinkDescrip;
                  DuplCount := 1;
                  while (FileExists(LinkPath + NewLinkDescrip + '.lnk')) do
                        begin
                              Inc(DuplCount);
                              NewLinkDescrip := LinkDescrip + ' (' + IntToStr(DuplCount) + ')';
                        end;
                  LinkDescrip := NewLinkDescrip;

                  // Set shortcut icon & hotkey.
                  if (LinkIcoFile = '') then
                        LinkIcoFile := TargetFile;
                  pShellLink.SetIconLocation(PChar(LinkIcoFile), LinkIcoIx);
                  pShellLink.SetHotkey(LinkHotKey);

                  // Set shortcut target info.
                  pShellLink.SetPath(PChar(TargetFile));
                  pShellLink.SetArguments(PChar(TargetArgs));
                  if (TargetWorkDir = '') then
                        TargetWorkDir := ExtractFilePath(TargetWorkDir);
                  pShellLink.SetWorkingDirectory(PChar(TargetWorkDir));

                  // Save the shortcut.
                  hRes := pShellLink.QueryInterface(IID_IPersistFile, pPersistFile);
                  if (hRes = S_OK) then
                        begin
                              MultiByteToWideChar(CP_ACP, 0, PChar(LinkPath + LinkDescrip + '.lnk'), -1, wPath, MAX_PATH);
                              hres := pPersistFile.Save(wPath, True);
                              Result := (hRes = S_OK);
                        end;
            end;
end;


The rest I leave up to you.  ;-)

Cheers,
JB
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345166
Mmm...  Sorry about the loss of indentation in the code.  It was fine when I posted it...

JB
0
 
LVL 1

Author Comment

by:michaue
ID: 1345167
Thanks very much.  It work great!

I will definately use this fascility again ...
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345169
Not bad, but does it work if the user installs to a different directory?

JB
0
 

Expert Comment

by:Delphiwizard
ID: 14681307
Hi,

This function look very good. So I tried to get it working.
But... "BackSlash" isn't recognized by Delphi, so I guess it's an function created by someone (maybe JimBob).

    if (LinkPath = '') then
      LinkPath := GetSpecialPath(CSIDL_DESKTOP)
    else
      LinkPath := BackSlash(LinkPath);

Can anybody give me the code for BackSlash?

Thanks a lot,
Stef
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

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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

910 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

17 Experts available now in Live!

Get 1:1 Help Now