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".


In reply to Re: Embedding perl into a win32 console application by gmpassos
in thread Embedding perl into a win32 console application by smack

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.