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
A simple console application I wrote takes parameters via command line and I want to know what is the maximum allowable amount.
-Microsoft support KB article 830473
As @Sugrue I'm also digging out an old thread.
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.
No matter how you launch program with command line arguments it goes to ShellExecute, CreateProcess or 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_ATTRIBUTES structure as a parameter, to create that structure InitializeObjectAttributes is used. In this place we see
UNICODE_STRING
. So now lets take a look into this structure: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
(becauseWCHAR
is 2 bytes long).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 thatcmd.exe
is not the only way to pass arguments for new process.Sorry for digging out an old thread, but I think sunetos' answer isn'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.