in reply to Re^9: Perl calls C calls Perl CallBack: How Perl callback use the same interpreter/context as Perl caller?
in thread Perl calls C calls Perl CallBack: How Perl callback use the same interpreter/context as Perl caller?

Hi,

The system works under CYGWIN.

It seemed to me that I should pass the Perl interpreter pointer from the Perl script itself to the C DLL. Can you please hint me about how to do it?


Many thanks,

Itamar

  • Comment on Re^10: Perl calls C calls Perl CallBack: How Perl callback use the same interpreter/context as Perl caller?

Replies are listed 'Best First'.
Re^11: Perl calls C calls Perl CallBack: How Perl callback use the same interpreter/context as Perl caller?
by salva (Canon) on Jul 08, 2014 at 15:18 UTC
    Sure, you start the argument declarations for your C function prototypes with pTHX/pTHX_, and then pass then as the first argument aTHX/aTHX_. For instance:
    /* Your C function declarations */ int foo(pTHX) { ... } int bar(pTHX_ int a) { ... } /* In your XS code */ int foo_result = foo(aTHX); int bar_result = bar(aTHX_ 21);

      Hi,

      Thanks for your responses!

      We use WIN32::API module to load the C DLL functions. I don't understand where should I use the code you have suggested. Please forgive me for trivial questions... I'm a FW guy, and in my first steps of learning the Perl guts.


      Thanks!

      Itamar

        You should be writing your C code as an XS file (perlxs). Perl (well, DynaLoader) will then take care of loading your extension, setting you up with the Perl interpreter and doing all the work.

        Most likely, perlxstut contains enough to get you started.

        As Corion says in his post, the common way to do that is to write an XS extension. That would probably make all your problems related to the Perl context go away.

        If you want to stick to Win32::API, that module provides Win32::API::Callback for passing Perl subs as callbacks to C functions.