假设我有这个C语言函数:
__declspec(dllexport) const char* GetStr()
{
static char buff[32]
// Fill the buffer with some string here
return buff;
}
还有这个简单的Lua模块:
local mymodule = {}
local ffi = require("ffi")
ffi.cdef[[
const char* GetStr();
]]
function mymodule.get_str()
return ffi.C.GetStr()
end
return mymodule
我该如何将从C函数返回的字符串作为Lua字符串获取:
local mymodule = require "mymodule"
print(mymodule.get_str())
cdata<const char *>: 0x00000001
,而不是字符串。根据文档,我应该使用ffi.string(buff [,len])
来获取实际字符串。但是当我这样做时,应用程序会崩溃。 - SLCffi.string
调用以及崩溃时的回溯等信息。最好也提供填充字符串的代码。 - Etan Reisner