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

How can I fetch the text from Windows Title Bars from all running processes that has a window open?
For example internet explorer, when visiting the site google.com has the title bar text "Google - Microsoft Internet Explorer"

I have tried to use  Win32::Process::Info, but the title bar text doesn't seem to be in the process information...

Anyone knows a fairly easy way to solve this problem?

Replies are listed 'Best First'.
Re: How can I fetch text from Windows Title Bars?
by bmann (Priest) on Sep 20, 2005 at 23:29 UTC
    Hi Ritter,

    Here's a working start using Win32::API and Win32::API::Callback to handle EnumWindows. Error handling is up to you ;)

    use strict; use warnings; use Win32::API; use Win32::API::Callback; my $getwindowtext = Win32::API->new( 'user32', 'GetWindowText', 'NPN', + 'N' ); my $enumwindows = Win32::API->new( 'user32', 'EnumWindows', 'KN', 'N' +); my $title = shift || "Mozilla"; $title = qr/\Q$title\E/i; my $cb = Win32::API::Callback-> new( sub { my $hwnd = shift; my $text = " " x 255; my $length = $getwindowtext->Call( $hwnd, $text, 255 ); $text = substr( $text, 0, $length ); if ( $text =~ $title ) { print "$text\n"; } 1; }, "NN", "N", ); my $ret = $enumwindows->Call( $cb, 0 );
    C:\S>perl enumwin.pl Mozilla Command Prompt - perl enumwin.pl Mozilla preview page - Mozilla Firefox
      The code works terrifically wonderful, thank you very much, bmann!

      And thanks you others for your suggestions of modules to use for this and similar tasks...

      WOW, thats cool :) I'm wondering if it can be done remotely with some sort of WMI interaction what do you think ?
Re: How can I fetch text from Windows Title Bars?
by wfsp (Abbot) on Sep 21, 2005 at 04:16 UTC
      Excelent module, I back this suggestion. And for all your X needs, the aptly named X::GuiTest.

      Evan Carroll
Re: How can I fetch text from Windows Title Bars?
by GrandFather (Saint) on Sep 20, 2005 at 22:38 UTC

    If you were working with the Win32 API directly you would use EnumWindows and GetWindowText, but I don't see anything that wraps EnumWindows in the Win32 documentation. Given that EnumWindows requires a call back function, it may be somewhat non-trivial to do.

    Perl is Huffman encoded by design.
Re: How can I fetch text from Windows Title Bars?
by Ultra (Hermit) on Sep 21, 2005 at 13:22 UTC
    Here is an article that may be useful to you.
    Dodge This!