?
Solved

How to create shortcuts

Posted on 1998-05-13
5
Medium Priority
?
256 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
[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
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

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

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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
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…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

770 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