JclShell.ShellLinkResolve获取错误的数据

时间:2014-03-12 16:33:12

标签: delphi winapi windows-shell

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位程序文件路径,并且存在DOES!

那么为什么ShellLinkResolve会在这里回复错误的数据呢?

编辑:我在Notepad ++中打开了链接:我发现其中只有ABSOLUTE 64位程序文件路径(没有环境变量),请参见:{ {3}}

EDIT2

你是对的,如果编译为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;

以下是视觉效果:
goo.gl/jWUDb9
http://goo.gl/MttrZA

EDIT3 :以下是编码的链接文件Xara Designer Pro X9.lnk,编码为Soap.EncdDecd.EncodeBase64):

TAAAAAEUAgAAAAAAwAAAAAAAAEabAAAAICAAAHCmGD2HDc8Ba9NGcKwWzwFwphg9hw3PASiAxAEA AAAAAQAAAAAAAAAAAAAAAAAAANcBFAAfUOBP0CDqOmkQotgIACswMJ0ZAC9DOlwAAAAAAAAAAAAA AAAAAAAAAAAAiAAxAAAAAAA1RFVrESBQUk9HUkF + MQAAcAAIAAQA777uOoUaNURVayoAAAA8AAAA AAABAAAAAAAAAAAARgAAAAAAUAByAG8AZwByAGEAbQAgAEYAaQBsAGUAcwAAAEAAcwBoAGUAbABs ADMAMgAuAGQAbABsACwALQAyADEANwA4ADEAAAAYAEoAMQAAAAAANURVaxAgWGFyYQAANgAIAAQA 7741RFVrNURVayoAAABpfRUAAAAFAAAAAAAAAAAAAAAAAAAAWABhAHIAYQAAABQAbgAxAAAAAAA1 RFlrECBYQVJBREV + MQAAVgAIAAQA7741RFVrNURZayoAAABqfRUAAAADAAAAAAAAAAAAAAAAAAAA WABhAHIAYQAgAEQAZQBzAGkAZwBuAGUAcgAgAFAAcgBvACAAWAA5AAAAGABoADIAKIDEASlE +抹布 IERFU0lHTn4xLkVYRQAATAAIAAQA774pRPqwNURZayoAAACEgRUAAAADAAAAAAAAAAAAAAAAAAAA RABlAHMAaQBnAG4AZQByAFAAcgBvAC4AZQB4AGUAAAAcAAAAcwAAABwAAAABAAAAHAAAADcAAAAA AAAAcgAAABsAAAADAAAAdG6zfBAAAABXaW43U1lTVEVNAEM6XFByb2dyYW0gRmlsZXNcWGFyYVxY YXJhIERlc2lnbmVyIFBybyBYOVxEZXNpZ25lclByby5leGUAAEwALgAuAFwALgAuAFwALgAuAFwA LgAuAFwALgAuAFwALgAuAFwALgAuAFwAUAByAG8AZwByAGEAbQAgAEYAaQBsAGUAcwBcAFgAYQBy AGEAXABYAGEAcgBhACAARABlAHMAaQBnAG4AZQByACAAUAByAG8AIABYADkAXABEAGUAcwBpAGcA bgBlAHIAUAByAG8ALgBlAHgAZQArAEMAOgBcAFAAcgBvAGcAcgBhAG0AIABGAGkAbABlAHMAXABY AGEAcgBhAFwAWABhAHIAYQAgAEQAZQBzAGkAZwBuAGUAcgAgAFAAcgBvACAAWAA5AFwAEAAAAAUA AKAmAAAAtQAAABwAAAALAACgtmNekL / BTkmynGW3MtPSGrUAAABNAAAACQAAoEEAAAAxU1BT4opY RrxMOEO7 / BOTJphtziUAAAAEAAAAAB8AAAAJAAAAUwAtADEALQA1AC0AMQA4AAAAAAAAAAAAAAAA AGAAAAADAACgWAAAAAAAAABoYXVwdC1wYwAAAAAAAAAAEIn2YAe8i0eH0eCntqcHxhEBFJmGguMR s54cb2UwHBYQifZgB7yLR4fR4Ke2pwfGEQEUmYaC4xGznhxvZTAcFgAAAAA =

1 个答案:

答案 0 :(得分:4)

也许这有帮助:通过解析内部结构从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;

自行承担风险!