Nick, Thanks for your help. Setting sv_setpvn(ST(N),bufl,size); works fine. What I was trying to do earlier was to set this in the OUTPUT section as well!!! Having this line of code outside the OUTPUT section works perfectly fine!
Thanks once again! Regards, Gopa -----Original Message----- From: Nick Ing-Simmons [mailto:[EMAIL PROTECTED] Sent: Monday, July 12, 2004 1:22 AM To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED] Subject: Re: passing char* to-from XS :( Marcus Holland-Moritz <[EMAIL PROTECTED]> writes: >Hi Gopa, >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. Agreed. > >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 :(( We need to see your XS code. My guess is that the _default_ OUTPUT typemap for char * having no other knowledge of how big "string" is does strlen() on it and so stops at 1st 0x00. It is probably simplest to return binary strings by explicit sv_setpvn(ST(N),bufl,size); somewhere, which will set SvCUR for you. On the other hand if you are always packing in input and unpacking on output why not make the perl API take list on input and return list >> >> 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? > > >Marcus