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

I have a perl gui application that needs to get the results of a dos command (like ipconfig /all) but I do not want the console window to flash when I call it. Is there a way to do this.
#!perl #when the following command is run in gui mode, it flashes a console w +indow... my @results=`ipconfig /all`;

Replies are listed 'Best First'.
Re: Console Window Minimized
by ccn (Vicar) on Sep 20, 2004 at 15:41 UTC

    # Listing 3. Code to Hide a Perl Console Window use Win32::API::Prototype; ApiLink( 'kernel32.dll', 'HWND GetConsoleWindow()' ) || die; ApiLink( 'user32.dll', 'BOOL ShowWindow( HWND hWnd, int iCommand )' ) || die; $hWnd = GetConsoleWindow(); print "About to hide the window...\n"; sleep( 1 ); # Hide the window. # BEGIN CALLOUT A ShowWindow( $hWnd, 0x00 ); # END CALLOUT A my @results=`ipconfig \all`; print @results; sleep( 5 ); print "About to restore the window...\n"; # Restore the window. # BEGIN CALLOUT B ShowWindow( $hWnd, 0x04 ); # END CALLOUT B print "Restored!\n";

    May be it will be useful.

    This code comes from Dave Roth's article Progressive Perl for Windows: Hiding the DOS Box and Other Magical Tricks

      You misread the question. I already know how to hide the console that launched the perl process. How do you hide the console window that is launched when perl calls a dos command and at the same time, capture its output?
        No, I do not misread. A console application (launched from a script) requires a console window. If the script is running in console already then new applicaion uses that console. If the script is executed by wperl (without console window) then the system creates new console window for that application.

        I meant that you can start your script in console, then hide it with Windows API function, then call any console application -- all of them will use the hidden console.

        I've tested the code before post it. It works as desired.

        Also I think that Win32::API::Prototype module is obsolete, you can use pure Win32::API instead of it.

Re: Console Window Minimized
by Scarborough (Hermit) on Sep 20, 2004 at 15:42 UTC
    If you compile with perl2exe you can use a switch option -gui and the program runs without a command window. Its what I've used in the past.