# How to bind C Struct to HashLink

**URL:** https://community.haxe.org/t/how-to-bind-c-struct-to-hashlink/2200
**Category:** Haxe
**Tags:** haxe-hl
**Created:** [January 5, 2020, 3:29am UTC](https://community.haxe.org/t/how-to-bind-c-struct-to-hashlink/2200 "2020-01-05T03:29:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Slander](https://community.haxe.org/user_avatar/community.haxe.org/slander/32/1096_2.png) [@Slander](https://community.haxe.org/u/Slander)
#### Post date: [January 5, 2020, 3:29am UTC](https://community.haxe.org/t/how-to-bind-c-struct-to-hashlink/2200/1 "2020-01-05T03:29:24Z")

</div>

I’ll try and keep things simple.

I wish to writing bindings to an arbitrary C library. However, this library uses struct values. Example:

> typedef struct { int a; int b; } MyThing;  
> MyThing createThing();  
> void useThing(MyThing t);

What would be the best way to go about implementing these functions? I don’t need to access the struct’s properties in the Haxe context, I just want to pass it between functions provided by the library. Preferably, the outputted HL/C code would simply pass around the “MyThing” value, but I’m having difficulty finding the correct way to set it up that way using the current HashLink/C examples.

The difficult part is these functions are not using pointers, so some of the typical options (like `_ABSTRACT(MyThing)` and `hl.Abstract<"MyThing">`) do not seem appropriate. Of course, I could wrap the functions and use “malloc”/“free” to force them into using “MyThing” pointers, but it’d be messy and require a manual destroy function instead of using garbage collection.

I also considered `_OBJ(_I32 _I32)` (of course, adding the `hl_type*` to a custom struct), but it’s still a little annoying since I cannot simply pass the struct into the function; instead, I must reconstruct it using the properties from the special struct containing `hl_type*` every time the function is called. Plus, once again, I don’t know how to represent structs that contain other struct “values”, i.e: `typedef struct { MyThing one; MyThing two; } MyThingContainer;`

Reading the HashLink API, there appears to be functions that allocate memory for the garbage collection. Perhaps I could use a special “malloc” to store and “pointer-ize” MyThing? If there is, I’m having trouble figuring it out. Perhaps I need to make my own hl\_type instance?

Any guidance on the proper way to handle this scenario would be greatly appreciated!

---

<div class="post-metadata">

### Author: ![josuigoa](https://community.haxe.org/user_avatar/community.haxe.org/josuigoa/32/611_2.png) [@josuigoa](https://community.haxe.org/u/josuigoa)
#### Post date: [January 9, 2020, 8:42am UTC](https://community.haxe.org/t/how-to-bind-c-struct-to-hashlink/2200/2 "2020-01-09T08:42:25Z")

</div>

Hi!

I wrote a [little entry](https://community.haxe.org/t/simple-hl-extension/1294) about a sample binding HashLink/C I did for learning purposes

hope this helps

---

<div class="post-metadata">

### Author: ![Slander](https://community.haxe.org/user_avatar/community.haxe.org/slander/32/1096_2.png) [@Slander](https://community.haxe.org/u/Slander)
#### Post date: [January 9, 2020, 3:23pm UTC](https://community.haxe.org/t/how-to-bind-c-struct-to-hashlink/2200/3 "2020-01-09T15:23:07Z")

</div>

Thank you so much!

But, well, I guess there’s no way to expose a struct directly. I suppose with a garbage collected environment, it wouldn’t make sense. Oh well. :0

---

<div class="post-metadata">

### Author: ![R32](https://community.haxe.org/user_avatar/community.haxe.org/r32/32/2123_2.png) [@R32](https://community.haxe.org/u/R32)
#### Post date: [January 22, 2020, 5:49am UTC](https://community.haxe.org/t/how-to-bind-c-struct-to-hashlink/2200/4 "2020-01-22T05:49:06Z")

</div>

If I understand correctly, I guess you don’t need to pass `MyThing`, see [hl.uv.Loop.hx](https://github.com/HaxeFoundation/haxe/blob/development/std/hl/uv/Loop.hx) and [libs/uv/uv.c](https://github.com/HaxeFoundation/hashlink/blob/master/libs/uv/uv.c)

For allocating, you could use `hl_gc_alloc_noptr()` for simple `struct*`, And if have a field in the `struct*` is a pointer that also allocated by the GC, then use `hl_gc_alloc_raw()`

On the haxe side, if it is a simple `struct`, I think it should be able to be created by `hl.Bytes`:

```haxe
// This could be built automatically by haxe macro
abstract MyThing(hl.Bytes) {
    public var a(get, set) : Int ;
    public var b(get, set) : Int ;
    public inline function new() {
        this = new hl.Bytes(8);
    }
    inline function get_a() return this.getI32(0);
    inline function get_b() return this.getI32(4);
    inline function set_a(v:Int) {
        this.setI32(0, v);
        return v;
    }
    inline function set_b(v:Int) {
        this.setI32(4, v);
        return v;
    }
}

```
