Quantcast
Channel: Deadbeef.COM » win32
Viewing all articles
Browse latest Browse all 8

Tip: Quoting arguments to WIN32 (DOS?) programs

$
0
0

Here are the quoting rules I have discovered through trial and error. If
you follow these guidelines, MSVCRT.dll will decode the arguments properly
and pass the correct parameters to main(argc,argv).

I assume here that the result will be passed to CreateProcess.

CommandLine = all args (including exe name) joined by spaces.
for each arg
If the arg contains any of newline, tab, space or ” (double quote), then
quote it.
If the arg is a null string, then quote it.
If the arg contains double back slash \\, then quote it.

Quoting in this context means:
” before and after
if you find a \, then look at the next non-\ character. If it is a
” or the end of the string, then replace \ with \\
embedded ” becomes \”

So:

foo -> foo
foo bar -> “foo bar”
foo\bar -> foo\bar
foo\ bar -> “foo\ bar”
foo”bar -> “foo\”bar”
foo\\bar -> foo\\bar
fo o\\bar -> “fo o\\bar”
foo bar\ -> “foo bar\\”
fo\\o\\\”ba\\\r -> “fo\\o\\\\\\”ba\\\r”

See the documentation for CommandLineToArgvW for more infomation.

Then call CreateProcess(0, CommandLine, …..). I always call CreateProcess
since I don’t trust exec or spawn to properly quote the arguments.


Viewing all articles
Browse latest Browse all 8

Trending Articles