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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4970075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:09:33  来源:igfitidea点击:

Do Windows shortcuts support very long argument lengths?

windowsdelphidelphi-2007shortcutmaxlength

提问by Jens Mühlenhoff

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

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

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

MSDN文档中明确指出,对Unicode字符串的字符串可以长于MAX_PATH。

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

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

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

我的实现有什么问题还是这是一些 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: OS is Windows XP (and above).

PS:操作系统是 Windows XP(及更高版本)。

回答by David Heffernan

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 GetPathwith a fixed length buffer.

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

My test.batlooks like this:

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

echo %1> test.out

The resulting test.outgoes right the way to _the_end!

结果test.out正好通往_the_end!

回答by user66001

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 shortcutthere 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.

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

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.

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