我的程序出了点问题。我的目标是让它打开几个带有可选参数的.exe文件。例如,如果我想打开一个pdf,我可以在cmd窗口中输入下面的字符串。
// If used in a cmd window it will open up my PDF reader and load MyPDF.pdf file
"c:\Test space\SumatraPDF.exe" "c:\Test space\Sub\MyPDF.pdf"下面是我使用的两个尝试。第一个命令会打开PDF,但当然不会加载文件。第二种方法根本不起作用。
// Opens the PDF in my program
system("\"C:\\Test space\\SumatraPDF.exe\"");
// Error I get inside of a cmd window is the comment below
// 'C:\Test' is not recognized as an internal or external command, operable program or batch file.
//system("\"C:\\Test space\\SumatraPDF.exe\" \"C:\\Test space\\Sub\\MyPDF.pdf\"");我不确定第二个不起作用的原因。可能是我误解了系统的某些方面,或者我没有正确使用分隔符。
我觉得有一个专门为此而设计的库,而不是创建一个使用如此多分隔符的长字符串。
谢谢你的帮助。
发布于 2011-10-06 09:41:01
欢迎来到Stack Overflow!
system方法的工作方式是将它的参数传递给cmd /c,所以你需要在它周围加上一组额外的引号。请参阅sled发布的related question。
作为system的替代方法,可以查看ShellExecute或ShellExecuteEx Win32 API函数。它有更多的功能,尽管它不是那么便携。
// ShellExecute needs COM to be initialized
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
SHELLEXECUTEINFO sei = {0};
sei.cbSize = sizeof(sei);
sei.lpFile = prog; // program like c:\Windows\System32\notepad.exe
sei.lpParameters = args; // program arguments like c:\temp\foo.txt
sei.nShow = SW_NORMAL; // app should be visible and not maximized or minimized
ShellExecuteEx(&sei); // launch program
CoUninitialize();更多信息here。
https://stackoverflow.com/questions/7669433
复制相似问题