PerlDiscuss - Perl Newsgroups and mailing lists said:
> I have a string in C which I need to pass to a Perl subroutine. Perl would
> process this string and return a number of strings back as an array. I'm
> not able to retrieve these strings from the perl stack from within my C
> program.
>
> The wrapper function looks like:
>
> static void GetMultipleMessages ()
> {
>    dSP;
>    ENTER;
>    SAVETMPS;
>    numOBRs = call_argv ("generateMultipleOBR", G_ARRAY, my_args);
>    SPAGAIN;
>
>    for (counter = 0; counter < numOBRs; counter++)
>    {
>       returnMsgs[counter] = POPs;

for various reasons, i've often found that i must separate the POPs into a
separate statement.

        SV * sv = POPs;

but since returnMsgs is a char*[], you are most certainly not doing what you
want to do.  POPs returns an SV*, *not* a char*!  this:

        returnMsgs[counter] = SvPV_nolen (sv);

will give you a pointer to a string that will live as long as the SV (it's
either the actual PV inside the SV or a stringified version of it, it's
magical).  if you need the strings to outlive this scope, e.g., so you can
return them for use in other parts of your C program, then you will need to
copy them, e.g.:

        returnMsgs[counter] = strdup (SvPV_nolen (sv));

and arrange for the calling code to free them.

>    }
>
>    PUTBACK;
>    FREETMPS;
>    LEAVE;
> }


-- 
muppet <scott at asofyet dot org>

Reply via email to