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

In reply to How to pass a function reference from C to perl by Anonymous Monk

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.