Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear All, I wonder if any of you can help me, I am new to XS and want to call a C function from perl. It is actually a pointer to a function which is a member of a structure. A part of the C header file is as follows:
typedef struct _mystruct1 { int val; int (* init) (void *object1, void **object2); } mystruct1; typedef struct _mystruct2 { mystruct *ptr; } mystruct2;
###############################################
In the C code I have:
mystruct2 *mine; int res; struct obj *object1, **object2; ..... mine->ptr->val = 100; res = mine->ptr->init(object1, object2);
###############################################
and I want to be able to say the same from perl, i.e.:
print "$mine->{"ptr"}->{"val"}"; # This seems to work $mine->{"ptr"}->{"init"}(object1, object2); # This does not work
###############################################
To do that I used an XS program which has the following:
rh = (HV *)sv_2mortal((SV *)newHV()); hv_store(rh, "val", 3, newSViv(mine->ptr->val), 0); hv_store(rh, "init", 4, newRV((SV *)mine->ptr->init), 0); # This line +did not work
###############################################
I have tried to find out the type of the return pointer as in:
if (SvROK ((SV *) mine->ptr->init)) { type = SvTYPE(SvRV((SV *)mine->ptr->init)); printf ("Something ..%d", type); }
but both statements failed ...

Any help appreciated ...

Maged Messeh

Replies are listed 'Best First'.
Re: How to pass a function reference from C to perl
by Matts (Deacon) on May 21, 2002 at 19:20 UTC
    You can't do that. Perl has different calling conventions to C. You can only call a C function from within C-space. So you'll have to have some sort of XS function that calls your init function.

    See the XS Cookbook which may have an example of storing and calling a function pointer.

      This in fact what I am trying to do (write an XS function).. but no success so far
Re: How to pass a function reference from C to perl
by rsteinke (Scribe) on May 21, 2002 at 22:18 UTC
    What you probably want to do is convert the function pointer to an integer, and pass it as an IV. This is how perl handles pointers to blessed C objects created in XSUBs, so it should also work for function pointers. When perl calls the C code, convert the IV back to a function pointer and do the call. Alternately, you can pass mystruct1 as a blessed object. There's a neat tutorial on how to do this here.