JclShell.TShellLink.Target给出错误的路径:

uses
  JclShell;
...
var
  ThisShellLinkRecord: JclShell.TShellLink;
  ThisTargetExePath: string;
begin
  JclShell.ShellLinkResolve('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Xara\Xara Designer Pro X9\Xara Designer Pro X9.lnk', ThisShellLinkRecord);
  ThisTargetExePath := ThisShellLinkRecord.Target;


上面代码中的ThisTargetExePath结果为:
C:\Program Files (x86)\Xara\Xara Designer Pro X9\DesignerPro.exe
请注意结果目标路径中的(x86),它指示32位程序文件路径。

但是,这是错误的路径,不存在!从Windows开始菜单手动打开Xara Designer Pro开始菜单链接的“属性”对话框时,目标路径为:
C:\Program Files\Xara\Xara Designer Pro X9\DesignerPro.exe
请注意,这是64位程序文件路径,确实存在!

那么,为什么ShellLinkResolve在这里返回错误的数据?

编辑:我已经在记事本++中打开了链接:我只在其中找到了绝对的64位程序文件路径(没有环境变量),请参见此处:goo.gl/jWUDb9

编辑2:

没错,如果编译为32位程序或64位程序,会得到不同的结果:

procedure TForm1.btn1Click(Sender: TObject);
var
  ThisShellLinkRecord: JclShell.TShellLink;
begin
  JclShell.ShellLinkResolve(Edit1.Text, ThisShellLinkRecord);

  {$IFDEF WIN32}
    Form1.Caption := 'This is a 32-bit program';
    Label1.Caption := ThisShellLinkRecord.Target;
  {$ELSE}
    Form1.Caption := 'This is a 64-bit program';
    Label1.Caption := JclFileUtils.PathGetLongName(ThisShellLinkRecord.Target);
  {$ENDIF}
end;


这是视觉效果:
http://goo.gl/MttrZA
http://goo.gl/hvQqP6

编辑3:这是编码的链接文件(Xara Designer Pro X9.lnk,用Soap.EncdDecd.EncodeBase64编码):

TAAAAAEUAgAAAAAAwAAAAAAAAEabAAAAICAAAHCmGD2HDc8Ba9NGcKwWzwFwphg9hw3PASiAxAEA
AAAAAQAAAAAAAAAAAAAAAAAAANcBFAAfUOBP0CDqOmkQotgIACswMJ0ZAC9DOlwAAAAAAAAAAAAA
AAAAAAAAAAAAiAAxAAAAAAA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AACQAAoEEAAAAxU1BT4opY
RrxMOEO7 / BOTJphtziUAAAAEAAAAAB8AAAAJAAAAUwAtADEALQA1AC0AMQA4AAAAAAAAAAAAAAAA
AGAAAAADAACgWAAAAAAAAABoYXVwdC1wYwAAAAAAAAAAEIn2YAe8i0eH0eCntqcHxhEBFJmGguMR
s54cb2UwHBYQifZgB7yLR4fR4Ke2pwfGEQEUmYaC4xGznhxvZTAcFgAAAAA =

最佳答案

也许有帮助:通过解析内部结构直接从lnk文件读取目标路径:

type
  TdecShellLinkHeader = packed record
    HeaderSize: DWORD;
    LinkCLSID: TGUID;
    LinkFlags: DWORD;
    FileAttributes: DWORD;
    CreationTime: TFILETIME;
    AccessTime: TFILETIME;
    WriteTime: TFILETIME;
    FileSize: DWORD;
    IconIndex: Integer;
    ShowCommand: DWORD;
    HotKey: Word;
    Reserved1: Word;
    Reserved2: DWORD;
    Reserved3: DWORD;
  end;

const
  LnkFileCLSID: TGUID = '{00021401-0000-0000-C000-000000000046}';

function DirectReadTargetNameFromLnkFile(const AFileName: UnicodeString): UnicodeString;
var
  Stream: TStream;
  Header: TdecShellLinkHeader;
  Size: Word;
  IDList: PItemIDList;
  Desktop: IShellFolder;
  ShellFolder: IShellFolder;
  ChildItem: PItemIDList;
  StrRet: TStrRet;
  AnsiResult: AnsiString;
  Result2: UnicodeString;
begin
  Result := '';
  Stream := TFileStream.Create(AFileName, fmOpenRead);
  try
    Stream.ReadBuffer(Header, SizeOf(Header));
    if not (Header.HeaderSize = SizeOf(Header)) or
      not IsEqualCLSID(Header.LinkCLSID, LnkFileCLSID) then
        raise Exception.Create('Invalid Lnk file');

    if Header.LinkFlags and SLDF_HAS_ID_LIST <> 0 then
      begin
        Stream.ReadBuffer(Size, SizeOf(Size));
        IDList := CoTaskMemAlloc(Size);
        try
          Stream.ReadBuffer(IDList^, Size);
          OleCheck(SHGetDesktopFolder(Desktop));
          try
            OleCheck(SHBindToParent(IDList, IShellFolder, Pointer(ShellFolder), ChildItem));
            try
              OleCheck(ShellFolder.GetDisplayNameOf(ChildItem, SHGDN_FORPARSING, StrRet));
              case StrRet.uType of
                STRRET_WSTR:
                  begin
                    Result := StrRet.pOleStr;
                    CoTaskMemFree(StrRet.pOleStr);
                  end;
                STRRET_OFFSET:
                  begin
                    Inc(PByte(ChildItem), StrRet.uOffset);
                    Result := UnicodeString(PAnsiChar(ChildItem));
                  end;
                STRRET_CSTR:
                  Result := UnicodeString(AnsiString(StrRet.cStr));
              else Result := '';
              end;
              Exit;
            finally
              ShellFolder := nil;
            end;
          finally
            Desktop := nil;
          end;
        finally
          CoTaskMemFree(IDList);
        end;
      end;

    if Header.LinkFlags and SLDF_HAS_LINK_INFO <> 0 then
      begin
        Stream.ReadBuffer(Size, SizeOf(Size));
        Stream.Seek(Size - SizeOf(Size), soFromCurrent);
      end;

    if Header.LinkFlags and SLDF_HAS_NAME <> 0 then
      begin
        Stream.ReadBuffer(Size, SizeOf(Size));
        if Header.LinkFlags and SLDF_UNICODE <> 0 then
          Stream.Seek(Size * SizeOf(WideChar), soFromCurrent)
        else
          Stream.Seek(Size * SizeOf(AnsiChar), soFromCurrent);
      end;

    if Header.LinkFlags and SLDF_HAS_RELPATH <> 0 then
      begin
        Stream.ReadBuffer(Size, SizeOf(Size));
        if Header.LinkFlags and SLDF_UNICODE <> 0 then
          begin
            SetLength(Result, Size);
            if Size > 0 then
              Stream.ReadBuffer(PWideChar(Result)^, Size * SizeOf(WideChar));
          end
        else
          begin
            SetLength(AnsiResult, Size);
            if Size > 0 then
              Stream.ReadBuffer(PAnsiChar(AnsiResult)^, Size * SizeOf(AnsiChar));
            Result := AnsiResult;
          end;

        if Header.LinkFlags and SLDF_HAS_EXP_SZ <> 0 then
          begin
            Size := ExpandEnvironmentStringsW(PWideChar(Result), nil, 0);
            if Size > 1 then
              begin
                SetLength(Result2, Size - 1);
                ExpandEnvironmentStringsW(PWideChar(Result), PWideChar(Result2), Size);
                Result := Result2;
              end;
          end;

        Result2 := ExtractFileDir(AFileName);
        while Pos('..\', Result) = 1 do
          begin
            Result2 := ExtractFileDir(Result2);
            Delete(Result, 1, 3);
          end;
        Result := Result2 + '\' + Result;
      end;
  finally
    Stream.Free;
  end;
end;


自行承担风险!

关于delphi - JclShell.ShellLinkResolve获取错误的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22357992/

10-12 02:04