How to get around the command line length limit?

2019-01-26 04:07发布

I've been working on a small and simple program that I drop files onto and, based on certian rules, they are moved to diffrent places.

The program works fine unless I drop more than a few files, then it kicks back an error (that appears to be more Windows than anything) that the start up command "c:\myapp.exe \file \file \file" is too long.

I realize I could set up a background proccess, but I really would prefer that this program not run in the backround (where it would be idle most of the time).

Is there a way around this limitation?

9条回答
疯言疯语
2楼-- · 2019-01-26 04:25

From this blog:

  • The maximum command line length for the CreateProcess function is 32767 characters. This limitation comes from the UNICODE_STRING structure.
  • If you are using the CMD.EXE command processor, then you are also subject to the 8192 character command line length limit imposed by CMD.EXE.
  • If you are using the ShellExecute/Ex function, then you become subject to the INTERNET_MAX_URL_LENGTH (around 2048) command line length limit imposed by the ShellExecute/Ex functions.
  • The maximum size of your environment is 32767 characters. The size of the environment includes all the variable names plus all the values.

So you're gonna have to settle with some of the mentioned workarounds (also, there's another workaround on the msdn blog I linked).

查看更多
神经病院院长
3楼-- · 2019-01-26 04:25

Unix commands frequently have one or two parameters that can be of unbounded length. Several of these commands have added parameters which can source those arguments from a file, or from standard input. So you have one command which gins up the list of arguments, and either pipes them to a temporary file, or pipes them to stdout.

See also, xargs, which can take a list of arguments and either invoke your command with all of the parameters, or in batches.

查看更多
Bombasti
4楼-- · 2019-01-26 04:27

I'd modify the application to pick up the files from a specific location (e.g. specific folder on a filesystem) rather than specifying each file on the command line.

UPDATE:

If the requirement is to be able to drag an item onto an .exe via Windows Explorer to start the application as Mark mentioned then you could put all of your files into one folder and drop the entire folder on the .exe.

查看更多
SAY GOODBYE
5楼-- · 2019-01-26 04:29

When the files are drag&dropped onto your application write the list of files names out to a text file, then give the location of this file to your program. The target program can then read in this file and process it line-by-line. This way you only ever pass a single file name.

查看更多
Fickle 薄情
6楼-- · 2019-01-26 04:39

I think the drag and drop handler is possibly one way to go, but it seems quite heavy.

An alternative solution is to use a Explorer Context Menu handler. With this in place, you would select all the files, but rather than dragging them, right click, and choose your new menu item "Send to ".

When the menu item is selected, it passes the list of commands to your program. There are a couple of ways of doing this:

  1. launch your program, and feed the list of files to standard input
  2. write the list of files to a temporary file, and launch your program with just one command argument - the temporary file listing the files to process. List files are usually prefixed with '@' on the command line to distinguish them from ordinary file names.
查看更多
来,给爷笑一个
7楼-- · 2019-01-26 04:40

I experienced a similar issue when I was trying to get security details on a long path. My solution was to map drives whenever the path length got too long. Check out my solution on How do I get the security details for a long path?

查看更多
登录 后发表回答