Reputation: 12381
I want to pass only ONE parameter containing some spaces to my function main. Here is an example:
string param = "{\"abc\" \"de\"}"; // the string is {"abc" "de"}
boost::replace_all(param, "\"", "\\\""); // now it becomes: {\"abc\" \"de\"}
boost::replace_all(param, " ", "\\40"); // now it becomes: {\"abc\"\40\"de\"}
ShellExecute(GetDesktopWindow(), "open", "myMainTest.exe", param.c_str(), "", SW_SHOWNORMAL); // execute my function main in another project
// in the function main of myMainTest.exe
cout<<argv[1];
I got this result:
{"abc"\40"de"}
It means that the double quote is OK but the space is not.
Upvotes: 0
Views: 2752
Reputation: 149075
IMHO, this is directly tied to the way windows processes its command line. Arguments are normally splitted on spaces with the exception that strings enclosed in double quotes ("
) are processed as a single parameter after removing quotes.
But it is far from the way Unix-like shells processes input! No simple and direct way to escape a quote itself. But as your quotes are balanced it will work. Here is the actual string that you must pass to ShellExecute
: "{\"abc\" \"def\"}"
. Now only remains how to write that is C++ source:
string param = "\"{\\\"abc\\\" \\\"def\\\"}\"";
ShellExecute(GetDesktopWindow(), "open", "myMainTest.exe", param.c_str(), "", SW_SHOWNORMAL);
And myMainTest.exe
should see only single parameter: {"abc" "def"}
Upvotes: 2