0

How do I pass a string with a double quote as a character as a command line argument?

The string basically has a space in between because of which it was getting treated as two different arguments. So I tried putting them in double quotes, like this.

wstring cmdLineParam = L"\"Test Value=abc\"";

This works perfectly fine in most of the cases. But now if I have a double quotes as a character in the string, it fails.

wstring cmdLineParam = L"\"Test Value=abc \"test me\"\"";// I want the string to be passed as Value=abc"test me"

Here the argument gets split into two arguments

argv[0] = "Test Value=abc test" argv[1] = "me"

This is my sample code.

wstring cmdLineParam = L"\"Test Value=abc \"test me\"\""; // Need to pass it as Value = abc "xyz uvw"

    wstring path = L"C:\\cmdtest.exe";

    BOOL bSuccess = CreateProcess ((LPCWSTR)path.c_str(), (LPWSTR)cmdLineParam.c_str(), NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
                                    NULL, NULL, &si, &piProcess);

and as I mentioned, the output is

argv[0] =  "Test Value=abc test"
argv[1] = "me"

Kindly guide

Thanks & Regards Sunil

S_R
  • 493
  • 1
  • 9
  • 22
  • Have you tried escaping the backslash like this `L"\\\"Test Value=abc \\\"test me\\\"\""`? – Axel Mar 20 '14 at 15:24
  • Yes, I tried that. It didnt work – S_R Mar 20 '14 at 15:26
  • There's also the question of whether Windows even supports escaping double quotes in command lines in the first place. (Although, this suggests it should: http://msdn.microsoft.com/en-us/library/17w5ykft.aspx) It's probably better to pass complicated input through stdin. – millimoose Mar 20 '14 at 15:26
  • Please look at http://stackoverflow.com/questions/7760545/cmd-escape-double-quotes-in-parameter and tell us if using the suggested `^` as escape character works. – Axel Mar 20 '14 at 15:29

0 Answers0