Search code examples
windowsluadllexport

Cannot load lua dll module


I cannot load a lua module, which is a 32bit c++ dll. The lua module and the lua code is in the same folder.

in c++ code:

extern "C" __declspec(dllexport) int luaopen_luartmidi(lua_State *L) {
    ...
}

lua first line:

local luartmidi = require 'luartmidi'

When i run the lua code with vs code lua 32bit debugger i get error message:

Exception has occurred: [C]:-1: error loading module ... from file ...

The specified function could not be found

Dependency Walker says LUA.DLL is missing. When i copy lua.dll in the same folder i get error

c:Users/xyz/.vscode/extensions/actboy168.lua-debug-1.23.1/runtime/win32/lua53/lua.exe: multiple Lua VMs detected


Solution

  • This happens when you link with a lua interpreter statically and then try to load an external module that links against a Lua DLL (and expects a lua.dll or similarly named library to be present). You can't both have a statically compiled interpreter and one loaded from a DLL to be present in the same process (the "multiple Lua VMs detected" message is triggered by a check against this condition).

    You have three options:

    1. Compile everything statically (don't load any external modules)
    2. Compile everything dynamically (don't link the Lua interpreter statically)
    3. Use a proxy library that will forward API calls from external libraries to statically linked Lua interpreter (will require exporting Lua symbols and writing a proxy library). See http://lua-users.org/wiki/LuaProxyDllFour links for the code and details (it's for Lua 5.1, but you can tweak the script for Lua 5.3).