Skip to content

How to pass struct as argument of function #114

Description

@nerzh

Hi, sorry for my english. I will show the problem with examples:

header.h

typedefstruct {
constchar*content;
uint32_tlen;
} tc_string_data_t;
typedefstructtc_string_handle_ttc_string_handle_t;
tc_string_handle_t*tc_create_context(tc_string_data_tconfig);
tc_string_data_ttc_read_string(consttc_string_handle_t*handle);
voidtc_destroy_string(consttc_string_handle_t*handle);

How its work in ruby with gem ffi

require"json"require"ffi"moduleBindingclassTcStringDataT < FFI::Structlayout:content,:pointer,:len,:uint32endendmoduleBindingextendFFI::Libraryffi_lib"/path_to_lib"defself.setup_bindingsattach_function:tc_create_context,[TcStringDataT.by_value],:pointerattach_function:tc_read_string,[:pointer],TcStringDataT.by_valueattach_function:tc_destroy_string,[:pointer],:voidendendconfig_json={network: {endpoints: ["http://some_url"]}}.to_json# make structdata_struct=Binding::TcStringDataT.newdata_struct[:content]=FFI::MemoryPointer.from_string(config_json)data_struct[:len]=config_json.bytesize# create contextstring_ref=Binding.tc_create_context(data_struct)# read context responseresult_data_struct=Binding.tc_read_string(string_ref)result_string=result_data_struct[:content].read_string(result_data_struct[:len])# destroy refBinding.tc_destroy_string(string_ref)

ffi gem works, but sometimes i get segmentation fault :(

So I decided to try to do it with fiddle !

But how ? How to do it with fiddle ? I climbed the entire Internet. I haven't found a single example of how to pass a struct to a function. There are only very few examples of how to pass ordinary primitives and not a single example of how to pass a structure. I have tried many options and still have not been able to achieve a result. Please help me !!! Write an example of calling these functions and passing arguments to them. Really looking forward to the answer.

Here is what I do with fidl:

require'fiddle'require'fiddle/import'require'json'moduleTestFiddleextendFiddle::ImporterextendFiddle::CParserdlload('/lib_client.dylib')TcStringDataT=struct['const char* content','uint len']# tc_string_handle_t* tc_create_context(tc_string_data_t config);# extern 'TcStringDataT* tc_create_context(TcStringDataT config)' ## unknown type TcStringDataTextern'TcStringDataT* tc_create_context(TcStringDataT* config)'## OK# tc_string_data_t tc_read_string(const tc_string_handle_t* handle);# extern 'TcStringDataT tc_read_string(const TcStringDataT* handle)' ## unknown type TcStringDataTextern'TcStringDataT* tc_read_string(const TcStringDataT* handle)'## OK# void tc_destroy_string(const tc_string_handle_t* handle)# extern 'void tc_destroy_string(const TcStringDataT handle)' ## unknown type TcStringDataTextern'void tc_destroy_string(const TcStringDataT* handle)'## OKendconfig_json={network: {endpoints: ["http://some_url"]}}.to_jsonstruct=TestFiddle::TcStringDataT.mallocstruct.content=Fiddle::Pointer.to_ptr(config_json)struct.len=config_json.bytesize# How to pass this struct ???ctx_ptr=TestFiddle.tc_create_context(struct)## ERROR ERROR ERROR, many many zeros and onesstring_ptr=TestFiddle.tc_read_string(ctx_ptr)struct=Test::TcStringDataT.new(string_ptr)pstruct.content.to_s

please help me with this 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions