3

我试图在 C++ 程序中运行两个命令行,但出现一个奇怪的错误。我要运行的命令行是

vlc -vvv dshow:// :dshow-vdev='USB Video Device' :dshow-adev=""  :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display} :sout-keep

vlc -vvv udp://@localhost:1234:network-caching=50

它们都在命令提示符下运行良好。但是当使用 C++ 系统 shell 调用来运行这些时,第一个失败,而第二个可以工作。我在 C++ 中运行这些的方式如下:

system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv dshow:// :dshow-vdev=\"USB Video Device\" :dshow-adev=\"none\"  :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display}:sout-keep");

system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv udp://@localhost:1234:network-caching=50");

第一个命令抛出错误“G:/Program 不被识别为内部或外部命令、可运行程序或批处理文件。”,这很奇怪,因为两个命令处理文件路径的方式是相同的。请让我知道原因。计算机在 Windows XP 上运行,我使用的是 Microsoft Visual Studio 2010。

4

2 回答 2

0

代替

system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv dshow:// :dshow-vdev=\"USB Video Device\" :dshow-adev=\"none\"  :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display}:sout-keep");

system( "\"G:/Program Files/VideoLAN/VLC/vlc\" -vvv udp://@localhost:1234:network-caching=50");

system( "G:\\\"Program Files\"\\VideoLAN\\VLC\\vlc -vvv dshow:// :dshow-vdev=\"USB Video Device\" :dshow-adev=\"none\"  :live-caching=50 :sout=#transcode{vcodec=WMV2,vb=800,acodec=wma2,ab=128,channels=2,samplerate=44100}:duplicate{dst=udp{dst=localhost:1234},dst=display}:sout-keep");

system( "G:\\\"Program Files\"\\VideoLAN\\VLC\\vlc -vvv udp://@localhost:1234:network-caching=50");
于 2013-11-07T07:29:39.523 回答
0

using system()to run commands 在有效运行时对您正在尝试的内容(来自 cmd 文档)有以下警告和切换要求cmd /C *mycommandhere*

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.

如果您将命令重新格式化为:

    system("cmd /S /C \"\"c:\\testDir\"\"\\dir with spaces\"\"\\myexe.exe");

这应该工作:)(在以前的版本中我忘记了一些转义字符)。

这里还有一个相关的问题。

或者,如果它仅适用于 Windows,您可以使用ShellExecuteShellExecuteEx以及上述msdn 上的一些文档,这里

希望这会有所帮助,因为我刚刚失去了与我的家庭环境的连接,目前无法为我以前的修复获得一个固定版本,但是今晚会这样做。

于 2013-11-07T09:20:19.497 回答