gpt4 book ai didi

lua - 如何将指针传递给 LuaJIT ffi 用作输出参数?

转载 作者:行者123 更新时间:2023-12-04 14:03:03 24 4
gpt4 key购买 nike

假设有以下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

关于lua - 如何将指针传递给 LuaJIT ffi 用作输出参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14011598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com