How to get the data from a ShellLink even when the Link Target does not exist anymore?

后端 未结 1 745
我在风中等你
我在风中等你 2021-01-28 22:33

In Delphi XE7, I wanted to use the following code to replace the link target of a shell link file (.lnk), even when the link target does not exist anymore:

uses
         


        
相关标签:
1条回答
  • 2021-01-28 22:34

    Why don`t you want to use very simple code like this:

    procedure ShellLinkReplaceLinkTarget(const AShellLinkFile, ANewTarget: UnicodeString);
    var
      ShellLink: IShellLinkW;
      PersistFile: IPersistFile;
    begin
      OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLinkW, ShellLink));
      try
        OleCheck(ShellLink.QueryInterface(IPersistFile, PersistFile));
        try
          OleCheck(PersistFile.Load(PWideChar(AShellLinkFile), STGM_READWRITE));
          OleCheck(ShellLink.SetIDList(nil));
          OleCheck(ShellLink.SetPath(PWideChar(ANewTarget)));
          OleCheck(PersistFile.Save(PWideChar(AShellLinkFile), True));
        finally
          PersistFile := nil;
        end;
      finally
        ShellLink := nil;
      end;
    end;
    

    And there is one very important detail. Some lnk files can contain different properties like background color (in Windows 8). And if you recreate lnk file properties will be lost.

    0 讨论(0)
提交回复
热议问题