2

I'm using the Lua52.exe binary that you can download from Lua's site. I want to extend it's functionality with a DLL that I write. So I wrote a DLL where I included the Lua source to my VS DLL project. That code is below. When I do the following in a lua file that I run through Lua52.exe I get the error "multiple Lua VM's detected". So Lua52.exe comes with lua52.dll so I assume it's dynamically linked and when it starts up it loads lua52.dll to get a lua VM started. When my DLL gets loaded I would suspect the lua_State that is passed in is from lua52.exe. What is the VM talk about? Do I HAVE to dynamically link against lua in my DLL to? Can I make my DLL NOT create a lua VM somehow? I mean I'm not doing it on my own so something in the lua source must be.

package.loadlib("LuaDLLTest.dll", "luaopen_msglib")()


#define DLL_EXPORT extern "C" __declspec(dllexport)

#include "lua.hpp"

#define PI (3.14159265358979323846)

static int miles_to_km(lua_State *L)
{
    double miles = luaL_checknumber(L, 1);
    double km = miles * 1.609;
    lua_pushnumber(L, km);
    return 1;   /* one result */
} /* end of miles_to_km */

static int circle_calcs(lua_State *L)
{
    double radius = luaL_checknumber(L, 1);
    double circumference = radius * 2 * PI;
    double area = PI * radius * radius;
    lua_pushnumber(L, circumference);
    lua_pushnumber(L, area);
    return 2;   /* one result */
} /* end of miles_to_km */

static const luaL_Reg testlib[] =
{
    { "miles_to_km", miles_to_km },
    { "circle_calcs", circle_calcs },
    { NULL, NULL }
};


/*
** Open msg library
*/
DLL_EXPORT int luaopen_msglib(lua_State *L)
{
    lua_newtable(L);
    luaL_setfuncs(L, testlib, 0);
    lua_setglobal(L, "Math");

    return 1;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user441521
  • 6,942
  • 23
  • 88
  • 160
  • 2
    lua binary modules must not link against lua themselves. On Windows this is more difficult than on other systems and I think you need to link against the **identical** lua dll of the lua binary that will be running your module. – Etan Reisner Feb 24 '15 at 16:33
  • So basically you are saying to do https://msdn.microsoft.com/en-us/library/ms810279.aspx ? I'm confused. Or do you mean do a dynamic link in my DLL project because that should load up the same lua52.dll that lua53.exe is using. – user441521 Feb 24 '15 at 16:35
  • I don't *think* so. Your module shouldn't be loading lua directly at all. Does [this](http://lua-users.org/wiki/BuildingModules) help? – Etan Reisner Feb 24 '15 at 16:39
  • OK, so yeah that's dynamic lib file, which I don't have. Not sure if I have to make that myself from source or what. – user441521 Feb 24 '15 at 16:43
  • The lua binary/etc. you installed should have had one. No? Or did you build that yourself also? – Etan Reisner Feb 24 '15 at 16:44
  • Not the .lib file no. It has a DLL but need a .lib to compile my DLL against. I got it working though with this (got a .lib file). It's interesting that the 1 VM inside the lua.dll is now shared between the exe and my DLL. ty – user441521 Feb 24 '15 at 17:10

1 Answers1

2

As long as lua52.exe and your DLL are linked against the same DLL, you should not see this error. This error may happen in two cases: (1) lua52.exe includes Lua interpreter (Lua VM) statically compiled and when your module loads VM from lua52.dll it's compiled against, you get "multiple Lua VMs detected" message. (2) lua52.exe is compiled against lua52.dll and your module is compiled against another dll that includes Lua VM. Both end up loaded (similar to the first case), which triggers the error.

First you need to figure out if lua52 is indeed linked against lua52.dll. You can simply check its size (the DLL is 180K and the exe is 14K on my Windows machine) or run something like depends to see the dependencies. If you confirm that it's linked against lua52.dll and your module is linked against the same DLL, I'd expect it to work. I tested on a similar library using the same command as you are using (package.loadlib("socket/core.dll", "luaopen_socket_core")()) and it's loaded without errors.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56