I've successfully made a shared library that can calculate directory sizes on Win32, and I can link to it via a 100% C utility. Now I want to build a perl module to access the lib. Unfortunately, I'm having some difficulties.

The function in the library is "_dir_size()" and the function I want to expose to perl is "dir_size()". The perl function will work like this:

my $hashref; my $result = dir_size("C:\\temp", $hashref); print "File count = ", $hashref->{FileCount}, "\n"; print "Dir size = ", $hashref->{LowSize}, "\n";

I have this XS function defined:

int dir_size (dirname,dirinfo,permsdie=0,otherdie=0) PREINIT: AV *errs = newAV(); HV *newdirinfo = newHV(); unsigned long hightotalsize = 0; unsigned long lowtotalsize = 0; long filecount = 0; long dircount = 0; INPUT: SV *dirinfo; char *dirname; int permsdie; int otherdie; CODE: RETVAL = _dir_size (errs, permsdie, otherdie, dirname, &highto +talsize, &lowtotalsize, &filecount, &dircount); hv_store(newdirinfo, "Errors", 6, newRV((SV *)errs), 0) +; hv_store(newdirinfo, "HighSize", 8, newSVuv(hightotalsize), + 0); hv_store(newdirinfo, "LowSize", 7, newSVuv(lowtotalsize +), 0); hv_store(newdirinfo, "FileCount", 9, newSViv(filecount), 0) +; hv_store(newdirinfo, "DirCount", 8, newSViv(dircount), 0); dirinfo = newRV_noinc((SV *)newdirinfo); printf("Found %i files\n", filecount); OUTPUT: dirinfo RETVAL

The printf() call in the above XS spits out the right number, but back in perl, my $hashref is undef. Any ideas?


In reply to XS returning undef? by meetraz

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.