Allo, all.

I'm attempting to do some pointer work, in pure perl, under win32 (winxp, with AS perl 5.8.2, build 808). In particular, at this point, I want to get a long out of memory at a given address. I've tried using Pointer, but found it to give mildly odd results, possibly because I don't really understand it's API.

My current incarnation looks like this:

sub get_long_at { my $addr=shift; printf "get_long_at(0x%x)\n", $addr; my $data=unpack("P4", $addr); printf "Got it\n"; return unpack("L", $data); }
This does not have the expected effect. In particular... I get printed out get_long_at(0x6761c) (the address is as expected.) Then, I get a nice dialog box:

The instruction at "0x77c4337c" referenced memory at "0x34333234". The memory could not be "read".

Click on OK to terminate the program
Click on CANCEL to debug the program
Note that 0x34333234 is the ASCII representation of 4324. What does this mean? I have no idea. It seems to be a constant, though. The backtrace, once I say debug, looks like this.
MSVCRT! 77c4337c() PERL58! Perl_sv_setpvn + 149 bytes PERL58! Perl_unpackstring + 2264 bytes PERL58! Perl_unpackstring + 58 bytes PERL58! Perl_unpackstring + 10147 bytes PERL58! Perl_runops_standard + 12 bytes PERL58! RunPerl + 134 bytes PERL! 00401012() KERNEL32! GetCurrentDirectoryW + 68 bytes

Note that the segfault is in Perl_sv_setpvn, and not directly in the upack code, which suggests to me that $data=unpack("P4", $addr); is doing something different then what I thought it did, and not that perl itself is buggy. Of course, it's always possible that I'm wrong.


Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).


In reply to unpack "P" and a horrible death by theorbtwo

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.