我编写了c/C++编写的控制台主机程序(我没有源代码)。主机程序支持lua脚本(可能使用lua虚拟机)。主机程序加载Lua库是否可以通过函数地址从lua脚本中调用任何主机c/C++函数?
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;
}
任何想法如何在compilied主机程序使用它呢?
是否主机程序加载的Lua动态或者Lua的静态链接到它?在第一种情况下,你可以编写自己的“Lua”库并欺骗主机。 – lhf