Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to create shortcuts

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

610 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