Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Embedding perl into a win32 console application

by gmpassos (Priest)
on Oct 08, 2004 at 01:42 UTC ( [id://397499]=note: print w/replies, xml ) Need Help??


in reply to Embedding perl into a win32 console application

You should take a look at perlcall, perlapi and perlguts.

But since I was working on something similar for PLJava and PLDelphi, here's some snippets with some extra comments:

char* EmbedPerl_call_args( char* code , char* arg1 ) { // dTHX enables multiple Perl interpreters for the API functions: dTHX ; // Enables the call of sub from C (handles @_ and returned values): dSP ; char *tmp , *retval ; SV* ret ; // Enable the call of the function from multiple native threads: PERL_SET_CONTEXT(PL_curinterp) ; // enter and save tmp to enable call to perl function (handles @_ fo +r us): ENTER ; SAVETMPS ; // Enable the insertion of arguments to send to the sub call: PUSHMARK(SP) ; // add arg1 as a new SV in the arguments (@_): XPUSHs(sv_2mortal(newSVpv(arg1, strlen(arg1)))); PUTBACK ; // call the sub: call_pv(code , G_SCALAR); // refresh returneds to get returned value: SPAGAIN ; // get the SV returned (SCALAR context): ret = POPs ; // tmp will point to the internal char* of the SCALAR, so, // when we clean the scalar we clean tmp, and can't return it... tmp = SvPV(ret , len) ; // ... so, we copy tmp to retval to can return the char* after cle +an tmps: retval = malloc(sizeof(char) * (len+1)) ; Copy(tmp, retval, len, char); // add end of char*: retval[len] = '\0'; // clean tmps and return to the previous state: FREETMPS ; LEAVE ; // the returned value: return retval ; }
You also can take a look at the sources of LibZip, at ./myldr/ and ./myldr/zlib-src/main.c to see sources of an executable that do that and for a MakeFile.PL that compile automatically your app on Linux and Windows.

Enjoy!

Graciliano M. P.
"Creativity is the expression of the liberty".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found