Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Caml productivity.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-07-24 (15:00)
From: Michael Tucker <mtucker@e...>
Subject: Re: [Caml-list] CamlIDL and function pointers

 Thanks for the help. I have seen another approach mentioned, but have not
been able to get it to work. I thought I could rewrite my struct for the
IDL as:

typedef strcut funp {
   int i;
} funp_t;

But leave the C version intact. Supposedly it would just ignore the other
field when passing it through to Caml. If that's the case, I was thinking
it might be possible to write the following in the IDL:

void myfun ([in, unique] funp_t* f)
     " _res = f->funp_fun ();");

So, I guess I would like to be able to ignore a field when converting from
C to Caml (and vice versa), but would like to be able to access the field
from all code on the C side (even that generated by camlidl). I naively
tried adding the function pointer field to the structure in the generated
header file, so that my stub code would compile, but that didn't solve my

When I call myfun, I got a segmentation fault if I pass it a valid
structure. I also tried editing the custom call to check for NULL and
failwith a string, but the only change was that after calling with None it
would raise an exception -- any valid structure still segfaults. I assume
that is because I'm accessing an invalid field in the string, however a
simple C program including the same header file can access it without a
problem. Any ideas on what is going wrong or how I can fix it?


On Tue, 23 Jul 2002, Xavier Leroy wrote:

> >   How would one annotate the following C struct for camlidl:
> >
> > typedef struct funp {
> >    int i;
> >    void (*funp_fun) ();
> > } funp_t;
> >
> >   Is it possible? Looking at the specs I don't see how one can have a
> > field that is a pointer to a function. Any ideas, or pointers (no pun
> > intended) to examples?
> CamlIDL, like the DCE and Microsoft IDL from which it derives, doesn't
> support exchanging function pointers between C and Caml.  Your best
> bet is to declare funp_t as an abstract type in the IDL file:
>         typedef [abstract] struct funp * funp_t;
> and export C functions that do what you need to do on this funp_t
> type, e.g.
>         void invoke(funp_t arg);
> where "invoke" is defined directly in C somewhere else:
>         void invoke(funp_t arg) { arg->funp_fun(arg->i); }
> Of course, you won't be able to, say, construct a funp_t where the
> function part is actually a Caml function.  If you need that kind of
> callback mechanism, you'll have to write some of the stub code by hand,
> using Caml's callback functions.  Alternatively, if you can re-shape
> your struct funp till it looks like a COM interface, you could then use
> the CamlIDL "interface" declaration and get Caml->C and C->Caml calls.
> Hope this helps,
> - Xavier Leroy
> -------------------
> To unsubscribe, mail Archives:
> Bug reports: FAQ:
> Beginner's list:

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: