I have created a very simple DLL that exports a function called inc_number(), whose function is to increment a number. I imported it into Perl and invoked it like so:

use Win32::API; die "ack!" unless Win32::API->Import('testdll.dll', 'int inc_number(in +t *num)'); my $num = 5; inc_number($num); print "Number is now $num\n";


Instead of the "Number is now 6" that I was hoping for, I got:

The instruction at 0xblahblah referenced memory at 0x00000005. The memory could not be "written".

If I change the initial value of $num to 8, then the memory address in the illegal operation changes to 0x00000008 as well.

It seems like maybe Perl isn't properly recognizing that this is a by-ref parameter, and is passing by value instead?

UPDATE

I tried using the deprecated method of importing a function where you specify the parameter types instead of giving it the C prototype. It worked! Almost. 7 becomes 8, 8 becomes 9, and 9 becomes colon. 900 becomes :00. It seems like it's just incrementing the ASCII value of the first character in the numeric representation of my value. Hmm.

In reply to Win32::API - Passing "by reference" by EricDB

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.