Mark
Mark

Reputation: 1650

Win32::SerialPort constructor goes buffer-overflow when reference variable is given

This works well:

my $PortObj;    
$PortObj = new Win32::SerialPort ("COM12") || die "Can't open COM\n";

However when I try to provide a variable instead of "COM12" from TK Entry, like this:

my $portNumVar = "12";
my $portNum = $mw->Entry(-justify=>'center',-width=>'5',-textvariable=>\$portNumVar)->pack();

Inside a subroutine (after a button click):

my $PortObj;
my $com = "COM".$portNumVar;
print $com;
$PortObj = new Win32::SerialPort ($com) || die "Can't open COM\n";

I get this error:

Win32::API::Call: parameter 1 had a buffer overflow at C:/Perlx86_5.16/site/lib/Win32API/CommPort.pm line 176.

How is it even possible? Shouldn't it be the same?

Thanks, Mark.

Upvotes: 4

Views: 634

Answers (2)

Valentin
Valentin

Reputation: 31

Got the same problem with Wx and Win32::SerialPort. In my case this workaround worked: $PortObj = new Win32::SerialPort (eval(qq("$com"))) || die "Can't open COM\n";

Upvotes: 3

user1126070
user1126070

Reputation: 5069

You need to dereference a variable if it is a reference (passed as \$val) to get the right value and not its memory address (pointer).

my $com = "COM".$$portNumVar;

Upvotes: -1

Related Questions