Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How to hide/inhibit the console window when launching a Perl script on Windows?

( #536260=categorized question: print w/ replies, xml ) Need Help??
Contributed by jdporter on Mar 13, 2006 at 15:04 UTC
Q&A  > GUI Programming


Description:

When I launch a Perl script on Windows, there's a console window that gets started too. How can I make this go away, or not get created in the first place?

Answer: How to hide/inhibit the console window when launching a Perl script on Windows?
contributed by jdporter

Use Win32::GUI and the following lines:

use Win32::GUI; my $hw = Win32::GUI::GetPerlWindow(); Win32::GUI::Hide($hw);
Answer: How to hide/inhibit the console window when launching a Perl script on Windows?
contributed by jdporter

Use Win32::API. The following code does exactly what Win32::GUI does.

use Win32::API 0.20; # just for completeness... use constant SW_HIDE => 0; use constant SW_SHOWNORMAL => 1; # the API we need my $GetConsoleTitle = new Win32::API('kernel32', 'GetConsoleTitle', 'P +N', 'N'); my $SetConsoleTitle = new Win32::API('kernel32', 'SetConsoleTitle', 'P +', 'N'); my $FindWindow = new Win32::API('user32', 'FindWindow', 'PP', 'N'); my $ShowWindow = new Win32::API('user32', 'ShowWindow', 'NN', 'N'); # save the current console title my $old_title = " " x 1024; $GetConsoleTitle->Call( $old_title, 1024 ); # build up a new (fake) title my $title = "PERL-$$-".Win32::GetTickCount(); # sets our string as the console title $SetConsoleTitle->Call( $title ); # sleep 40 milliseconds to let Windows rename the window Win32::Sleep(40); # find the window by title $hw = $FindWindow->Call( 0, $title ); # restore the old title $SetConsoleTitle->Call( $old_title ); # hide the console! $ShowWindow->Call( $hw, SW_HIDE ); # sleep one second, then show the console again sleep(1); $ShowWindow->Call( $hw, SW_SHOWNORMAL );
Answer: How to hide/inhibit the console window when launching a Perl script on Windows?
contributed by jdporter

If you're using ActivePerl, use wperl.exe to run your script. This is a copy of perl.exe that just doesn't create a console window at all. Since you won't have STDIN/STDOUT/STDERR, you won't be able to catch errors. Therefore be sure your script is totally bulletproof before calling it with wperl.exe.

More info about this method is available.

Answer: How to hide/inhibit the console window when launching a Perl script on Windows?
contributed by jdporter

Using Win32::Process::Create, You could have a "starter" script which launches your script as a detached process:

Win32::Process::Create( $Obj, "$perl_path/perl.exe", "perl test.pl", 0, DETACHED_PROCESS, "$hs_path/scripts" ) or die ErrorReport();
Answer: How to hide/inhibit the console window when launching a Perl script on Windows?
contributed by jdporter

The following technique is useful when the window you want to hide isn't the one you're interacting with. You can see the program's console output if you run it from a command prompt; but if you launch the script from Explorer (double-click; Start>Run, etc.), this hides the window.

use Win32::API; sub hide_console { Win32::API->new( 'kernel32', 'FreeConsole', [], 'I' )->Call(); }
Answer: How to hide/inhibit the console window when launching a Perl script on Windows?
contributed by jdporter

You can create a standalone .exe file using PerlApp (from ActiveState's PDK) and use the -gui option which will suppress the DOS window on program startup.

Answer: How to hide/inhibit the console window when launching a Perl script on Windows?
contributed by jdporter

If you want to stop console windows from popping up when you call commands with backticks, try adding the following block to your script:

BEGIN { Win32::SetChildShowWindow(0) if defined &Win32::SetChildShowWindow; }

This answer was taken from ActiveState's Perl FAQ, and was pointed out by Eradicatore in supressing console with perl2exe and backticks.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (14)
    As of 2014-12-18 16:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (58 votes), past polls