UPDATE:
This issue was caused by my ignorance of my Perl's pointer size (8 bytes in this case). Packing the pointer into an integer of the correct size "fixes" the issue I was experiencing. Thank you tye and ikegami for your help!

(It is obvious now in hindsight, but maybe the Perldocs for pack/unpack could be updated to mention the 'p/P' templates and Perl's pointer size?)




Good evening!

I am trying to more clearly learn how pack/unpack works with values returned from Win32::API, specifically pointers and how to dereference them. I have a small example program that calls a "cat" function in a "test.dll" that takes two strings and returns them concatenated together.

Setting the output parameter in the function template to char* works as expected:

#!/usr/bin/perl use 5.018; use strict; use warnings; use Win32::API; use Data::Dumper; $Win32::API::DEBUG = 1; my $method = new Win32::API::More('test.dll', 'char* cat(char* a, char +* b)'); if( !defined $method ) { die "Can't import API [cat]: $^E\n"; } my $return = $method->Call('my ', 'potato'); print Dumper $return; # prints 'my potato'

Setting the output parameter to int in the function template returns a valid pointer (I can peek at that memory address and see the concatenated string), and I can dereference it with various combinations of pack/unpack, but I do not know why some work:

#!/usr/bin/perl use 5.018; use strict; use warnings; use Win32::API; use Data::Dumper; $Win32::API::DEBUG = 1; my $method = new Win32::API::More('test.dll', 'int cat(char* a, char* +b)'); if( !defined $method ) { die "Can't import API [cat]: $^E\n"; } my $return = $method->Call('my ', 'potato'); print Dumper $return; # pointer address: 42952784 # works as expected: 'my potato' my $x = unpack 'p', pack 'J', $return; print Dumper $x; # works as expected: 'my potato' my $x = unpack 'p', pack 'Q', $return; print Dumper $x; # does not work as expected: undef my $x = unpack 'p', pack 'I', $return; print Dumper $x; # does not work as expected: undef my $x = unpack 'p', pack 'L', $return; print Dumper $x; # does not work as expected: undef my $x = unpack 'p', pack 'N', $return; print Dumper $x; # Update: I made a typo in my original post. # This (obviously) will not work on little-endian machines #my $x = unpack 'p', pack 'N2', $return; #print Dumper $x; # works(??): 'my potato' #my $x = unpack 'p', pack 'V2', $return; #print Dumper $x; # works(??): 'my potato' my $x = unpack 'p', pack 'IN', $return; print Dumper $x;

What exactly is going on with the different combinations of pack/unpack?

Perl info:

perl -v This is perl 5, version 18, subversion 4 (v5.18.4) built for MSWin32-x +64-multi-thread

USE_64_BIT_INT has been enabled as well.
Version 0.82 of Win32-API

Thank you for your time


In reply to [SOLVED] Dereferencing a pointer returned from Win32::API with unpack 'p' by ateague

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.