Board index » delphi » Desktop shortcut in D2?

Desktop shortcut in D2?

How do you create a shortcut to a particular file?
I've created a special folder under Start Menu\Programs but I'm unsure how
to make a shortcut.
Any help would be grateful thanks.

--
Smeagol
mel...@ihug.co.nz
ICQ: 32082362

 

Re:Desktop shortcut in D2?


I'm not sure if this will work in D2. It does work in D3 and D4.

Note: FileName in this routine is the name of the shortcut (with the
.lnk extension).

You also need to add the following units to your uses clause : ActiveX,
ShlObj, OLE2  (Delphi 4 - Delphi 3 may be different)

It is actually part of a larger unit I have created that can create and
edit shortcuts. If you have trouble getting this to work, email me, and
I'll send you the whole unit.

Chris.

{-- Code --}
var
   hRes              :  HRESULT;
   psl               :  IShellLink;
   ppf               :  IPersistFile;
   wsz               :  PWideChar;
begin
     Result := false;
     if FileName = '' then
        exit;
     CoInitialize(nil);
     try
        GetMem(wsz,MAX_PATH*2);
        try
           { Get a pointer to the IShellLink interface. }
           hres := CoCreateInstance(TCLSID(CLSID_ShellLink), nil,
                                   CLSCTX_INPROC_SERVER,
TIID(IID_IShellLinkA), psl);
           if SUCCEEDED(hres) then
              begin
                   { Set the path to the shortcut target, and add the
description.  }
                   psl.SetPath(PChar(FPath));
                   psl.SetDescription(PChar(FDesc));
                   psl.SetWorkingDirectory(PChar(FWorkDir));
                   { Arguments to be passed to the executable }
                   psl.SetArguments(PChar(FArgs));
                   { The name of the file containing the icon you
require }
                   psl.SetIconLocation(pChar(FIconLoc), FIconIdx);
                   { Short cut key - Use the ShortCut function to
generate }
                   psl.SetHotkey(ShortcutToHotKey(FHotKey));

                   { Query IShellLink for the IPersistFile interface for
saving the
                     shortcut in persistent storage. }
                   if
SUCCEEDED(psl.QueryInterface(System.TGUID(IID_IPersistFile), ppf)) then
                      begin
                           { Ensure that the string is ANSI. }
                           MultiByteToWideChar(CP_ACP, 0,
PChar(FileName),-1,wsz,MAX_PATH);
                           { Save the link by calling
IPersistFile::Save. }
                           hres := ppf.Save(wsz,TRUE);
                           ppf.Release;
                           Result := Succeeded(HRes);
                      end;
              end;
        finally
               FreeMem(wsz,MAX_PATH*2);
        end;
     finally
            CoUninitialize;
     end;
end;

Other Threads