Perl Win32::API buffer overflow at GetWindowPlacement

Viewed 63

my problem is getting a WINDOWPLACEMENT structure from GetWindowPlacement on a windows 10 machine. I get a valid window handle and also creating the structure looks good but calling GetWindowPlacement fails. here is my code:

#!/usr/bin/perl

use strict;
use warnings;
use Win32::API;

my $wpl; # WINDOWPLACEMENT structure 
my $hwnd;
my $rv;

Win32::API->Import("user32",   "FindWindow",         "PP",   "N");
Win32::API->Import("user32",   "GetWindowPlacement", "NP",   "I");

Win32::API::Struct->typedef( 'POINT', qw(
    LONG x; 
    LONG y; 
));
Win32::API::Struct->typedef( 'RECT', qw(
    LONG left;
    LONG top;
    LONG right;
    LONG bottom;
));
Win32::API::Struct->typedef( 'WINDOWPLACEMENT', qw(
    UINT  length;
    UINT  flags;
    UINT  showCmd;
    POINT ptMinPosition;
    POINT ptMaxPosition;
    RECT  rcNormalPosition;
    RECT  rcDevice;
));

$hwnd = FindWindow('SciCalc', 0);
print("window handle: $hwnd\n");

$wpl = Win32::API::Struct->new( 'WINDOWPLACEMENT' ); 
$wpl->{length} = Win32::API::Struct::sizeof($wpl);
print("pointer: $wpl\n");
print("length: $wpl->{length}\n");

$rv = GetWindowPlacement($hwnd, $wpl);
print("return value: $rv\n");

exit(0);

__END__

the result is:

window handle: 722532
pointer: Win32::API::Struct=HASH(0x2609b60)
length: 60
Win32::API::Call: parameter 2 had a buffer overflow at C:\Users\ma\MyFolder\test\winpos.pl line 41.

Using AutoIt3 for getting the WINDOWPLACEMENT structure gives me:
![AutoIt3 result]AutoIt3

1 Answers

The solution is to change:

Win32::API->Import("user32",   "GetWindowPlacement", "NP",   "I");

to:

Win32::API->Import("user32",   "GetWindowPlacement", "NT",   "I");

Found in the documentation.

Related