So I wrote a little .dll with C++, in Visual Studio 2017. All it does is export a function which I call from a lua script. The lua script runs in an third party application; I want to extend its functionality with this library. I have to use package.loadlib because require is modified and only allows to load .lua files. (I confirmed that it does find .dll files, so its nothing with the package pathing)
#include "lua.hpp"
extern "C" __declspec(dllexport) int test(lua_State* L) {
// luaL_checknumber(L, 1); // access violation
// lua_pushnumber(L, 123); // freeze
return 1;
}
I load this function using this code:
test, err = package.loadlib(dllpath, "test")
print("test = ", test, "err = ", err)
print("test() = ", test())
print("done")
that prints
test = function: 001D168B err = nil
test() = function: 001D168B
done
as expected...
But when I make the library call either lua function the application either permanently freezes or gets an access violation. lua_push* functions cause these freezes, while luaL_check* functions cause access violations.
I use Windows 10 (x64), the application is 32 bit, my .dll is compiled as 32 bit and both have the exact same lua version.
What am I doing wrong?
EDIT:
I was indeed linking wrong. I linked against lua5.3.lib when it should have been lua5.3-static.lib. I did not know that. It is working now.