Solved

How to create shortcuts

Posted on 1998-05-13
5
247 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks very much.  It work great!

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

Expert Comment

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

JB
0
 

Expert Comment

by:Delphiwizard
Comment Utility
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

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

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

9 Experts available now in Live!

Get 1:1 Help Now