如何通过一个指针LuaJIT FFI用作out参数?(How to pass a pointer t

2019-08-04 14:09发布

假设有下列的C代码:

struct Foo { int dummy; }
int tryToAllocateFoo(Foo ** dest);

...如何做LuaJIT以下?

Foo * pFoo = NULL;
tryToAllocateFoo(&pFoo);

Answer 1:

local ffi = require 'ffi'

ffi.cdef [[
  struct Foo { int dummy; };
  int tryToAllocateFoo(Foo ** dest);
]]

local theDll = ffi.load(dllName)

local pFoo = ffi.new 'struct Foo *[1]'
local ok = theDll.tryToAllocateFoo(pFoo)

if ok == 0 then -- Assuming it returns 0 on success
  print('dummy ==', pFoo[0].dummy)
end


文章来源: How to pass a pointer to LuaJIT ffi to be used as out argument?
标签: lua ffi luajit