Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

How to hide window with Win32:API?

by Anonymous Monk
on Oct 28, 2002 at 12:54 UTC ( #208491=perlquestion: print w/replies, xml ) Need Help??

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

How can I hide perl script console window from Windows Taskbar? (I use ActiveState Perl) I think this should be done like that (not sure):
use Win32::API; $ShowWindow = new Win32::API("user32", "ShowWindow", [P, N], N); $ShowWindow->Call($hw, 0);
where $hw - is an application (running script) handle. But I dont know how to get this handle from Perl. Can anybody help, please?

Replies are listed 'Best First'.
Re: How to hide window with Win32:API?
by dada (Chaplain) on Oct 28, 2002 at 13:16 UTC
    you have several options:
    1. use the wperl.exe provided with ActivePerl to run your script. this is a copy of perl.exe that just doesn't create a console window at all. remember, however, that you have no STDIN/STDOUT/STDERR, so you can't even catch errors. be sure your script is totally bulletproof before calling it with wperl.exe. also note that more info about this method are available here.
    2. use Win32::GUI and the following lines:
      use Win32::GUI; my $hw = Win32::GUI::GetPerlWindow(); Win32::GUI::Hide($hw);
    3. use Win32::API, but this requires a lot of work:
      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 );
      what the above code does is exactly what Win32::GUI does (a nasty trick I've found in the MSDN documentation :-).


    King of Laziness, Wizard of Impatience, Lord of Hubris

Re: How to hide window with Win32:API?
by Nitrox (Chaplain) on Oct 28, 2002 at 13:58 UTC
    You could also simply use a "starter" script to launch your process as a detached process:
    Win32::Process::Create($Obj, $perl_path . "/perl.exe","perl",0 +,DETACHED_PROCESS,$hs_path . "/scripts") || die ErrorReport();


Re: How to hide window with Win32:API?
by simonflk (Pilgrim) on Oct 28, 2002 at 18:12 UTC

    This works on my system as long as I'm not trying to hide a window I'm typing in. Which is pretty useful - I can see STDERR / debugging info if I run the command from a shell, but the window is hidden if launched from explorer / start > run / etc

    sub hide_console { # Hide the dos window require Win32::API; my $FreeConsole = new Win32::API('kernel32', 'FreeConsole', [], 'I +'); $FreeConsole->Call(); }

    -- simonflk

    update: according to MSDN, this should work on Windows 95 and above.

Re: How to hide window with Win32:API?
by rochlin (Acolyte) on Oct 28, 2002 at 19:26 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://208491]
Approved by broquaint
Front-paged by tye
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2021-12-02 10:44 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (18 votes). Check out past polls.