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

Hi to all,
I am looking how to attache a cmdline tool to a console and controll it then.
1. Is this possible?
2. How do I attache the console to the spawned process?
a) do i first spawn the process and ttache the console
b) do I first launch console and start a process
c) how do I use $CONSOLE->Aloc(); togehter with the process?
Aloc() is ment not to have anny arguments . . . .
My sample code:
#! perl -w use strict; use Win32; use Win32::Console; my $pid; my $process = Win32::Spawn('testapp', 'testapp.exe with arguments', $p +id); my $CONSOLE = Win32::Console->new(); $CONSOLE->Alloc(); $CONSOLE->Title("Console Control"); $CONSOLE->Display() or die "Cant display Console"; sleep 30; __END__

Replies are listed 'Best First'.
Re: Win32::Console Question: How to attache a process to console
by BrowserUk (Patriarch) on Aug 04, 2010 at 17:01 UTC

    Why are you using Win32::Spawn?

    By default, processes inherit the same console as their parent.

    The way to change that default is to create one (or more) consoles prior to creating the process, and supply their handle(s) in the hStdInput, hStdOutput, hStdError elements of the STARTUPINFO structure passed to the CreateProcess().

    Alternatively, you could just obtain the console handles associated with the current process, before starting the child process, since it will inherit the console of it's parent.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Win32::Console Question: How to attache a process to console
by dasgar (Priest) on Aug 04, 2010 at 14:53 UTC

    I've never actually used that module before so I might be wrong. I believe that you use the alloc method to create the new console. Once you have the console created, you then can mimic keyboard and mouse control through the other methods in the Win32::Console module. Using one of those methods, you should be able issue a command in that console to use the command line tool that you want. Just beware that I haven't tested this, so I might be completely wrong.