Hi Marcus,
My apologies for posting a query without my sample code!
So here goes:

###########Test.pl
$buffer = "Test";
#myTest is the XS routine which would change $buffer! Instead of "test",
this is to be filled with "XSTest" (2 more #characters
myPack::myTest($buffer); 

################Test.xs
void myTest (buffer)
        char* buffer

PREINIT:
        char tmp[7];
CODE:
        strcpy(tmp, "XSTest");
        strcpy(buffer, tmp);
OUTPUT:
        buffer

####################################
I would expect buffer at the .pl side to hold "XSTest". But it holds only
"Test". I guess it's because there is space allocation only for 4 bytes; if
I initialize $buffer to hold 8 characters (i.e. 8 bytes?) and send it down
to the XS function I get back the whole string I set there (i.e. XSTest). Is
there another way of getting this done than by forcing "memory allocation"
at the .pl side to hold atleast as many bytes the XS code could return?

Thanks for looking into this problem!

Thanks,
Gopa

-----Original Message-----
From: Marcus Holland-Moritz [mailto:[EMAIL PROTECTED] 
Sent: Friday, July 09, 2004 8:21 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: passing char* to-from XS :(

Hi Gopa,

On 2004-07-08, at 20:45:21 +0530, Gopakumar Ambat wrote:

> Hi,
> I can't seem to figure out a way to get this right:(... 
> 
> I have a C function which accepts a char*, a true/false flag, another 
> char* and an int for the size of the last buffer...
> void myTest (char* buf1, int flag, char* buf2, int buf2_size).
> 
> buf1 would be passed as a scalar (I don't intend to modify this value 
> from the Perl script side).. Depending on the "flag", buf2 could be a 
> character array (from Perl script) whose size is specified in 
> buf2_size OR has to be filled in at the C side and the size of the buffer
set to buf2_size.
> The perl call for this XS interface would be:
> 
> @buf1_arr = (0x12, 0x00, 0x00, 0x00, 0x5C, 0x00); pack("C*", 
> @buf1_arr);
> $buf1 = pack("C*", @buf1_arr); # this gets me a packed value!
> $flag = 0; # 0 implies buf2 is not filled here, to be filled from the 
> C code!
> 
> myPack::myTest($buf1, $flag, $buf2, $buf_size); if($flag == 0)
>   @mydat = unpack ("C*", $buf2);
> 
> My XS/C code, seems to be working pretty fine... I allocate memory to 
> buf2 if I need to (Newz), copy the required data to buf2 and set buf2 
> in the OUTPUT list. If I try printing buf2 in the XS side, it gives me 
> proper data (it normally has around 100 bytes), but when I get it back 
> in .pl and print it I get only the first 6 bytes :((
> 
> Tried manipulating the stack -
> OUTPUT
> buf2  sv_setpv(ST(2), (char *)&my_data_buf, 100);
> with same result... 
> 
> On the other hand if I have a char arr[100], memset it with an 
> arbitrary value and copy that to buf2 it comes out fine at the .pl 
> end!! I guess there is something wrong with my stack handling, could
anyone suggest a way out?

Chances of getting help on this list usually increases dramatically when
including _full_ working (or in your case non-working) example code with
your questions. Then we don't have to spend a lot of time guessing how you
might have coded the things you tried to explain above. Also, it's always
nice to see the results you get as well as the results you expect.

Marcus

-- 
    All language designers are arrogant.  Goes with the territory... :-)
            --Larry Wall in <[EMAIL PROTECTED]

Reply via email to