当前位置 : 主页 > 网络编程 > lua >

如何将指针传递给LuaJIT ffi以用作out参数?

来源:互联网 收集:自由互联 发布时间:2021-06-23
假设有以下C代码: struct Foo { int dummy; }int tryToAllocateFoo(Foo ** dest); …如何在LuaJIT中进行以下操作? Foo * pFoo = NULL;tryToAllocateFoo(pFoo); local ffi = require 'ffi'ffi.cdef [[ struct Foo { int dummy; }; int t
假设有以下C代码:

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

…如何在LuaJIT中进行以下操作?

Foo * pFoo = NULL;
tryToAllocateFoo(&pFoo);
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
网友评论