在Lua5.1中使用自定义DLL
我正在尝试使用Lua中的自定义DLL。我有一个简单的DLL,例如在Lua5.1中使用自定义DLL,dll,lua,Dll,Lua,我正在尝试使用Lua中的自定义DLL。我有一个简单的DLL,例如 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(dllexpo
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;
}
}
或
DLL已加载并按预期运行,同时还运行指定的run.lua,该run.lua可以很好地执行函数_1
lua没有什么特别之处,就像
f = function_1()
print("[Lua] Function_1 says", f, "\n");
我现在的问题是:
尝试调用全局“function_1”(一个nil值)
L=luaL_newstate()代码>在我的C代码中。由于某些原因,它不能与传递的lua_State*一起工作,我认为这就是为什么我不能从加载DLL的lua脚本调用已注册函数的原因。在运行luaL_newstate()之前,我的lua_状态有一个有效的地址,该地址在newstate之后不会更改
p、 美国:我正在使用Lua5.1,下面的代码应该可以工作。由于以下原因,可能不起作用:
- 用于运行初始Lua脚本(其中包含
)的二进制文件具有不同的Lua版本和/或不使用共享dllrequire(“myDLL”)
- 您在C++代码中使用的Lua头与原始代码< Lua.exe < /C> < /LI>有不同的Lua版本。
- 将项目链接到不同的Lua版本
- 使用解决方案再次编译Lua(如果要使用
,则必须仅使用头文件,并且已经提供了带有Lua发行版的.lib文件)Lua.exe
static int function_1(lua_State*L)
{
std::cout任何Lua脚本都会将您的函数视为fun1
而不是function\u 1
。您应该使用passedLua\u State*
而不是使用luaL\u newstate
创建新函数。使用passed Lua State时会出现什么错误?我当然使用了fun1()。很抱歉,我在问题中写错了。(如前所述,fun1()在C方法使用luaL_dofile(L,“run.lua”);)调用的lua脚本中使用时起作用)当我尝试在没有newstate的情况下执行C代码时,我得到一个“进程完成,退出代码为-1073741819(0xC000005)”,当我到达尝试使用已传递状态的部分时。非常感谢。我确实在创建DLL的VS解决方案中链接到了不同的LUA。非常感谢。
require("myDLL")
f = function_1()
print("[Lua] Function_1 says", f, "\n");
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;
}