What processing/validation is performed on command

2019-06-27 01:43发布

问题:

I've a small WPF application that accepts file paths as command line arguments.

If a user drags in too many files with long paths, it will exceed the maximum command line length, at least on 32-bit WinXP.

The result is an error window showing:

Windows cannot access the specified device, path, or file. You may not have the appropriate permissions to access the item.

This appears similar to the error

The filename or extension is too long.

In these cases, it appears that the process never starts.

I thought that drag-and-dropping files effectively just passed their paths as strings, but these errors indicate otherwise, and that some part of the OS/shell/framework is doing some kind of validation based on the fact that these are file/directory paths, and when that fails, the process does not start.

Does anyone know what happens between the time that command line arguments are passed to a .NET .exe and when that .exe starts up, if ever?

回答1:

The answer is in your question: the path list exceeds the maximum command line size, so your program cannot start.

The operating system builds the command line before it starts your process, since that information is required at process creation time. Since the command line length exceeds the maximum size, the operating system can't build it and fails, probably with ERROR_FILENAME_EXCED_RANGE (sic), before even trying to create the process.

Therefore, your program never starts.