Friday, 15 January 2010

c - Creating a struct of callbacks in LuaJIT FFI -



c - Creating a struct of callbacks in LuaJIT FFI -

so first load in dll need

local ffi = require("ffi") local thedll = ffi.load("thisdll")

in ffi cdef have struct

ffi.cdef [[ typedef struct { /* * begin_proj callback */ bool (__cdecl *begin_proj)(char *proj); /* * save_proj_state */ bool (__cdecl *save_proj_state)(unsigned char **buffer, int *len); } structcallbacks;

i have function in cdef

__declspec(dllexport) int __cdecl start_session(structcallbacks *cb);

now phone call function

print(thedll.start_session(mycallbacks))

the question how can pass structs function needs (how create mycallbacks struct of callbacks lua functions)?

just create struct , assign fields lua functions, other value.

local callbacks = ffi.new("structcallbacks") callbacks.begin_proj = function(proj) homecoming false end callbacks.save_proj_state = function(buffer, len) homecoming true end

see ffi callback docs more in-depth info on callbacks.

c dll lua ffi luajit

No comments:

Post a Comment