COMMUNITY

Send byte array from cpp to haxe

haxe-cpp

(Daniel) #1

Is there a way to send an array of bytes from native c/cpp to haxe and be able to acces them preferrably as an array?


#2

With CFFIPrime you can do alloc_buffer_len( 0 ), cast it to Array_obj<unsigned char>* (because it is), do setUnmanagedData, cast it to value and then return it. Treat it as an array on haxe side. If you want arrays of anything but bytes when you are out of luck, because it looks like it is impossible to do (there is an actual array in cffi api, but I’m pretty sure it boxes everything, because it’s supposed to be Array<Dynamic>).

Another way is to use native externs, make you function return cpp.Pointer and use NativeArray.setUnmanagedData. This way you can return arrays of ints, floats etc.

On hl you can just return hl.Bytes and wrap them in hl.BytesAccess.


(Daniel) #3

What headers should be included to use alloc_buffer_len, Array_obj and setUnmanagedData?


#4

alloc_buffer_len is standard cffi api, hx/CFFI.h or hx/CFFIPrime.h includes it. Array_obj is in Array.h, but you’ll need to include hxcpp.h before it.
That way you can actually “return” other arrays if you don’t mind passing a reference to array into ffi function.
It’s kinda hacky but it works.


(Kevin Leung) #5

What is the difference between setUnmanagedData and setManagedData?