简体   繁体   中英

C++ parsing a command line

I am trying to run two of the command lines in a C++ program, but landing in a strange error. The command lines that i want to run are

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

They both run fine on command prompt. But when in use a C++ system shell call to run these, the first one fails, where as second one works. The way i run these in C++ is as follows:

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");

The first command throws an error "G:/Program is not recognized as an internal or external command, operable program or batch file.", which is strange since the way both commands handle path to the file is the same. Please let me know the reason for this. The computer is running on Windows XP and i am using Microsoft Visual Studio 2010.

Replace

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");

with

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");

using system() to run commands has the following caveats and switch requirements for what you are attempting (from cmd documentation) as it effectively runs 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.

If you reformat your commands as:

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

that should work:) ( in the previous version I had forgotten some escape characters).

There is also a related question here.

Or if it is for windows only You could utilise ShellExecute or ShellExecuteEx with some documentation on the aforementioned at msdn, here

Hope this helps, as I just lost connection to my home environment and cannot get a fixed version for my previous fix at present, will do so tonight however.

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