lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I have a windows x64 executable with embedded lua interpreter and I want to load my own C module into it. There is no source code of this application; interpreter is 64bit and it's version is 5.1.5, statically compiled (there is no lua5.1.dll or any dlls at all). I have full access to the interpreter (ie able to call 'require(mymodule)').

Source code of dll:

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
int test_add1(lua_State *L) {
    double d = luaL_checknumber(L, 1);
    lua_pushnumber(L, d + 1);
    return 1;
}
static const struct luaL_reg testlib[] = {
    {"add1",   test_add1},
    {NULL, NULL}
};
extern "C"
int __declspec(dllexport) luaopen_test (lua_State *L) {
    luaL_register(L, "test", testlib);
    return 1;
}

How am I supposed to build the dll so it would be able to access lua state in application? When I build it against lua5.1.dll (x64) from lua-binaries, I need to put lua5.1.dll along with executable and my module. Module cannot access any variables from application lua state, lua_getglobal(L, "_G") (and other globals that are supposed to be there) always returns nil - it seems there are two lua states now: application one and new one.
I am building it like this:

gcc -O2 -c -o test.o test.cpp && gcc -O -shared -o test.dll test.o -L . -llua5.1

and then simply require it:

    require('test')

After requiring there seems to be some sort of conflict between embedded interpreter and lua5.1.dll - I lose all control and application hangs.

I think I need to tell the compiler that lua functions are defined in executable with the interpreter; is it possible with gcc? It should also be noted that executable exports no functions: dumpbin.exe /exports myapp.exe from visual studio returns nothing. I have tried to build and use proxy dll (http://lua-users.org/wiki/LuaProxyDll , all variants) and all of them failed on different stages.
LuaProxyDll assumes that we have application sources - we don't.
LuaProxyDllTwo needs a .def file - had to make it myself. GCC cannot build the dll because symbols were stripped from exe:

    Cannot export luaopen_string: symbol not defined
    Cannot export luaopen_table: symbol not defined

LuaProxyDllThree builds proxydll just fine, but GCC cannot build module dll against it:

    $ gcc -O2 -c -o test.o test.cpp && gcc -o test.dll test.o -L . -lproxy
    test.o:test.cpp:(.text+0xe): undefined reference to `luaL_checknumber'

LuaProxyDllFour was the closest to success, but I had to make some essential changes to proxydll source file - gcc doesn't support `naked` attribute and visual studio compiler doesn't like __asm (not supported on x64), so I had to replace inline assembler:

void __declspec(dllexport) luaD_growstack() { __asm { jmp s_funcs.luaD_growstack } };

became

void __declspec(dllexport) luaD_growstack() { s_funcs.luaD_growstack() };


Proxy and module build fine, but I get 'Invalid access to memory address.' error after requiring - definitely an issue in proxy.

Is there any possible solution to this problem?