Solved

How to create shortcuts

Posted on 1998-05-13
5
250 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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