Discussion:
Passing scalars to C functions
(too old to reply)
Reinhard Pagitsch
2009-06-05 11:12:49 UTC
Permalink
Hi,
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
Did you looked at the eval_* functions in perlapi.pod?

eval_pv

Tells Perl to eval the given string and return an SV* result.
NOTE: the perl_ form of this function is deprecated.
SV* eval_pv(const char* p, I32 croak_on_error)

eval_sv

Tells Perl to eval the string in the SV.
NOTE: the perl_ form of this function is deprecated.
I32 eval_sv(SV* sv, I32 flags)


regards,
Reinhard
Reinhard Pagitsch
2009-06-05 13:07:10 UTC
Permalink
Post by Reinhard Pagitsch
Hi,
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
Did you looked at the eval_* functions in perlapi.pod?
eval_pv
Tells Perl to eval the given string and return an SV* result.
NOTE: the perl_ form of this function is deprecated.
SV* eval_pv(const char* p, I32 croak_on_error)
eval_sv
Tells Perl to eval the string in the SV.
NOTE: the perl_ form of this function is deprecated.
I32 eval_sv(SV* sv, I32 flags)
No,
but some kind soul already pointed me at perlguts and there I found a
void
scsi_cmd_in(fh, cmd, data_cnt)
PerlIO *fh
SV *cmd
int data_cnt
...
unsigned char *scsi_cmd;
STRLEN cmd_len;
...
scsi_cmd = SvPV(cmd, cmd_len);
Josef
Hm, than I misunderstood you, I guess.
regards,
Reinhard
Josef Moellers
2009-06-05 12:52:59 UTC
Permalink
Post by Reinhard Pagitsch
Hi,
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
Did you looked at the eval_* functions in perlapi.pod?
eval_pv
Tells Perl to eval the given string and return an SV* result.
NOTE: the perl_ form of this function is deprecated.
SV* eval_pv(const char* p, I32 croak_on_error)
eval_sv
Tells Perl to eval the string in the SV.
NOTE: the perl_ form of this function is deprecated.
I32 eval_sv(SV* sv, I32 flags)
No,

but some kind soul already pointed me at perlguts and there I found a
reference to SvPV() which does exactly what I want:

void
scsi_cmd_in(fh, cmd, data_cnt)
PerlIO *fh
SV *cmd
int data_cnt
PREINIT:
...
unsigned char *scsi_cmd;
STRLEN cmd_len;
PPCODE:
...
scsi_cmd = SvPV(cmd, cmd_len);

Josef
--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html
Loading...