How to invoke callback functions retrieved from external functions

I am struggling with delegates when retrieving them as function pointers from external functions.

E.g, if I call a declared function that returns a function pointer, I cannot declare it like this as this produces a syntax error:

declare function returnsFuncPtr lib "somelib" () as MyDelegate

Only this works:

declare function returnsFuncPtr lib "somelib" () as Ptr

But then I cannot seem to convert this into a delegate so that I could invoke it.

I understand that delegates need to be objects containing an object reference when they’re pointing to instance methods of objects. But when I’m using global functions as delegates, like in this case, a simple pointer should suffice, right?

Is there any way to solve this other than having to use a stub library that takes the function ptr as a Ptr and then invokes the function accordingly?

Pass the Ptr to Delegate.Constructor.

e.g.

declare function returnsFuncPtr lib "somelib" () as Ptr Delegate Function SomeFunc() As Integer Dim p As Ptr = returnsFuncPtr() Dim myDelegate As New SomeFunc(p)

Relevant (Win32) project

Yes! Thanks.

With that, I just successfully implemented a custom sqlite3 function that returns a value in Xojo code.

Whew!