能否通过函数地址从Lua脚本调用任何主机C/C++函数?

我编译了一个用C/C++编写的控制台主机程序(我没有源代码)。此主机程序支持lua脚本(可能是使用lua虚拟机)。主机程序加载lua库:

luaopen_base luaopen_table luaopen_string luaopen_math luaopen_debug

并允许重新加载所有lua脚本。

是否可以通过函数地址(从主机程序中的外部调试器获取)从lua脚本调用任何主机C/C++函数?

在这种情况下,是否可以从lua中加载任何C/C++编译的库并调用其函数?

另一个论坛上的人针对这个问题编写了以下代码:

// re = callClientFunction(addr, { args }, 'cdecl')
// re = callClientFunction(method, { obj, arg1 }, 'this')
// re = callClientFunction(0x08, { obj, arg1 }, 'this')   = obj->vtable[2]->(arg1)

inline int callCdeclFunction(lua::State* L, uintptr_t addr, const std::vector<lua::Integer>& args)
{
typedef lua::Integer __cdecl cf0();
typedef lua::Integer __cdecl cf1(lua::Integer);
typedef lua::Integer __cdecl cf2(lua::Integer, lua::Integer);
typedef lua::Integer __cdecl cf3(lua::Integer, lua::Integer, lua::Integer);
typedef lua::Integer __cdecl cf4(lua::Integer, lua::Integer, lua::Integer, lua::Integer);
typedef lua::Integer __cdecl cf5(lua::Integer, lua::Integer, lua::Integer, lua::Integer, lua::Integer);

lua::Integer re = 0;
switch(args.size())
{
case 0: re = reinterpret_cast<cf0*>(addr)(); break;
case 1: re = reinterpret_cast<cf1*>(addr)(args[0]); break;
case 2: re = reinterpret_cast<cf2*>(addr)(args[0], args[1]); break;
case 3: re = reinterpret_cast<cf3*>(addr)(args[0], args[1], args[2]); break;
case 4: re = reinterpret_cast<cf4*>(addr)(args[0], args[1], args[2], args[3]); break;
case 5: re = reinterpret_cast<cf5*>(addr)(args[0], args[1], args[2], args[3], args[4]); break;
default:
  luaL_error(L, "%s: too many args (max %d, provided %d).\n", __func__, 5, args.size());
}
return re;
}

有没有关于如何在编译的主机程序中使用它的想法?

点赞
用户754147
用户754147

在Lua中访问C/C ++函数需要通过某个API公开它们,Lua不能直接加载“常规”dll(或者.so如果你愿意)。相反,你需要一个中间库来公开C函数并使其在Lua环境中可用。

干杯

2014-02-14 10:35:30
用户16827
用户16827

使用 Lua 调用 C/C++ 函数的正确方法是编写接口代码以在 Lua 栈上交换数据。

但有扩展允许直接调用共享库中的函数(.dll.so)。

请查看 FFI 类库(http://luajit.org/ext_ffi.html)或使用 libffi 类库(http://www.sourceware.org/libffi/)的 Alien Lua(http://alien.luaforge.net/)。

2014-02-14 10:55:06