lua-users home
lua-l archive

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


> 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.
> 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'd be interested in a possible solution as well, as I ran into the
same issue with a similar setup. I also tried various combinations of
ProxyDLLs, but none of them worked for me (even though I successfully
used them with 32bit linked apps that export Lua symbols). I resorted
to providing my own Lua DLL that was modified to "link" some of the
internal structured with the ones from the main state. For example,
there are values like `luaO_nilobject` and `dummynode` that are
defined as address references and are instance-specific, so I
attempted to set them to the values from the main state in the loaded
dll, but it didn't fully resolve the issue as I'm still getting a
crash (when trying to load luasocket core.dll). I also made sure that
the DLLs is for the same sub-version of the interpreter that is
statically compiled.

It could be useful to have a set of defines that produce a DLLs that
can be used alongside with the statically compiled interpreter and,
when properly "synced", would be loaded from Lua modules and work
similar to how the proxy DLL works, but without forwarding of the
calls.

Paul.

On Sat, Sep 2, 2017 at 6:51 AM, unky <ohboyahat@gmail.com> wrote:
> 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?
>