Does anyone know what the maximum line length is for the system() and wsystem() functions?
Quote from: CommonTater on October 10, 2012, 11:27:45 PM
Does anyone know what the maximum line length is for the system() and wsystem() functions?
As this should land on the underlying OS command line, I would assume that the limits are either 2047 bytes (Windows NT 4.0/2000) or 8191 bytes (Windows XP and newer), as is also stated by this M$ knowledge base article (http://support.microsoft.com/kb/830473)...
Ralf
Quote from: Bitbeisser on October 11, 2012, 05:15:47 AM
Quote from: CommonTater on October 10, 2012, 11:27:45 PM
Does anyone know what the maximum line length is for the system() and wsystem() functions?
As this should land on the underlying OS command line, I would assume that the limits are either 2047 bytes (Windows NT 4.0/2000) or 8191 bytes (Windows XP and newer), as is also stated by this M$ knowledge base article (http://support.microsoft.com/kb/830473)...
Ralf
Hi Ralf ... I would assume the same, but I thought I should make sure before I write a ton of complicated software just to have it crash and burn on the one function. The help file doesn't say anything about line length...
I know from today's tests that it's at least 256 characters (MAX_PATH) ...
Quote from: CommonTater on October 11, 2012, 05:25:10 AM
Quote from: Bitbeisser on October 11, 2012, 05:15:47 AM
Quote from: CommonTater on October 10, 2012, 11:27:45 PM
Does anyone know what the maximum line length is for the system() and wsystem() functions?
As this should land on the underlying OS command line, I would assume that the limits are either 2047 bytes (Windows NT 4.0/2000) or 8191 bytes (Windows XP and newer), as is also stated by this M$ knowledge base article (http://support.microsoft.com/kb/830473)...
Ralf
Hi Ralf ... I would assume the same, but I thought I should make sure before I write a ton of complicated software just to have it crash and burn on the one function. The help file doesn't say anything about line length...
I know from today's tests that it's at least 256 characters (MAX_PATH) ...
I don't think I ever had in the 36 years that I am now programming a need for such (as in maximum of command line length) a large command line string, on any OS... ;-)
Well, I am certain that I have used a few years back (on Windows XP) strings in excess of 256 bytes though.
MAX_PATH doesn't really apply here, as the system() command is feeding the command interpreter (cmd.exe in case of Windows XP and newer) and just a simply copy instructions could have in excess of 2xMAX_PATH characters (source and target file/pathname)... ;)
Ralf
Quote from: Bitbeisser on October 11, 2012, 09:12:59 AM
I don't think I ever had in the 36 years that I am now programming a need for such (as in maximum of command line length) a large command line string, on any OS... ;-)
I've had a couple of pretty long ones over the years. As you point out a simple copy command can hit MAX_PATH * 2.
What I was wondering was if there was an artificial limit imposed by C in general or Pelles C in particular. I guess I will have to kludge up a bit of code to torture it and see when it breaks...
What cmd.exe tells to you ?
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
#define MAXLEN 8191
char cmd[MAXLEN+1];
strcpy(cmd, "echo ");
memset(&cmd[5], 'x', MAXLEN);
cmd[MAXLEN] = 0;
system(cmd);
return 0;
}
In Win7 8160 was the limit.
Quote from: timovjl on October 11, 2012, 02:54:08 PM
What cmd.exe tells to you ?
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
#define MAXLEN 8191
char cmd[MAXLEN+1];
strcpy(cmd, "echo ");
memset(&cmd[5], 'x', MAXLEN);
cmd[MAXLEN] = 0;
system(cmd);
return 0;
}
In Win7 8160 was the limit.
Thanks Timo ... I just tried a similar test, using ever growing strings... and got 8158 before the shell errored off.
I'm guessing the result is the limit of the CMD.EXE command line, including the executable name...
c:\windows\system32\cmd.exe /c == 31 characters
8191 - 31 = 8160
I wasn't expecting that...
Further testing ...
This expands it to 32k when running executables but is still limited to 8k when running CMD.EXE /c
WCHAR gWorkDir[MAX_PATH + 1];
// execute a program and commandline then return the errorlevel
DWORD Shell(PWCHAR Program, PWCHAR CommandLine)
{
PROCESS_INFORMATION pi = {0}; // handles from create process
STARTUPINFO si = {0}; // for create process
DWORD err;
WCHAR cmd[MAX_PATH];
// precondition startup info
si.cb = sizeof(si);
// find the executable
if (! SearchPath(NULL,Program,NULL,MAX_PATH,cmd,NULL))
return -1;
// run the command
if (! CreateProcess(cmd, CommandLine, NULL, NULL, 0, CREATE_UNICODE_ENVIRONMENT,
NULL, gWorkDir, &si, &pi))
return -1;
// wait for completion
WaitForSingleObject(pi.hthread, INFINITE);
GetExitCodeThread(pi.hThread, &err);
// cleanup
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return err;
}
(EDIT : included a more real world version of the function
added WaitForSingleObject() )
A quick update... working more with this, the limits of the function in my previous message are 8160 characters when using "cmd /c ...." and ~32700 when running an executable.