在Lua中调用图书馆 [英] Calling Library in Lua

查看:119
本文介绍了在Lua中调用图书馆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Lua中创建了Wireshark解剖器,用于TCP上的应用程序.我正在尝试使用zlib压缩和base64解密.我该如何在Lua中实际创建或调用现有的c库?

I have created a Wireshark dissector in Lua for an application over TCP. I am attempting to use zlib compression and base64 decryption. How do I actually create or call an existing c library in Lua?

我所看到的文档只是说您可以获取库并使用require()调用或luaopen_调用,但不能实际使程序查找和识别实际的库.所有这些都是在Windows中完成的.

The documentation I have seen just says that you can get the libraries and use either the require() call or the luaopen_ call, but not how to actually make the program find and recognize the actual library. All of this is being done in Windows.

推荐答案

不能使用普通Lua加载任何现有的C库,该库不是为Lua创建的.至少这不是小事.

You can't load any existing C library, which was not created for Lua, with plain Lua. It's not trivial at least.

*.so/*.dll必须遵循一些特定的标准,在Lua#26.2中的编程中直白地提到了这一点. lua用户Wiki 此处也回答了类似的问题.

*.so/*.dll must follow some specific standard, which is bluntly mentioned in programming in Lua#26.2 and lua-users wiki, code sample. Also similar question answered here.

有两种方法可以解决您的问题:

There are two ways You could solve Your problem:

  1. 遵循这些标准编写自己的Lua zlib库包装器.
  2. 采取一些已经完成的解决方案:
  1. Writing Your own Lua zlib library wrapper, following those standards.
  2. Taking some already finished solution:
    • zlib@luapower
    • lua-zlib

更大的列表 @lua用户Wiki

这同样适用于base64编码/解码.唯一的区别是,已经有了普通的Lua库.代码示例和几个链接 @ lua-users Wiki .

The same applies to base64 encoding/decoding. Only difference, there are already plain-Lua libraries for that. Code samples and couple of links @lua-users wiki.

注意:Lua模块程序包管理器,例如 LuaRocks LuaDist MIGHT 为您节省大量时间.

NOTE: Lua module package managers like LuaRocks or LuaDist MIGHT save You plenty of time.

此外,简单地加载Lua模块通常包含一行:

Also, simply loading a Lua module usually consists of one line:

local zlib = require("zlib")

将在您的Lua解释器的 luaconf.h中定义的位置中搜索该模块. 文件.

The module would be searched in places defined in Your Lua interpreter's luaconf.h file.

对于5.1,它是:

#if defined(_WIN32)
/*
** In Windows, any exclamation mark ('!') in the path is replaced by the
** path of the directory of the executable file of the current process.
*/
#define LUA_LDIR        "!\\lua\\"
#define LUA_CDIR        "!\\"
#define LUA_PATH_DEFAULT  \
            ".\\?.lua;"  LUA_LDIR"?.lua;"  LUA_LDIR"?\\init.lua;" \
                         LUA_CDIR"?.lua;"  LUA_CDIR"?\\init.lua"
#define LUA_CPATH_DEFAULT \
    ".\\?.dll;"  LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"

#else

这篇关于在Lua中调用图书馆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆