Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: embedding Perl into C for Windows

by syphilis (Canon)
on Feb 11, 2014 at 23:55 UTC ( #1074546=note: print w/ replies, xml ) Need Help??


in reply to embedding Perl into C for Windows

Hi,

With the code you posted, I too get a crash on latest Strawberry Perl (5.18.2).
By inserting a couple of debug printf() statements, I found that it crashed when perl_parse() was executed. Same for you ?

However, there's a very similar example given in Strawberry Perl's perlembed (perldoc perlembed) documentation:

#include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; static void PerlPower(int a, int b) { dSP; /* initialize stack pointer */ ENTER; /* everything created after here */ SAVETMPS; /* ...is a temporary variable. */ PUSHMARK(SP); /* remember the stack pointer */ XPUSHs(sv_2mortal(newSViv(a))); /* push the base onto the stack */ XPUSHs(sv_2mortal(newSViv(b))); /* push the exponent onto stack */ PUTBACK; /* make local stack pointer global */ call_pv("expo", G_SCALAR); /* call the function */ SPAGAIN; /* refresh stack pointer */ /* pop the return value from stack */ printf ("%d to the %dth power is %d.\n", a, b, POPi); PUTBACK; FREETMPS; /* free that return value */ LEAVE; /* ...and the XPUSHed "mortal" args.*/ } int main (int argc, char **argv, char **env) { char *my_argv[] = { "", "power.pl" }; PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct( my_perl ); perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_run(my_perl); PerlPower(3, 4); /*** Compute 3 ** 4 ***/ perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }
With the file "power.pl" that contains:
sub expo { my ($a, $b) = @_; return $a ** $b; }
That example works fine, for me.
By working through the differences, you should be able to determine the reason(s) that one works, but the other doesn't.
(I'm assuming the version provided by the Strawberry docs works for you.)

Cheers,
Rob


Comment on Re: embedding Perl into C for Windows
Select or Download Code
Re^2: embedding Perl into C for Windows
by ccad (Novice) on Feb 12, 2014 at 07:24 UTC
    Hello Rob, this was the missing link:
    PERL_SYS_INIT3(&argc,&argv,&env); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; PERL_SYS_TERM();
    I added these three commands - now it works! Many, many thanks - great! Greeting from Germany Frank

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1074546]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (12)
As of 2014-09-22 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (192 votes), past polls