6

Assuming there is following C code:

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

...How to do following in LuaJIT?

Foo * pFoo = NULL;
tryToAllocateFoo(&pFoo);
prapin
  • 6,395
  • 5
  • 26
  • 44
Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160

1 Answers1

11
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
finnw
  • 47,861
  • 24
  • 143
  • 221