# C Binding non-void callback

**URL:** <https://forum.crystal-lang.org/t/c-binding-non-void-callback/2079>\
**Category:** Help & Support\
**Created:** [May 8, 2020, 6:59am UTC](https://forum.crystal-lang.org/t/c-binding-non-void-callback/2079 "2020-05-08T06:59:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![actor](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@actor](https://forum.crystal-lang.org/u/actor)\
**Post date:** [May 8, 2020, 6:59am UTC](https://forum.crystal-lang.org/t/c-binding-non-void-callback/2079/1 "2020-05-08T06:59:27Z")

</div>

Hi, I’ve been trying to create bindings to a C library ([IUP](http://webserver2.tecgraf.puc-rio.br/iup/)) but ran into trouble with callbacks, below is a sample of the C lib in Crystal

```crystal
type Ihandle = Ihandle_ # Ihandle_ is a struct
alias Icallback = (Ihandle*) -> LibC::Int
fun set_callback(ih : Ihandle*, name : LibC::Char*, func : Icallback) : Icallback

```

A snippet of the Crystal binding is written below (doesn’t work), some background info about the current code:

- `Ihandle` is an abstract class
- I’ve created `callback` so the shard user doesn’t have to use `LibIUP::SomethingHere`, I’m not sure if there is a better way to do this.

```crystal
class Button < Ihandle
  @@box : Pointer(Void)?
  @c_ptr : LibIUP::Ihandle* # created in initializers.

  # skipping initializers and extras

  def action(&block : Ihandle -> Int32)
    callback = ->(ih : LibIUP::Ihandle*){
      block.call(Button.new(ih))
    }
    # Mainly the code below here is where I'm uncertain.
    @@box = Box.box(callback)
    LibIUP.set_callback(@c_ptr, "ACTION", ->(ih){
      unpacked = Box(typeof(callback)).unbox(???)
      unpacked.call(ih)
    })
  end
end

```

I originally tried passing `callback` for `func` from which I found out you can’t [send closures to C functions](https://crystal-lang.org/reference/syntax_and_semantics/c_bindings/callbacks.html#passing-a-closure-to-a-c-function).

Any help would be appreciated, if more code samples are needed for context let me know.

---

<div class="post-metadata">

**Author:** ![jhass](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jhass/32/1779_2.png) [@jhass](https://forum.crystal-lang.org/u/jhass)\
**Post date:** [May 8, 2020, 7:36am UTC](https://forum.crystal-lang.org/t/c-binding-non-void-callback/2079/2 "2020-05-08T07:36:10Z")

</div>

Yes, you can only reasonably pass closure procs to a C API that allows you to pass an opaque pointer along. For example GObject provides [`g_signal_connect_data`](https://developer.gnome.org/gobject/stable/gobject-Signals.html#g-signal-connect-data) which invokes the callback with the `data` argument passed to the original call, alongside the regular arguments. With that you can do something like:

```auto
counter = 0
block = -> (object : GObject) do
  counter += 1
  puts "#{counter}: Got #{object}"
end
boxed_callback = Box.box(block)
# simplified, not the real call
LibGObject.signal_connect_data(object, "foo", boxed_callback, -> (object, data) { 
  Box(GObject ->).unbox(data).call(object) 
}

```

The link into the language reference you already provided also explains this.

With the lack of that you may have some luck in being able to have some global state to lookup the original callback from. Say a singleton class `CallbackRegistry` where you can register the callback in a way that lets you later identify it again, say maybe the object pointer you register the callback for and the event name, so you can have a generic callback handler like `->(ih) { CallbackRegistry.instance.lookup(ih, :button_pressed).call(ih) }`.

Of course for something like this you likely want to have a cleanup registry to not leak your callback handlers after they’re not needed anymore. That means to make it viable the library you’re binding to needs to have a hook that notifies you of the deregistration of the callback or the destruction/finalization of the object the callback was registered to, or something like that.

---

<div class="post-metadata">

**Author:** ![actor](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@actor](https://forum.crystal-lang.org/u/actor)\
**Post date:** [May 8, 2020, 12:14pm UTC](https://forum.crystal-lang.org/t/c-binding-non-void-callback/2079/3 "2020-05-08T12:14:14Z")

</div>

Thanks for the reply, it cleared up how it all works.

Unfortunately, the C callback doesn’t have a `data` parameter, nor does there seem to be any “hook” for when the object is destroyed that I can use.

I’ll try the projects mailing list in case there is something that I’m not seeing in the docs.

Thanks again.

---

<div class="post-metadata">

**Author:** ![mavu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/mavu/32/213_2.png) [@mavu](https://forum.crystal-lang.org/u/mavu)\
**Post date:** [May 8, 2020, 2:27pm UTC](https://forum.crystal-lang.org/t/c-binding-non-void-callback/2079/4 "2020-05-08T14:27:09Z")

</div>

I’m not sure I understood all that correctly, but you can check out this thread, where I invent a Rube-Goldberg machine to let an application talk to itself through a Unix-Socket.

(using C-lib that takes a callback but has no data-pointer)

> [@\[Help\] c binding of function that takes callback](https://forum.crystal-lang.org/t/help-c-binding-of-function-that-takes-callback/325):
>
> Hi tldr; how do I “interface” (call functions, etc) with the outside from inside a Proc passed as a callback to a c function? I am trying to use the wiringPi library in crystal on a raspberry pi and I have a problem with a function that requires a callback it has the following signature: extern int wiringPiISR (int pin, int mode, void (\*function)(void)) ; What works so far is this: module Wiringtest @[Link("wiringPi")] lib LibwiringPi fun wiringPiSetupSys : Int32 fun wiringPiISR…

Not sure if you want to go there, but maybe it can offer some ideas?

---

<div class="post-metadata">

**Author:** ![actor](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@actor](https://forum.crystal-lang.org/u/actor)\
**Post date:** [May 11, 2020, 6:50am UTC](https://forum.crystal-lang.org/t/c-binding-non-void-callback/2079/5 "2020-05-11T06:50:49Z")

</div>

Sorry for the delayed reply.

> I’m not sure I understood all that correctly

Was this because of my explanation? I can rewrite it if need be.

While your solution (using Unix sockets) might work, I’m not sure about using them as they are specific to Unix and Unix-like systems while IUP is also used on Windows. Although that’s not currently a problem (as Crystal doesn’t natively work on Windows), I can’t be certain that there is something similar on Windows.

Additionally, with how I see it being used, it’d still require some sort of notification from IUP that the callback isn’t needed so it can be freed from memory.

---

<div class="post-metadata">

**Author:** ![oprypin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/oprypin/32/859_2.png) [@oprypin](https://forum.crystal-lang.org/u/oprypin)\
**Post date:** [May 11, 2020, 7:33am UTC](https://forum.crystal-lang.org/t/c-binding-non-void-callback/2079/6 "2020-05-11T07:33:17Z")

</div>

Passing extra data is not a fixed pattern that has to be done through 2 callback arguments. The point is, having the thing you’re setting the callback on, you need any way to set any data associated with it, and then any way to get to that data, having only the callback arguments that the library provides (here just the Ihandle). It is very apparent here that in both parts you have access to the same Ihandle. So it is indeed possible to use a lookup hashtable (with it as the key) as @jhass suggests. But there may be better ways, that would require learning the library itself in detail. In a glance, I see it has “attributes” which may or may not be a way to store arbitrary data on an object.

---

<div class="post-metadata">

**Author:** ![actor](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@actor](https://forum.crystal-lang.org/u/actor)\
**Post date:** [May 13, 2020, 5:24am UTC](https://forum.crystal-lang.org/t/c-binding-non-void-callback/2079/7 "2020-05-13T05:24:21Z")

</div>

Thanks for explaining it some more. I think some of my problem was the uncertainty of how much/what Crystal code could be used in the C callback, and I thought that boxing _had_ to be used as in the overview.

> So it is indeed possible to use a lookup hashtable (with it as the key) as @jhass suggests.

I have since implemented this sort of solution. My concern with this was that I couldn’t find any way for the library to notify the binding that the callback (or to a larger extent, the object) isn’t needed anymore. I’ve since found that this is possible, so I don’t see any issues with this solution for now.

> But there may be better ways, that would require learning the library itself in detail. In a glance, I see it has “attributes” which may or may not be a way to store arbitrary data on an object.

Before starting this binding I didn’t have any experience with the library itself, so I am aware of the need to learn it, which I am doing as I go and in some free time. If I do find a better alternative I’ll swap to using that. As for using the “attributes” (if you’re curious) they are more-or-less properties for the controls/dialogs (e.g. title, size, visibility, etc.). You _can_ create one with any name you want, however, they only store and return primitive types. Whilst you might be able to store the pointer address for the proc or something, that doesn’t seem like a good idea when compared with a “notification” from the library that memory can be released (this particular one if specifically for language bindings).
