in reply to Redirecting Output From Command Line in Threads

Win32::Process::Create creates processes, not threads. You might think that this is semantics, but the distinction is important - you will just confuse everyone if you talk about threads when you mean processes. If you are not sure of the difference, threads are several concurrent paths of execution within the same process, and require specialised techniques (not to mention the odd pagan incantation).

Meanwhile, back at your code, ikegami suggested some solutions, although you have a couple of problems with your code.

Your $perl_command has single back-slashes in it, which 'escapes' the character which follows (gives it a special meaning, or removes a special meaning if it had one). You did it right in the second parameter to Win32::Process::Create by using \\.

You should always test your return value when you can - if you get the chance to trap an error then take it!

Finally, you might find it easier to use the comspec environment variable to give you the path to the shell.

Here are my suggestions with modifications adapted to run on my system (you will have to alter the paths, watch those \):
#!/usr/bin/perl use strict; use Win32; use Win32::Process; my $ProcessObj; my $perl_command = ''; print "Starting thread logging test\n"; $perl_command = "$ENV{comspec} /c C:\\perl\\bin\\perl.exe C:\\output_t +est.pl >MyLog.txt 2>&1"; print "\n$perl_command\n"; my $result = Win32::Process::Create($ProcessObj,$ENV{comspec},$perl_co +mmand,1,CREATE_NO_WINDOW,"."); if ($result == 0) { die "Error: ",Win32::FormatMessage( Win32::GetLastError() ),"\n"; } print "Thread logging test completed\n";
By the way, you shouldn't really need a shell to do this redirection for you. The C/C++ Win32 API CreateProcess has a struct within it which includes handles for stdin, stdout and stderr of the child process. It is a great shame that the implementor of Win32::Process::Create chose to omit these.

Replies are listed 'Best First'.
Re^2: Redirecting Output From Command Line in Threads
by CKCJim (Beadle) on Feb 02, 2011 at 14:48 UTC

    Thanks for the valuable assistance. I'm not a Win32 guy by trade, so I'm still trying to absorb much of this info. Someone else I work with reminded me of the issue of threads vs. processes, and I am aware of the difference. As you say, it's semantics, but very important to avoid confusion, so please accept my apologies.

    I have applied your suggestions, and now my code is generating a log file for each "child" process. That addresses our primary need. I'm still looking at other options, as a separate log file for each process isn't where we really want to be, but at least we can move forward from here. In the long term I hope to come up with a solution that allows us to launch a large number of essentially simultaneous processes and have the command line output all logged to the same file, but if that isn't doable we will have to live with what we have right now.

    Again, thanks for your help.