I am writing a small C++ program, which involves starting a .exe process with many arguments, including sending paths which contain spaces, and hence require double quotes around the path. I have remembered to use the \” escape character.
I have tried lots of different ways of starting this process, and so have ended up at system(), simply to try to make the starting code as minimalist as possible.
I have this code:
string ExpandEnvironmentVariables(string input)
{
char buffer[512];
ExpandEnvironmentStringsA(input.c_str(), buffer, sizeof(buffer) / sizeof(*buffer));
return buffer;
}
which works fine as far as I can tell.
Here is the code I am using to start the process (all the variables are std::string). This one works fine, however, it doesn’t contain all the arguments I want to pass.
system(ExpandEnvironmentVariables("\"" + path + "\" " + arg1 + " -c ").c_str());
This one doesn’t work:
system(ExpandEnvironmentVariables("\"" + path + "\" " + arg1 + " -c \"").c_str());
Notice the addition of the extra double quotes. It is not that the process errors, it is that the process never even starts up.
I am using Process Monitor to detect when the process is starting up, and no process.exe appears in it as soon as I add that extra double quotes into it. However, here is what I want to start the process with (which doesn’t work either)
system(ExpandEnvironmentVariables("\"" + path + "\" " + arg1 + " -c \"" + arg2 + "\"" + arg3 + arg4 + " " + arg5 + arg6 + " " + arg6 + "\\" + arg7).c_str());
I even tried to pair up the double quotes, but this doesn’t work either:
system(ExpandEnvironmentVariables("\"" + path + "\" " + arg1 + " -c \"test\"").c_str());
Finally, I was concerned about length. The addition of the third double quotes is what breaks it. However, before that, the length is 134 characters long. However, I want the actual starting routine to be 346 characters long. This isn’t too long, is it???
I can debug the program, take the formatted string, and paste into Command Prompt, and the process starts perfectly. With those extra quotes, it is formatted perfectly. But I can’t get it to start through C++!
Does anyone know what is going on here? It is driving me absolutely mad! Thank you so much for any help or advice you can offer.
EDIT:
I will try and suggest you two workarounds:
instead of using
"around your arguments, you could replace each space character by\"; to do the replacement correctly, you will have to specify\\"as a replacement string; try it at first without specifying any argument (only for the command name), and see if you can extend it;write the entire command line to a batch file (it seems you are on Windows), then execute that file.
Check also this post about Win32 double-quote escaping rules.
Try this:
notice the 3
\: you want to pass the backslash to the shell (as if on the command line); if you only escape"once, the shell receives it as such, and interprets it as a string delimiter. what you want is that the shell receives it escaped, hence the double\in front of\".