JStrom has asked for the wisdom of the Perl Monks concerning the following question:

This does exactly what I want (aside from how system blocks on the child process):
system( "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe", "--rc-host", "127 +.0.0.1:9999" );
I've tried turning this into a Win32::Process::Create call:
Win32::Process::Create( $p, "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe", "vlc --rc-host 127.0.0.1:9999", 0, NORMAL_PRIORITY_CLASS, '.' ) or die; Win32::Process::Create( $p, "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe", "--rc-host 127.0.0.1:9999", 0, NORMAL_PRIORITY_CLASS, '.' ) or die;
Neither of which work. For the first one, VLC pops up a window saying it can't parse the command line. For the second, nothing happens at all (no error from perl, VLC doesn't seem to start).

Replies are listed 'Best First'.
Re: Starting a process under Win32
by ikegami (Patriarch) on Feb 18, 2008 at 06:29 UTC

    System-specific:

    system(1, qq{"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe" --rc-host 127 +.0.0.1:9999});

    Note the leading 1,. Documented in perlport.

    (I avoid the multiple arg form of system and exec on Windows for hardcoded commands since Windows doesn't have such a feature. Perl guesses at what the command should look like.)

    Portable:

    IPC::Open3

Re: Starting a process under Win32
by marcussen (Pilgrim) on Feb 18, 2008 at 06:05 UTC

    It would help if you could explain what you are attempting to achieve, such as do you need to block while the application is running, or perform additional tasks after running the command; since you are using system and not exec. Is there a particular reason you you would rather fork outside of your code?

    You could use the windows start to fork your process off, ie;

    system("start","C:\\Program Files\\VideoLAN\\VLC\\vlc.exe", "--rc-host +", "127.0.0.1:9999" ); [http://support.microsoft.com/kb/126410]
    but it might not be what you need.

    while ( whoring ){ for ( xp ){ grep /the source/,@perlmonks; }}
      I'm starting an instance of VLC as a TCP server, then connecting to it. So I can't exec nor block. I'm currently using this code:
      if( fork ) { system( "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe", "--rc-host", +"127.0.0.1:9999" ); exit; } sleep 1;
      Which seems like a sub optimal solution as I'm creating a thread to do nothing but block on a system(). And I need that kind of hacky sleep to make sure the system() executes before I try opening the socket.
Re: Starting a process under Win32
by cdarke (Prior) on Feb 18, 2008 at 13:16 UTC
    The problem is that the documentation is not clear, partly because of the way Microsoft document the underlying API CreateProcess. The 3rd argument must be the complete command-line, including the program name:
    my $program = 'C:\\Program Files\\VideoLAN\\VLC\\vlc.exe', Win32::Process::Create( $p, $program, "$program --rc-host 127.0.0.1:9999", 0, NORMAL_PRIORITY_CLASS, '.' ) or die;
    Note that the full path-name must be included.
      Seems clear to me
      SYNOPSIS use Win32::Process; use Win32; sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); } Win32::Process::Create($ProcessObj, "C:\\winnt\\system32\\notepad.exe", "notepad temp.txt", 0, NORMAL_PRIORITY_CLASS, ".")|| die ErrorReport(); $ProcessObj->Suspend(); $ProcessObj->Resume(); $ProcessObj->Wait(INFINITE);
      The program name needs to have the full path in the 2nd argument, but not the 3rd.
      I've changed it to
      Win32::Process::Create( $p, "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe", "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe --r +c-host 127.0.0.1:9999", 0, NORMAL_PRIORITY_CLASS, '.' ) or die $! . $^E;
      Still the same problem, VLC reports the command line is no good.
        OK. What is the exact text of the error message?