How to pass a pointer to LuaJIT ffi to be used as out argument?
Asked Answered
A

1

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);
Applaud answered 23/12, 2012 at 14:7 Comment(0)
E
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
Elagabalus answered 23/12, 2012 at 22:1 Comment(1)
Aha! You have to use [1] instead of a second *! Makes sense, but takes some getting used to.Applaud

© 2022 - 2024 — McMap. All rights reserved.