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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.