1

c/c++ で書かれたコンソール ホスト プログラムをコンパイルしました (ソースはありません)。ホスト プログラムは lua スクリプトをサポートしています (おそらく lua 仮想マシンを使用)。ホスト プログラムが lua ライブラリをロードする

luaopen_base luaopen_table luaopen_string luaopen_math luaopen_debug

すべての lua スクリプトのリロードを許可します。

lua スクリプトからホスト c/c++ 関数を関数アドレス (ホスト プログラムの外部デバッガーから取得) で呼び出すことはできますか?

この場合、lua から C/C++ コンパイル済みライブラリをロードしてその関数を呼び出すことは可能ですか?

他のフォーラムの 1 人の男性がこの質問に対してこのコードを書きました

// 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;
}

コンパイルされたホストプログラムでそれを使用する方法はありますか?

4

2 に答える 2

3

Lua から C/C++ 関数を呼び出す適切な方法は、インターフェイス コードを記述して Lua スタックでデータを交換することです。

ただし、共有ライブラリ (.dll または .so) 内の関数を直接呼び出すことができる拡張機能があります。

FFI ライブラリ ( http://luajit.org/ext_ffi.html ) またはlibffi ライブラリ( http://www.sourceware.org /libffi/ )

于 2014-02-14T10:55:06.940 に答える
1

Lua で C/C++ 関数にアクセスするには、特定の API を介してそれらを公開する必要があります。Lua は「通常の」dll (または .so の場合は .so) を直接ロードしません。代わりに、C 関数を使用できる Lua 環境に公開する中間ライブラリが必要です。

乾杯

于 2014-02-14T10:35:30.447 に答える