繁体   English   中英

是否可以通过函数地址从lua脚本调用任何主机c / c ++函数?

[英]is it possible to call any host c/c++ function from lua script by function address?

我已经编写了用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;
}

任何想法如何在编译的宿主程序中使用它?

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

但是,有一些扩展名可以直接调用共享库(.dll或.so)中的函数。

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

要在Lua中访问C / C ++函数,您必须通过特定的api将其公开,Lua不会直接加载“常规” dll(如果您使用的话,则为.so)。 相反,您必须具有一个中间库才能向Lua环境公开C函数可用。

干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM