I have created the following test case to show how to preserve null (\0) characters during Perl/C string conversion. You need to use newSVpvn to convert C char * to Perl string while preserving '\0' in the char *. I suspect the Win32::API module uses newSVpvf or similar that does not preserve the null characters. I would roll my own ShareMem in XS instead of creating a separate DLL and accessing it via Win32::API.
# ----- Makefile.PL ----- use ExtUtils::MakeMaker; WriteMakefile( 'NAME' => 'StringTest', 'VERSION_FROM' => 'StringTest.pm', 'PREREQ_PM' => {}, 'LIBS' => [], 'DEFINE' => '', 'INC' => '', ); # ----- StringTest.pm ----- package StringTest; require 5.00503; use strict; use warnings; require Exporter; require DynaLoader; our @ISA = qw(Exporter DynaLoader); our @EXPORT_OK = qw(set); our $VERSION = '1.00'; bootstrap StringTest $VERSION; 1; # ------ StringTest.xs ------ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" MODULE = StringTest PACKAGE = StringTest SV *set(SV *s, IV l) CODE: { char *str = malloc(l); int i; memcpy(str, SvPVX(s, l), l); // OK // debug the copy printf("(l=%d) ", l); for (i=0;i<l;i++) { printf("%02x ", str[i]); } printf("\n"); RETVAL = newSVpvn(str, l); // newSVpvn preserves null free(str); } OUTPUT: RETVAL # ----- stest.pl ------ use strict; use StringTest qw/set/; my $str = "A\0B\0C\0"; # a string with nulls print "OLD: $str\n"; my $new_str = set($str, length($str)); # create a new string print "NEW: $new_str\n"; # ------ OUTPUT ------ OLD: A B C (l=6) 41 00 42 00 43 00 NEW: A B C


In reply to Re: Re: Re: Problems Passing String Argument With Nulls From C To Perl by Roger
in thread Problems Passing String Argument With Nulls From C To Perl by Anonymous Monk

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.