现在假设您有两个具有不同 lua 实例的程序。一个是主程序,第二个是你为它编写的dll。
在我的问题中,我将从现在开始将主程序命名为 main, dll i child。我们将子进程加载到主进程中,绕过它并以某种方式访问 lua_State。
我的主要问题是,我们可以在主程序运行时通过我们抓取的 lua_State 执行 lua_pcall 或 dofile 吗?
示例代码
主程序:
#include <lua.hpp>
bool loadFile(lua_State* L) {
// run the Lua script
luaL_dofile(L, "helloworld.lua");
if (lua_pcall(L, 0, 0, eh) != 0)
{
std::string err = luaL_checkstring(L, -1);
lua_pop(L, 1);
}
}
int main()
{
// create new Lua state
lua_State *lua_state;
lua_state = luaL_newstate();
loadFile(lua_state);
}
child 节目:#include <lua.hpp>
#include "hookingLibrary.h"
typedef int(__fastcall* main_loadFile_Proto)(lua_State* L);
main_loadFile_Proto main_loadFile_Ptr;
lua_State * L lastState;
uint64_t main_loadFile_Addr = 0x0;
int main_loadFile_Detour(lua_State* L) {
lastState = L;
return main_loadFile_Ptr(L);
}
int main()
{
// detouring etc.
// I do not put detouring codes here. I am just declaring it as an
// opinion.
HookingLibrary::hook((LPVOID)(uintptr_t)main_loadFile_Addr, &main_loadFile_Detour, (LPVOID*)&main_loadFile_Ptr);
do{
Sleep(100);
}while(!lastState);
// create new Lua state
lua_State *lua_state;
lua_state = lastState;
// run the Lua script
luaL_dofile(lua_state, "helloworld.lua");
// close the Lua state
lua_close(lua_state);
}
最佳答案
Now imagine you have two programs with different lua instances. One is the main program, the second is the dll you coded for it.
这个说法不是很清楚,要看你的预期。我看到了 2 个可能的答案。DLL的接口(interface)是这样的:
#ifndef DLL_AUGMENTED
#define DLL_AUGMENTED
#include "lua.h"
lua_State *DLL_CreateAugmentedLuaInterpreter ();
void DLL_FreeLuaInterpreter ();
#endif
并且可以被 main 使用:#include "lua-augmented.h"
int main (int argc, char **argv)
{
lua_State *LuaState = DLL_CreateAugmentedLuaInterpreter ();
// TODO: use the augmented Lua instance
DLL_FreeLuaInterpreter(LuaState);
return 0;
}
One is the main program, the second is the dll
所写。 .在这种情况下,它更难,因为 IPC
进程间通信需要用sockets
实现或 pipes
.最好找LuaSocket
图书馆。 Interprocess communication in Lua with Example?
关于c++ - 从另一个应用程序访问 lua_State,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64129234/