[Gambas-user] Calling external function by pointer

Tobias Boege taboege at ...626...
Thu May 8 18:41:13 CEST 2014


On Thu, 08 May 2014, Bruno F??lix Rezende Ribeiro wrote:
> Em Wed, 07 May 2014 21:36:04 +0200
> Beno??t Minisini <gambas at ...1...> escreveu:
> 
> > Le 07/05/2014 20:43, Bruno F??lix Rezende Ribeiro a ??crit :
> > > Em Wed, 07 May 2014 19:32:46 +0200
> > > Beno??t Minisini <gambas at ...1...> escreveu:
> > >
> > >> I'm afraid this is not possible, because the FFI library used by
> > >> Gambas must know the signature of the function before calling it.
> > >
> > > What about providing a way to declare the function signature for a
> > > pointer variable?
> > >
> > 
> > Why do you need that?
> 
> For example, the Xlib function 'XSetErrorHandler' accepts the new error
> handler as an argument and returns the old one.  I have a class called
> 'X11Error' which interfaces with the Xlib's error facilities.  Inside
> its '_new' method it registers within Xlib its private method
> 'ErrorHandler' as the error handler and stores the returned pointer to
> the default Xlib's error handler inside a private variable.  Then, to
> define a new error handler the user has to create a new object of class
> 'X11Error' and then catch the event 'Error' which will be raised by
> 'ErrorHandler'.  However, if there is no observer for such object the
> Xlib's default error handler is called instead.  Therefore I need the
> ability to call an anonymous function whose signature is known.  Do you
> know any other way?  Even if there is a specific way to solve this
> very problem within Xlib, I think Gambas module programmers would
> benefit of such improvement.
> 

Practically anything can be done in a C component. In this case, you can
create a function that accepts a function pointer and a Variant[] with the
values you want to pass and let the C function handle the rest.

In fact, I would recommend for your component to have at least a little C
backend to ease your life. I understand if you want to be able to do all the
things in pure Gambas but for that, new syntaxes must be created and I can't
imagine how those could fit into the existing Basic language elements...

To interface with C libraries, it's IMHO still best to write C components or
at least small C cores and build a complete component in Gambas on top of
them.

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list