Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
676 views
in Technique[技术] by (71.8m points)

windows - Check for existence of a shortcut pointing to a specific target in Inno Setup

In my Inno Setup installer I need to make sure a shortcut to a certain file is present in a folder. The name of the shortcut is arbitrary and not under my control. I only know which file it needs to point to. If the shortcut is missing, I need to generate the shortcut. If it is already present, it must not be created again.

I guess that it is somehow possible to iterate through all shortcut files in the relevant folder and check which file they point to. In a comment to an answer to Shared Shortcuts/Icons, a IShellLink interface is mentioned, but I don’t know how to make it available in the Code section. (Uses ShlObj; is not recognized)

Does anybody have a suggestion how I could solve this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on

Requires Unicode version of Inno Setup (the only version as of Inno Setup 6).

const
  MAX_PATH = 260;
  STGM_READ = $00000000;
  SLGP_SHORTPATH = $1; 
  SLGP_RAWPATH = $4; 
  SLGP_RELATIVEPRIORITY = $8;
  CLSID_ShellLink = '{00021401-0000-0000-C000-000000000046}';

type
  TWin32FindDataW = record
    dwFileAttributes: DWORD;
    ftCreationTime: TFileTime;
    ftLastAccessTime: TFileTime;
    ftLastWriteTime: TFileTime;
    nFileSizeHigh: DWORD;
    nFileSizeLow: DWORD;
    dwReserved0: DWORD;
    dwReserved1: DWORD;
    cFileName: array[0..MAX_PATH-1] of Char;
    cAlternateFileName: array[0..13] of Char;
  end;

  IShellLinkW = interface(IUnknown)
    '{000214F9-0000-0000-C000-000000000046}'
    function GetPath(pszFile: string; cchMaxPath: Integer;
      var FindData: TWin32FindDataW; fFlags: DWORD): HRESULT;
    procedure Dummy2;
    procedure Dummy3;
    function GetDescription(pszName: string; cchMaxName: Integer): HRESULT;
    function SetDescription(pszName: string): HRESULT;
    function GetWorkingDirectory(pszDir: string; cchMaxPath: Integer): HRESULT;
    function SetWorkingDirectory(pszDir: string): HRESULT;
    function GetArguments(pszArgs: string; cchMaxPath: Integer): HRESULT;
    function SetArguments(pszArgs: string): HRESULT;
    function GetHotkey(var pwHotkey: Word): HRESULT;
    function SetHotkey(wHotkey: Word): HRESULT;
    function GetShowCmd(out piShowCmd: Integer): HRESULT;
    function SetShowCmd(iShowCmd: Integer): HRESULT;
    function GetIconLocation(pszIconPath: string; cchIconPath: Integer;
      out piIcon: Integer): HRESULT;
    function SetIconLocation(pszIconPath: string; iIcon: Integer): HRESULT;
    function SetRelativePath(pszPathRel: string; dwReserved: DWORD): HRESULT;
    function Resolve(Wnd: HWND; fFlags: DWORD): HRESULT;
    function SetPath(pszFile: string): HRESULT;
  end;

  IPersist = interface(IUnknown)
    '{0000010C-0000-0000-C000-000000000046}'
    function GetClassID(var classID: TGUID): HRESULT;
  end;

  IPersistFile = interface(IPersist)
    '{0000010B-0000-0000-C000-000000000046}'
    function IsDirty: HRESULT;
    function Load(pszFileName: string; dwMode: Longint): HRESULT;
    function Save(pszFileName: string; fRemember: BOOL): HRESULT;
    function SaveCompleted(pszFileName: string): HRESULT;
    function GetCurFile(out pszFileName: string): HRESULT;
  end;

function GetLinkFileTarget(const FileName: string): string;
var
  FindData: TWin32FindDataW;
  ComObject: IUnknown;
  ShellLink: IShellLinkW;
  PersistFile: IPersistFile;
begin
  ComObject := CreateComObject(StringToGuid(CLSID_ShellLink));
  PersistFile := IPersistFile(ComObject);
  OleCheck(PersistFile.Load(FileName, STGM_READ));
  ShellLink := IShellLinkW(ComObject);
  SetLength(Result, MAX_PATH);
  OleCheck(ShellLink.GetPath(Result, MAX_PATH, FindData, SLGP_RAWPATH));
  SetLength(Result, Pos(#0, Result) - 1);
end;

procedure IterateShortcuts(Path: string);
var
  FindRec: TFindRec;
  ShortcutPath: string;
  TargetPath: string;
begin
  Path := AddBackslash(Path);

  Log(Format('Looking for .lnk in [%s]', [Path]));

  if FindFirst(Path + '*.lnk', FindRec) then
  begin
    try
      repeat
        ShortcutPath := Path + FindRec.Name;
        TargetPath := GetLinkFileTarget(ShortcutPath);
        Log(Format('Target of shortcut [%s] is [%s]', [ShortcutPath, TargetPath]));
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end;
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...