How can I obtain a handle to the Win32 console the process is attached to without reference to one of the standard handles so that it doesn't matter what redirections the user makes?

The effect I want is to be able to write a message on the console immediately following normal program output regardless of any redirection in a similar way to the bash builtin time command. Essentially, similar to opening and writing to /dev/tty in Unix.

I've tried my $console = new Win32::Console() to allocate a new console followed by $console->Display() but this does completely the wrong thing.

There usually is a good reason to say 2>blubb. I hate it when programs demand attention when I told them to be silent.
–
IngoFeb 12 '13 at 22:12

@Ingo: Fair point but I'm not going to modify the requirements for a program I write for myself to suit you. :-)
–
Adrian PronkFeb 12 '13 at 22:18

You made my day! You mean, just in case you accidentaly type 2>NUL ....
–
IngoFeb 12 '13 at 22:30

@Ingo: No. My command is like bash's time and I want to use it like: cd this && mytime mvn ... >> ..\x 2>&1 && cd ..\that && mytime mvn ... >> ..\x 2>&1 with compile output in x and timings on screen. Plus it fills in what seemed to me to be a gap between what Win32::Console allows and what is possible with the Windows API.
–
Adrian PronkFeb 12 '13 at 22:57

I looked at the code for the new() function inside Win32::Console and saw that it just creates a hash containing the handle to a console. If the parameter specifies stdin/stdout/stderr, it just retrieves the associated handle otherwise it creates a new console screen buffer and uses the handle for that.

So I just manually created the Win32::Console object containing a handle to the console returned by CreateFile.

According to the AllocConsole() docs (C++ docs, but the concepts are the same):

"A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. A process can use the FreeConsole function to detach itself from its current console, then it can call AllocConsole to create a new console or AttachConsole to attach to another console."

Since your console is already redirected it doesn't look like there's anything you can do about it; even if you detach the console and allocate a new one, the new console inherits the redirection. In C++ you would use the SetStdHandle() API to force the standard handles to point to a different file or device, but I can't find any Perl equivalent of that.

Redirecting the standard handles does not free the console. CreateFile('conout$') will retrieve the handle to the allocated console but there's no direct way of using this in Perl's Win32::Console module.
–
Adrian PronkFeb 12 '13 at 22:05

Failing to see where I said that redirecting would free the console... In any case your comment about having no direct way to change it was my point. Also I would expect CreateFile on conout$ to also get you a handle to the redirected output.
–
HerrJoebobFeb 12 '13 at 22:10