Muthukumar Palaniappan
Muthukumar Palaniappan

Reputation: 1670

commandline argument parameter limitation

Language: C# I have to pass a huge string array (built dynamically) as an argument to run an exe. I am thinking of acheiving it by the below 2 ways. But I am not feeling confident.

  1. I can create it as one string delimited by spaces. I can invoke the exe through Process.Start. Hence the running child process considers the space and holds as a string array. However I am unsure about the string array limitation. Suppose if my string array count exceeds more than 10,000

  2. I can create it as one string delimited by a special symbol, which never fall in data. I can invoke the exe with the string. The running child process considers it as one single string, where i can split it with the same delimiter to get the string array back. However, here i am unsure about the command size. Will that do, if the command line string length is large

Can anyone help me in letting me know the parameter size limitations

Upvotes: 11

Views: 21089

Answers (8)

Theodor Zoulias
Theodor Zoulias

Reputation: 43525

If starting the child process directly from the parent process is acceptable (UseShellExecute= false), then you could redirect the StandardInput of the child process and pass arbitrary size of data throw it. Here is an example passing an array of 100000 strings and other stuff, serializing them in binary format.

static void Main(string[] args)
{
    if (args.Length == 0)
    {
        var exeFilePath = Assembly.GetExecutingAssembly().Location;
        var psi = new ProcessStartInfo(exeFilePath, "CHILD");
        psi.UseShellExecute = false;
        psi.RedirectStandardInput = true;
        Console.WriteLine("Parent - Starting child process");
        var childProcess = Process.Start(psi);
        var bf = new BinaryFormatter();
        object[] data = Enumerable.Range(1, 100000)
            .Select(i => (object)$"String-{i}")
            .Append(13)
            .Append(DateTime.Now)
            .Append(new DataTable("Customers"))
            .ToArray();
        Console.WriteLine("Parent - Sending data");
        bf.Serialize(childProcess.StandardInput.BaseStream, data);
        Console.WriteLine("Parent - WaitForExit");
        childProcess.WaitForExit();
        Console.WriteLine("Parent - Closing");
    }
    else
    {
        Console.WriteLine("Child - Started");
        var bf = new BinaryFormatter();
        Console.WriteLine("Child - Reading data");
        var data = (object[])bf.Deserialize(Console.OpenStandardInput());
        Console.WriteLine($"Child - Data.Length: {data.Length}");
        Console.WriteLine("Child - Closing");
    }
}

Output:

Parent - Starting child process
Child - Started
Child - Reading data
Parent - Sending data
Parent - WaitForExit
Child - Data.Length: 100003
Child - Closing
Parent - Closing

This example executes in 6 sec in my machine.

Upvotes: 2

kim
kim

Reputation: 21

Upvotes: 2

Oded
Oded

Reputation: 499002

It depends on the OS:

See Command prompt (Cmd. exe) command-line string limitation on the Microsoft Support site.

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. On computers running Microsoft Windows 2000 or Windows NT 4.0, the maximum length of the string that you can use at the command prompt is 2047 characters.

(emphasis mine)

In regards to the size of a string array - if you have many millions of strings in a string array - you are mostly limited by the amount of memory available.

Upvotes: 20

Alex K.
Alex K.

Reputation: 175776

Although a bad idea, Process.start with useshellexecute=false would invoke createprocess() which allows for 32767 characters in the command line (although this is also the maximum size for the entire environment block)

Upvotes: 6

Alex Mendez
Alex Mendez

Reputation: 5150

You may want to consider creating a parameter file and passing the file as the parameter.

I found this:

For OS: maximum command line lenght is 32767 characters (this limit is from unicode string structure), command prompt maximum lenght is 8192 characters (this limit is from cmd.exe). You may also check:

http://support.microsoft.com/kb/830473

Hope this help.

Upvotes: 2

Anders Marzi Tornblad
Anders Marzi Tornblad

Reputation: 19305

It is not really good practice to use command line arguments for huge arrays. Put your arguments in a configuration file instead, and just pass the filename as a command line argument.

The OS limit varies with the Windows version. It could be about 2k or 8k:

http://support.microsoft.com/kb/830473

Upvotes: 2

Icarus
Icarus

Reputation: 63966

If you are passing 10,000 arguments to a program, you should be putting those arguments in a file and reading the file from disk.

Upvotes: 6

keyboardP
keyboardP

Reputation: 69372

You could store the arguments in a text file and pass that text file as the argument. Your application can then parse the text file to analyse the arguments.

Upvotes: 3

Related Questions