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;
}