# Example 2.xx: # ---------------------------------------- # From "Win32 Perl Programming: Administrators Handbook" by Dave Roth # Published by Macmillan Technical Publishing. # ISBN # 1-57870-215-1 # This dumps the class and title (if available) of each window. use Win32::API; $GW_CHILD = 5; $GW_HWNDNEXT = 2; $GetWindow = new Win32::API( 'User32.dll', 'GetWindow', [N,N], N ) || die; $GetTopWindow = new Win32::API( 'User32.dll', 'GetTopWindow', [N], N ) || die; $GetWindowText = new Win32::API( 'User32.dll', 'GetWindowText', [N,P,N], N ) || die; $GetClassName = new Win32::API( 'User32.dll', 'GetClassName', [N,P,N], N ) || die; sub GetClassName { my( $hWnd, $String, $Size ) = @_; if( ! $GetClassName ) { $GetClassName = new Win32::API( 'User32.dll', 'GetClassName', [N,P,N], N ) || die "Can not load GetClassName\n"; } return( $GetClassName->Call( $hWnd, $$String, $Size ) ); } if( $hWnd = $GetTopWindow->Call( 0 ) ) { # DumpWindow( $hWnd ); ProcessWindow( $hWnd ); } sub ProcessWindow { my( $hWnd ) = @_; if( $hWnd ) { do { DumpWindow( $hWnd ); ProcessWindow( $GetWindow->Call( $hWnd, $GW_CHILD ) ); } while( $hWnd = $GetWindow->Call( $hWnd, $GW_HWNDNEXT ) ); } print "\n"; } sub DumpWindow { my( $hWnd ) = @_; my $Size = 256; my $Class = String( $Size ); my $Title = String( $Size ); my $Length; $Length = $GetClassName->Call( $hWnd, $Class, $Size ); $Class = FixString( $Class ); # $Length = GetClassName( $hWnd, \$Class, $Size ); $Length = $GetWindowText->Call( $hWnd, $Title, $Size ); $Title = FixString( $Title ); printf( "Wnd: 0x%08x '%s' (%s)\n", $hWnd, $Class, $Title ); } sub String { my( $Size ) = @_; return( "\x00" x ( ++$Size * ( 1 + Win32::API::IsUnicode() ) ) ); } sub FixString { my( $String ) = @_; $String =~ s/\x00//gs; return( $String ); }