Calls to system() in Windows

Published on March 22, 2011

I recently ran into a stupid problem using the system() call in C++ on Windows platforms. For some strange reason, calls to system() get passed through the cmd /c command. This has some strange side effects if your paths contain spaces, and you try to use double quotes to allow those paths. From the cmd documentation:

If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (“) characters:

  1. If all of the following conditions are met, then quote characters on the command line are preserved:
    • no /S switch
    • exactly two quote characters
    • no special characters between the two quote characters, where special is one of: &<>()@^|
    • there are one or more whitespace characters between the two quote characters
    • the string between the two quote characters is the name of an executable file
  2. Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character.

As you can see from this documentation, if you have any special characters or spaces in your call to system(), you must wrap the entire command in an extra set of double quotes. Here’s a working example:

string myCommand = "\"\"C:\\Some Path\\Here.exe\" -various -parameters\"";
int retVal = system(myCommand.c_str());
if (retVal != 0)
{
    // Handle the error
}

Note that I’ve got a pair of quotes around the entire command, as well as a pair around the path with spaces. This requirement isn’t apparent at first glance, but it’s something to keep in mind if you ever find yourself in this situation.

2 Comments

kip

Do you ever read Raymond Chen’s blog, The Old New Thing? He’s a longtime Windows developer at Microsoft that has lots of insight into why things like this work the way they do. (He’s the guy who wrote the original TweakUI for Win95, as a tool for himself, that was eventually released to the public.)

Usually things like this are because Microsoft goes way out of their way to maintain backwards compatibility–a batch script written in 1981 for DOS 1.0 will probably still run in Windows 7. Sometimes they add new APIs that can be used as an alternative when upgrades to the old API would break backwards compatibility, but I haven’t done any Windows C++ programming in a while so I’m not sure if there is one for system().

Here’s an interesting post about an issue similar to the one you’re describing: What’s up with the strange treatment of quotation marks and backslashes by CommandLineToArgvW?

Jonah

That’s not a blog I’m familiar with, though I’ll add it to Google Reader. It looks interesting.

Part of me salutes Microsoft for backwards compatibility. Another part of me wishes they would just say “to hell with it” and drop all the legacy support. The amount of baggage that Windows programmers have to carry around simply increases over time. There’s something to be said for Apple’s “upgrade to our new standard, or go elsewhere” approach.

Comments are closed.

Copyright © 2004-2018 Jonah Bishop. Hosted by DreamHost.