Accessing lua_State from another application

485 views Asked by At

Now imagine you have two programs with different lua instances. One is the main program, the second is the dll you coded for it.

In my question, I will name the main program as main, dll i child from now on. We load the child into the Main process, detouring it and somehow accessing lua_State.

My main question is, can we do lua_pcall or dofile via the lua_State we grab while the main program is running?

Sample code

Main program:

#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 program:

#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);
}
1

There are 1 answers

3
Robert On
Now imagine you have two programs with different lua instances. One is the main program, the second is the dll you coded for it.

This statement is not very clear, it depends on your expectations. I see 2 possible answers.

  1. Create a DLL which implement some additional functions for Lua. The DLL library can be used later by the main program. In this case, there is only 1 instance of lua_state, there is only 1 Lua interpreter. This Lua interpreter may be created by the DLL or by the main function.

The interface of DLL is something like that:

#ifndef DLL_AUGMENTED
#define DLL_AUGMENTED

#include "lua.h"

lua_State *DLL_CreateAugmentedLuaInterpreter ();
void       DLL_FreeLuaInterpreter ();

#endif

And can be used by 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;
}
  1. There is a need to have 2 Lua instances, as written One is the main program, the second is the dll. In this case, it's more difficult because a IPC Interprocess Communication need to be implemented with sockets or pipes. The best is to look for LuaSocket library.

Interprocess communication in Lua with Example?