简体   繁体   中英

Running CMD line in C++ Using variables (not string literals) as Params

I've executed this command in the CMD window and it works. I now need to run it in 2015 VC++ program code but I cannot get it. I've read the various posts dealing with this but they are mainly for string literals and I have variables. I think this is throwing off the double quotes. I do not have to use _wsystem if there is a better/safer way.

UPDATED:

std::wstringstream wss;
wss <<  std::wstring(L"\"") << CPathUtil::get_exe_fullpath() << std::wstring(L"\"") << L" /q /a " << std::wstring(L"\"") + cp.c_str() + std::wstring(L"\"");
const auto command = wss.str();
const auto result = _wsystem(command.c_str());

It still does not run from VC++ app.. command = "C:\\Users\\Valued Customer\\Documents\\Visual Studio 2015\\Projects\\svn_3dg_wc\\x64\\Debug\\MyAppD.exe" /q /a "C:\\Users\\Valued Customer\\Documents\\Visual Studio 2015\\Projects\\svn_3dg_wc\\Samples\\New folder\\C - Copy (2).abc" If I copy value from debugger and paste into CMD window, it works.
Any ideas?

The quotes you add in the last step before calling _wsystem turn the whole string into a single command . The quotes are needed to group space-delimited "words" in the command.

The contents of param should be something like

"C:\Users\Valued Customer\Documents\Visual Studio 2015\Projects\svn_wc\x64\Debug\MyApp.exe" /q /a "C:\Users\Valued Customer\Documents\Visual Studio 2015\Projects\svn_3dg_wc\Samples\New folder\C - Copy (2).abc"

Note that the /q /a part is not within quotes, only the paths to the files (which contains spaces and therefore needs the quotes).

Also note that there's no multiple of quotes anywhere, only single double-quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM