Passing scalars to C functions

J

Josef Moellers

I'm fairly new to the XS business. I have managed to get the skeleton up
(h2xs) and have also managed to write short functions that have ints as
arguments and return strings, but I'd like to pass a full Perl scalar
(my $cdb = pack('C*', 0x12, 0, 0, 0, 128, 0);) and retrieve the length
(6) and the bytes themselves in the C function and also return such a
scalar.

How do I do that?

Thanks,

Josef
 
J

Josef Moellers

Ben said:
MODULE = My::XS PACKAGE = My::XS

SV *
my_xs_func(sv)
SV *sv
PREINIT:
const char *bytes;
STRLEN len;
CODE:
bytes = SvPV(sv, len);
RETVAL = sv_2mortal(newSVpvn(bytes, len));
OUTPUT:
RETVAL

Of course, there are lots of other ways, this being Perl. See perlxs for
the details.

Thanks. This works.

Josef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top