Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: embedding Perl into C for Windows

by syphilis (Chancellor)
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


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; /* 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[] = { "", "" }; 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 "" 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.)


Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1074546]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-02-19 17:02 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (293 votes). Check out past polls.