【问题标题】:Do Windows shortcuts support very long argument lengths?Windows 快捷方式是否支持很长的参数长度?
【发布时间】:2011-02-11 14:22:17
【问题描述】:

我正在尝试创建一个包含长参数字符串 (> MAX_PATH) 的快捷方式(在桌面上)。

MSDN documentation 明确指出,对于 Unicode 字符串,该字符串可以比 MAX_PATH 长。

生成的快捷方式恰好在 MAX_PATH 字符之后被剪切(即Path + Arguments)。

我的实现有问题还是这是 Windows 的限制?

procedure CreateShortcut(APath: WideString;
  AWorkingDirectory: WideString; AArguments: WideString; ADescription: WideString;
  ALinkFileName: WideString);
var
   IObject : IUnknown;
   ISLink  : IShellLinkW;
   IPFile  : IPersistFile;
begin
   IObject := CreateComObject(CLSID_ShellLink);
   ISLink := IObject as IShellLinkW;
   ISLink.SetPath(            PWideChar(APath));
   ISLink.SetWorkingDirectory(PWideChar(AWorkingDirectory));
   ISLink.SetArguments(       PWideChar(AArguments));
   ISLink.SetDescription(     PWideChar(ADescription));
   IPFile := IObject as IPersistFile;
   IPFile.Save(PWideChar(ALinkFileName), False);
end;

PS:操作系统是 Windows XP(及以上)。

【问题讨论】:

  • 您是否尝试过使用 \\?\ 前缀来启用长文件名?例如,\\?\D:\very\long\path - 参见msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx
  • @David:是的,这会导致 Explorer 挂起几秒钟,但限制仍然存在。
  • 是什么被截断了参数、路径或工作目录?我感觉你需要使用 SetIDList 而不是 SetPath。
  • @David:当你组合路径和参数时得到的字符串(在资源管理器对话框中被称为“目标”)被削减到 260 个字符。
  • 我认为这是 shell 对话框中的一个限制,而不是支持它的 shell 链接中的底层内容。执行快捷方式时会发生什么?它的行为是否正确?

标签: windows delphi delphi-2007 shortcut maxlength


【解决方案1】:

事实证明,这个问题实际上只是 Explorer shell 对话框中的一个限制。生成的快捷方式文件没有 260 个字符的限制。只是对话框拒绝显示具有更多字符的目标。大概它用固定长度的缓冲区调用GetPath

procedure TForm11.Button1Click(Sender: TObject);
var
  sl: IShellLinkW;
  pf: IPersistFile;
begin
  CoCreateInstance(CLSID_ShellLink, nil, 
    CLSCTX_INPROC_SERVER, IID_IShellLinkW, sl);
  sl.SetPath('c:\desktop\test.bat');
  sl.SetWorkingDirectory('c:\desktop\');
  sl.SetArguments(PChar(StringOfChar('x', 300)+'_the_end'));
  pf := sl as IPersistFile;
  pf.Save('c:\desktop\test.lnk', False);
end;

我的test.bat 看起来像这样:

echo %1> test.out

生成的test.out 正确地到达_the_end!

【讨论】:

  • 我可以在 W7 上用 D2007 确认一个 311 字节的“test.out”。
【解决方案2】:

感谢所有为这个帖子做出贡献的人 - 它对我帮助很大。

但是,如果可以的话,我想添加以下我在制定解决方案时发现的信息:

  1. 在 Windows 7 Enterprise ~SP1 上,使用VBS to create the shortcut 似乎仍然存在(至少)参数字段中的最大字符数限制。在被截断之前,我最多测试了 1023 个字符。我认为同样的限制也适用于 Delphi 方法。

  2. 在 Windows XP Professional ~SP3 上,虽然 VBS 方法会创建一个超过 260 个字符的快捷方式(lnk 文件包含数据),但在执行它时似乎将其截断到这个数字。

【讨论】:

    猜你喜欢
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2019-03-21
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    相关资源
    最近更新 更多