windows 命令行字符串的最大长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3205027/
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-09 07:25:56  来源:igfitidea点击:

Maximum Length of Command Line String

windowscommand-line

提问by

In Windows, what is the maximum length of a command line string? Meaning if I specify a program which takes arguments on the command line such as abc.exe -name=abc

在 Windows 中,命令行字符串的最大长度是多少?这意味着如果我指定一个在命令行上接受参数的程序,例如abc.exe -name=abc

A simple console application I wrote takes parameters via command line and I want to know what is the maximum allowable amount.

我编写的一个简单的控制台应用程序通过命令行获取参数,我想知道允许的最大数量是多少。

采纳答案by sunetos

From the Microsoft documentation: Command prompt (Cmd. exe) command-line string limitation

来自 Microsoft 文档:命令提示符 (Cmd.exe) 命令行字符串限制

On computers running Microsoft Windows XP or later, the maximum length of the string that you can use at the command prompt is 8191 characters.

在运行 Microsoft Windows XP 或更高版本的计算机上,您可以在命令提示符下使用的字符串的最大长度为 8191 个字符。

回答by Sugrue

Sorry for digging out an old thread, but I think sunetos' answerisn't correct (or isn't the full answer). I've done some experiments (using ProcessStartInfo in c#) and it seems that the 'arguments' string for a commandline command is limited to 2048 characters in XP and 32768 characters in Win7. I'm not sure what the 8191 limit refers to, but I haven't found any evidence of it yet.

很抱歉挖出一个旧线程,但我认为sunetos 的答案不正确(或者不是完整的答案)。我做了一些实验(在 c# 中使用 ProcessStartInfo),似乎命令行命令的“参数”字符串在 XP 中限制为 2048 个字符,在 Win7 中限制为 32768 个字符。我不确定 8191 限制指的是什么,但我还没有找到任何证据。

回答by ST3

As @Sugrue I'm also digging out an old thread.

作为@Suglue,我也在挖掘一个旧线程。

To explain why there is 32768 (I think it should be 32767, but lets believe experimental testing result) characters limitation we need to dig into Windows API.

要解释为什么有 32768(我认为应该是 32767,但让我们相信实验测试结果)字符限制,我们需要深入研究 Windows API。

No matter how you launch program with command line arguments it goes to ShellExecute, CreateProcessor any extended their version. These APIs basically wrap other NT level API that are not officially documented. As far as I know these calls wrap NtCreateProcess, which requires OBJECT_ATTRIBUTESstructure as a parameter, to create that structure InitializeObjectAttributesis used. In this place we see UNICODE_STRING. So now lets take a look into this structure:

无论您如何使用命令行参数启动程序,它都会转到ShellExecuteCreateProcess或它们的任何扩展版本。这些 API 基本上封装了其他未正式记录的 NT 级别 API。据我所知,这些调用包装了NtCreateProcess,它需要OBJECT_ATTRIBUTES结构作为参数,以创建使用InitializeObjectAttributes 的结构。在这个地方我们看到了UNICODE_STRING。那么现在让我们来看看这个结构:

typedef struct _UNICODE_STRING {
    USHORT Length;
    USHORT MaximumLength;
    PWSTR  Buffer;
} UNICODE_STRING;

It uses USHORT(16-bit length [0; 65535]) variable to store length. And according this, length indicates size in bytes, not characters. So we have: 65535 / 2 = 32767(because WCHARis 2 bytes long).

它使用USHORT(16-bit length [0; 65535]) 变量来存储长度。并根据,长度表示以字节为单位,而不是以字符大小。所以我们有:(65535 / 2 = 32767因为WCHAR是 2 个字节长)。

There are a few steps to dig into this number, but I hope it is clear.

有几个步骤可以深入了解这个数字,但我希望它很清楚。



Also, to support @sunetos answer what is accepted. 8191 is a maximum number allowed to be entered into cmd.exe, if you exceed this limit, The input line is too long.error is generated. So, answer is correct despite the fact that cmd.exeis not the only way to pass arguments for new process.

另外,为了支持@sunetos 回答被接受的内容。8191 是允许输入的最大数量cmd.exe,如果超过此限制,The input line is too long.则会生成错误。因此,尽管这cmd.exe不是为新进程传递参数的唯一方法,但答案是正确的。

回答by Joseph Dewey

In Windows 10, it's still 8191 characters...at least on my machine.

在 Windows 10 中,它仍然是 8191 个字符……至少在我的机器上是这样。

It just cuts off any text after 8191 characters. Well, actually, I got 8196 characters, and after 8196, then it just won't let me type any more.

它只是在 8191 个字符之后切断任何文本。嗯,实际上,我得到了 8196 个字符,在 8196 个字符之后,它就不会让我再打字了。

Here's a script that will test how long of a statement you can use. Well, assuming you have gawk/awk installed.

这是一个脚本,用于测试您可以使用多长时间的语句。好吧,假设您安装了 gawk/awk。

echo rem this is a test of how long of a line that a .cmd script can generate >testbat.bat
gawk 'BEGIN {printf "echo -----";for (i=10;i^<=100000;i +=10) printf "%%06d----",i;print;print "pause";}' >>testbat.bat
testbat.bat