简体   繁体   English

如何从最新的lua 5.3调用c dll

[英]how to call c dll from latest lua 5.3

UPDATED: Problem solved. 更新:问题解决了。 The dll must not be statically linking to lua, otherwise it crashes with a multiple Lua VMs detected exception. dll不能静态链接到lua,否则会因多个Lua VM检测到异常而崩溃。 The code blow actually works fine, just leave it here in case someone got this problem too. 代码打击实际上工作正常,只是留在这里以防万一有人也遇到这个问题。

And wireshark uses lua5.2 because there's a "lua52.dll" in it's folder. wireshark使用lua5.2,因为它的文件夹中有一个“lua52.dll”。


I'm writing wireshark plugin, some algorithm in C is difficult to implement in Lua, so I try to use these algorithm through dll. 我正在编写wireshark插件,C中的一些算法难以在Lua中实现,所以我尝试通过dll使用这些算法。

Most examples online use the old version of Lua, which use luaL_register in the dll code. 大多数在线示例使用旧版本的Lua,它在dll代码中使用luaL_register The luaL_register is replaced by lua_newtable / luaL_setfuncs in newer version, but I didn't find any working example online. luaL_register被替换lua_newtable / luaL_setfuncs在较新的版本,但我没有在网上找到任何工作示例。

Here's what I tried : 这是我试过的:

#include <stdio.h>
#include <string.h>
#include "lua.hpp"

#include <windows.h>

extern "C" {
    static int add(lua_State* L)
    {
        MessageBox(0, "", "", 0);
        double op1 = luaL_checknumber(L,1);
        double op2 = luaL_checknumber(L,2);
        lua_pushnumber(L,op1 + op2);
        return 1;
    }

    static luaL_Reg mylibs[] = { 
        {"add", add},
        {0, 0} 
    }; 
    __declspec(dllexport)
    int luaopen_mylib(lua_State* L) 
    {
        lua_newtable(L);
        luaL_setfuncs(L, mylibs, 0);
        lua_setglobal(L, "mylib");

        return 1;
    }
}

and the lua code: 和lua代码:

require "mylib" -- <----------crashes
-- local mylib = package.loadlib("mylib.dll","luaopen_mylib");  

print (mylib) 
if(mylib)then
    --mylib();
else
    -- Error
end

local b=mylib.add(11,33);
print("sum:", b);

The lua code crashes at first line. lua代码在第一行崩溃。 How to fix it? 怎么解决?

Another question, how to verify which version of Lua is wireshark using? 另一个问题,如何验证哪个版本的Lua是wireshark使用的? Calling print(_VERSION) in wireshark's lua console, it shows nothing. 在wireshark的lua控制台中调用print(_VERSION) ,它什么也没显示。

当静态链接到lua.lib时发生崩溃,我想在lua.lib中已经有一个lua VM,所以使用动态链接,问题就消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM