Visual Studio 2022中system调用执行VLC启动命令异常求助
问题分析与解决
你的代码问题出在start命令的特殊行为上:当使用start启动带空格的路径时,start会将第一个被引号包裹的字符串视为新窗口的标题,而不是要执行的程序路径。所以你当前的命令cmd /c start "c:\Program Files\VideoLAN\VLC\vlc.exe",实际上是创建了一个标题为c:\Program Files\VideoLAN\VLC\vlc.exe的空CMD窗口,并没有真正启动VLC。
修复方案
给start命令添加一个空的标题参数(即""),让它把后续的引号内容识别为程序路径。修改代码如下:
#include <string> #include <iostream> using namespace std; int main() { string Command1 = "cmd /c start \"\" "; // 添加空标题参数 string Command2 = "\"c:\\Program Files\\VideoLAN\\VLC\\vlc.exe\""; string myCommand = Command1 + Command2; // cout << myCommand; // 可取消注释查看最终生成的命令 system(myCommand.c_str()); return 0; }
最终生成的命令是cmd /c start "" "c:\Program Files\VideoLAN\VLC\vlc.exe",这样就能正确启动VLC播放器了。
另外,若不需要单独的CMD窗口,也可以直接调用VLC程序,代码可简化为:
#include <string> #include <iostream> using namespace std; int main() { string vlcPath = "\"c:\\Program Files\\VideoLAN\\VLC\\vlc.exe\""; system(vlcPath.c_str()); return 0; }
内容的提问来源于stack exchange,提问作者Aventurijn
相关产品推荐
相关产品推荐

