2
votes

I'm trying to use a custom DLL from within Lua. I have a simple DLL, for example, like

extern "C"
{
  static int function_1(lua_State* L)
  {
    std::cout << "[DLL]this is a custom function" << std::endl;
    lua_pushnumber(L, 10);
    return 1;
  }

  __declspec(dllexport) int __cdecl luaopen_myDLL(lua_State* L)
  { 
    L = luaL_newstate();
    luaL_openlibs(L);
    std::cout << "[DLL] being initialized!" << std::endl;

    lua_register(L, "fun1", function_1);
    luaL_dofile(L, "./run.lua");
    return 1;
  }
} 

written in VS and built as dll.

After running within Lua

package.loadlib("./myDLL.dll", "luaopen_myDLL")() 

or

require("myDLL")

the DLL is loaded and runs like expected and also runs the specified run.lua that execute function_1 just fine.

The run.lua has nothing special in it, just something like

f = function_1()
print("[Lua] Function_1 says", f, "\n");

My current issues now are:

  1. I cannot run function_1() from the initial Lua script calling the DLL. Trying to do that I get

    attempt to call global 'function_1' (a nil value)

  2. i must use L = luaL_newstate(); inside my C code. For some reason, it doesn't work with the passed lua_State*, which I think is the reason why I cannot call the registered functions from the LUA script loading my DLL. Before running luaL_newstate() my lua_State has a valid address which doesn't change after the newstate.

I could theoretically run any Lua script from within my C library executing the registered functions, but this seems more like a dirty workaround to me.

My question now is if I'm missing something essential?

p.s.: I'm using Lua 5.1

1
Any Lua script will see your function as fun1 instead of function_1.Egor Skriptunoff
You should use passed lua_State* instead of creating new one with luaL_newstate. What error are you getting when using passed Lua state?Egor Skriptunoff
I used fun1() of course. Sorry i wrote that wrong in the question. ( As mentioned, fun1() works when i use it inside a lua script called by the C method with luaL_dofile(L, "run.lua"); ) When I try to execute my C code without a newstate i get a "Process finished with exit code -1073741819 (0xC0000005)", as soon i reach a part where i try to use the passed state.Senbazuru

1 Answers

2
votes

The code below should work. It may not work because of the following reasons:

  • your binary you use to run initial Lua script (that has require("myDLL") in it) has a different Lua version and/or does not use shared dll.
  • your Lua headers you use in your C++ code have different Lua version from original lua.exe
  • you link your project against different Lua version
  • you compile Lua again with your solution (you must use only headers and already provided .lib file with Lua distribution, if you want to use lua.exe)

To make your code available in Lua you must use Lua headers for proper Lua version and link against proper .lib file and use lua.exe that uses shared library (lua.dll, I guess).

static int function_1(lua_State* L)
{
    std::cout << "[DLL]this is a custom function" << std::endl;
    lua_pushnumber(L, 10);
    return 1;
}

extern "C" int __declspec(dllexport) luaopen_quik(lua_State *L) {
    std::cout << "[DLL] being initialized!" << std::endl;
    lua_register(L, "fun1", function_1);
    luaL_dofile(L, "./run.lua");
    return 0;
}

P. S. please provide your solution files so I can help further because it is not an issue with the code. -- it's the linkage issue.