Windows 快捷方式是否支持很长的参数长度?

发布于 2024-10-17 03:37:28 字数 1020 浏览 2 评论 0原文

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

MSDN 文档明确指出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(及以上)。

I am trying to create a shortcut (on the Desktop) that contains a long argument string (> MAX_PATH).

The MSDN documentation clearly states that for Unicode string the string can be longer than MAX_PATH.

The resulting shortcut is cut exactly after MAX_PATH characters (that is the Path + the Arguments).

Is there something wrong with my implementation or is this some Windows limitation?

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: OS is Windows XP (and above).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-10-24 03:37:28

事实证明,这个问题实际上只是 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!

It turns out that this issue is in fact solely a limitation in the Explorer shell dialog. The generated shortcut file does not have a 260 character limitation. It's simply that the dialog refuse to display a Target with more characters than that. Presumably it calls GetPath with a fixed length buffer.

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;

My test.bat looks like this:

echo %1> test.out

The resulting test.out goes right the way to _the_end!

愁杀 2024-10-24 03:37:28

感谢所有为该主题做出贡献的人 - 它对我帮助很大。

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

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

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

Thanks all who contributed to this thread - it helped me immensely.

However, if I may, I would like to add the below information I discovered in crafting my solution:

  1. On Windows 7 Enterprise ~SP1, it would seem that using VBS to create the shortcut there is still a limit on maximum characters in (at least) the arguments field. I tested up to 1023 chars before it got trunicated. I presume the same limit would apply to the Delphi method likewise.

  2. On Windows XP Professional ~SP3, while the VBS method will create a shortcut longer than 260 characters (lnk file contains the data), it seems to trunicate it at about this number when executing it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文