Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Win32 GUI Window Problem

by Acid Amygdala (Novice)
on Jan 24, 2009 at 00:34 UTC ( [id://738626]=perlquestion: print w/replies, xml ) Need Help??

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

Can anyone enlighten me on how to keep GUI Windows open unless I specifically terminate the window? This project looks into a log file and creates popups using the information found there. The log file will be written to almost constantly. The problem is that I need each Window to stay open until I exit it, however, when the code finishes reading the log file (whether or not more is then added to it), all of the open windows automatically close. It seems that this is the result of the windows terminating as the foreach loop is completed. Does anyone know how I could alter this so the windows are not terminated with the completion of the loop, or if that is even possible? Here is a piece of the log file:
Time: 10/31-13:36:10.802641 event_ref: 0 (portscan) TCP Portsweep Priority Count: 5 Connection Count: 8 IP Count: 13 Scanned IP Range: Port/Proto Count: 3 Port/Proto Range: 22:3389 Time: 10/31-13:37:58.075795 event_ref: 0 (portscan) TCP Portsweep Priority Count: 5 Connection Count: 9 IP Count: 13 Scanned IP Range: Port/Proto Count: 3 Port/Proto Range: 25:3389 Time: 10/31-13:38:10.031849 event_ref: 0 (portscan) ICMP Sweep Priority Count: 5 Connection Count: 13 IP Count: 12 Scanned IP Range: Port/Proto Count: 0 Port/Proto Range: 0:0
And here is my code:
use warnings; use Win32::GUI(); use Win32::Sound; use diagnostics; $filename="log3.txt"; open(TXT, $filename)||die("Could not open file!"); @filedata = <TXT>; close(TXT); @alert = ['a'..'z']; $i=0, $p=1; foreach $element (@filedata) { if($element =~ /(\s+)(\d{1,4})(\.\d{1,4}){3}(\s+)/) { &Build_Window; } ++$i; ++$p; } open (TXT, ">$filename"); close TXT; sub Build_Window { $datetime = localtime(); $alert[$i] = new Win32::GUI::Window(-name => "Alert", -width = +> 500, -height => 150, -pos => [$p*2, $p*2]); $font = Win32::GUI::Font -> new(-name => "Arial", size => 46, +-bold => 1); $alert[$i] -> AddLabel(-text => $datetime, -font => $font); $alert[$i] -> AddLabel(-text => "SNORT ALERT: MALICIOUS BEHAVI +OR DETECTED!", -font => $font, -top => 50); $alert[$i] -> AddLabel(-text => $element, -font => $font, -top + => 75); my $t1 = $alert[$i] -> AddTimer('T1', 1000); Win32::Sound::Play("SystemExclamation"); $alert[$i] -> Show(); Win32::GUI::Dialog(); sub T1_Timer { while(@filedata){return -1;} } sub Alert_Terminate { return 1; } }
Also, if you know of any websites that could help me with this, that would be greatly appreciated as well. Thanks so much for your time and help.

Replies are listed 'Best First'.
Re: Win32 GUI Window Problem
by BrowserUk (Patriarch) on Jan 24, 2009 at 00:55 UTC
      This makes sense, however, it doesn't work when I try it. Do you have any idea why?
        It seems that it has to do with the windows automatically terminating at the completion of the foreach loop. Any ideas for fixing this? All comments, advice and ideas are greatly appreciated. And a big thank you to BrowserUk for your advice.
        Your first version of code with the excellent suggestion from BrowserUK looks like it should work. What may be happening here is that the windows you put up are just info info windows and re-display of these windows happens so fast that you don't notice it before program ends.

        Try putting a sleep(10) after BrowserUK's suggestion to see if windows really do come back. If that works, then you need to put a Window up that asks a question and waits for a user response. I'm not familiar with the particular module that you are using, but for example with Win32::MsgBox (I didn't worry about import of specific constants, etc).. just the basics .....

        use Win32; my $ERROR_OK_BOX=16; # Red Error X box with an ok button Win32::MsgBox("Some errors deteted, click Ok to end program", $ERROR_OK_BOX,"some_title" ); print "program now exiting\n";
        This segment of code will cause a "block" until user hits the OK button, ie program hangs until user does something. So presumably whatever you've already got up there will stay up there.
Re: Win32 GUI Window Problem
by jplindstrom (Monsignor) on Jan 28, 2009 at 13:43 UTC

    Don't call Win32::GUI::Dialog many times.

    Create the windows, populate them, show them, and then call Win32::GUI::Dialog once. This is the main loop which runs until any event handler returns -1.

    As it is now, the code will put up a window, wait for you to close it, then look at the next line.

    Personally, I'd create a single window and add rows to a text area or something like that.

    (If in the future you want to do something like tail -f you need to poll file handles IO::Select. Set up a timer to check if there's anything to read.)

    Other resources

    I think you need to read an intro to Win32::GUI, especially how event handlers work and how window names relate to them (this is a bit clunky in Win32::GUI).

    There's a mailing list which may be able to provide better answers: win32-gui-users

    There's a half finished tutorial which covers the basics about gui programming with Win32::GUI.

    /J

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://738626]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-18 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found