Okay, you can't use "P" for a return value unless what is being returned is a simple '\0'-terminated string. So you have to get the raw pointer back (a return value that is a struct that won't fit into a register is returned as a pointer to the struct -- I think). So you use a return value of "I" and use little-used parts of unpack() [and even pack()]. I also removed the one argument being passed in as that got it working for me:
use strict; use Win32::API; my $CpuSpeed ||= Win32::API->new ("cpuinf32", "cpuspeed", [], "I") or die "Could not load DLL\n"; my $aInfo = $CpuSpeed->Call(); my $pInfo = pack("L",$aInfo); my $sInfo = unpack("P16",$pInfo); my ($in_cycles, $ex_ticks, $raw_freq, $norm_freq) = unpack("L4",$sInfo); print "In cycles: $in_cycles\n"; print "Ex ticks: $ex_ticks\n"; print "Raw freq: $raw_freq\n"; print "Norm freq: $norm_freq\n";
The reason that you can't use "P" is because Perl really hates to deal with pointers except to buffers that it allocated itself. If you use "P", then Win32::API will look at that address for the first '\0' and copy all of the characters upto and including it into a buffer that Perl likes, which would mean that the copy will be missing much of the data since the long values are small enough that they have zero bytes in them.
So here is what is going on in the working code...
$aInfo gets set to an integer containing the address of the buffer that Perl doesn't want to deal with very well. pack() builds a string, $pInfo, that contains this same pointer value because the only way Perl (by itself) can deal with foreign pointers is via "p" and "P" of unpack() and those require that the pointer be packed into a string. unpack("P16") copies the 16 bytes that the pointer points to into a buffer that Perl likes, $sInfo. The final unpack() pulls the values out of this copy of the struct.
- tye (but my friends call me "Tye")In reply to (tye)Re: Difficulties with Win32::API
by tye
in thread Difficulties with Win32::API
by Guildenstern
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |