Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 创建回调结构以传递到LuaJIT FFI_C++_Dll_Lua_Ffi_Luajit - Fatal编程技术网

C++ 创建回调结构以传递到LuaJIT FFI

C++ 创建回调结构以传递到LuaJIT FFI,c++,dll,lua,ffi,luajit,C++,Dll,Lua,Ffi,Luajit,所以首先我加载一个我需要的DLL local ffi = require("ffi") local theDLL = ffi.load("thisDLL") 在ffi cdef中,我有两种不同的结构 ffi.cdef [[ typedef struct StructSession StructSession; typedef struct { /* * begin_proj callback */ bool (__

所以首先我加载一个我需要的DLL

local ffi = require("ffi")
local theDLL = ffi.load("thisDLL")
在ffi cdef中,我有两种不同的结构

ffi.cdef [[
    typedef struct StructSession StructSession;
    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;
我在cdef中也有此功能

__declspec(dllexport) int __cdecl start_session(StructSession **session,
                                                           StructCallbacks *cb);
现在我想调用这个函数

print(theDLL.start_session(a,b))

变量a和b显然是占位符,问题是如何传递函数所需的结构?假设我们让StructSession正常工作,对LuaJIT中的函数进行回调,甚至StructCallbacks也可以实现吗?

创建
StructCallbacks
很容易;您可以使用
ffi.new
创建它,并为字段创建ffi回调(有关回调的信息,请参阅)

创建
StructSession
更为复杂,因为它是一种不透明类型,但与在C中的实现方式没有太大区别

下面是如何在C中创建一个:

StructSession* S = NULL;
start_session(*S, foo);
请注意,您没有直接分配
StructSession
。相反,您分配一个指向一个的指针,并让
start\u session
分配实际的结构

现在我们将其转换为LuaJIT代码:

local S = ffi.new("StructSession*")
lib.start_session(getPointer(S), foo) -- getPointer should take the pointer of S, but...
…FFI不提供任何获取对象指针的方法(这是有意的;它允许优化)

那么,我们如何获得指向
结构会话的指针呢?回想一下,数组可以转换为指针,我们可以通过FFI访问它们。因此,我们创建一个单槽指针数组,并将其传递给
start\u session

local S_slot = ffi.new("StructSession*[1]")
lib.start_session(S_slot, foo)
local S = S_slot[0]
现在有了一个
StructSession
对象