日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Lua直接调用动态链接库(DLL或so文件)

發(fā)布時間:2025/3/20 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Lua直接调用动态链接库(DLL或so文件) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原網址:

http://my.oschina.net/hac2009/blog/14099?catalog=67125


使用Lua編程,擴展外部接口時,不一定要編寫lua專用的dll,使用系統(tǒng)的,或傳統(tǒng)的動態(tài)鏈接庫也可以,這樣更具有通用性。

基本用法:

require("alien") --1.加載alien
libc = alien.load("msvcrt.dll") -- 2.加載動態(tài)鏈接庫
libc.puts:types("void", "string") -- 3.說明參數(shù)類型
libc.puts:types{abi='cdecl',ret="void";"string"} --3.或者 說明函數(shù)調研類型,顯式申明返回值
libc.puts("kasdfajdskfas;dlfjkads") -- 調用

Alien 轉換Lua numbers 為 C的 numeric類型, 轉換nil為NULL, strings 為const char* , userdata 為 void* 指針. 而函數(shù)返回值的工作轉換正好相反 ( pointer類型轉換為userdata).

對于引用類型參數(shù), Alien 在堆棧中分配空間, Lua的變量傳值給函數(shù)參數(shù)(正確轉換), 調用函數(shù)時使用分配的空間地址調研。返回時通過lua的函數(shù)返回方式返回結果值,如:scanf
scanf = libc.scanf
scanf:types("int", "string", "ref int", "ref double")
_, x, y = scanf("%i %lf", 0, 0) -- 后面兩個參數(shù)其實沒有使用傳入的值
==》23 42.5

分配緩沖區(qū)(buffer):

?當調用的函數(shù)時,參數(shù)需要預先分配空間時,使用alien.buffer來分配空間

如果沒有指定參數(shù),分配平臺默認的參數(shù);如果指定參數(shù),根據(jù)參數(shù)數(shù)值分配空間。
如:
require("alien") --1.加載alien
libc = alien.load("msvcrt.dll") -- 2.加載動態(tài)鏈接庫
libc.gets:types("pointer", "string") -- 3.說明參數(shù)類型
libc.gets:types{abi='cdecl',ret="pointer";"string"} --3.或者 說明函數(shù)調研類型,顯式申明返回值
buf = alien.buffer(100)
libc.gets(buf) -- 調用
=buf:tostring() -- 轉換為字符串,
或者為:tostring(buf)
可以象C字符串一樣,通過buf的數(shù)組下標操作字符串單元,但是這里使用的是Lua的數(shù)組風格(下標從1開始,不是從0開始)
=string.char(buf[1])
也可以通過
buf:get(offset,type),或buf:set(offset,value,type)來讀取或更改數(shù)據(jù),如buf中有4個int數(shù)據(jù),可以這樣讀取或存儲:
i=buf:get(1,"int"),j=buf:get(5,"int"),k=buf:get(9,"int"),l=buf:get(13,"int")
注意:get或set沒有邊界檢查,請注意超出界限問題

使用數(shù)組
libc = alien.load("msvcrt.dll") -- 2.加載動態(tài)鏈接庫

function sort(a, b)
return a - b
end
compare = alien.callback(sort, "int", "ref int", "ref int")

qsort = libc.qsort
qsort:types("void", "pointer", "int", "int", "callback")

nums = alien.array("int", { 4, 5, 3, 2, 6, 1 })
qsort(nums.buffer, nums.length, nums.size, compare)
for i, v in nums:ipairs() do print(v) end
--可以直接使用數(shù)組下標操作
=nums[1]
=nums[2]
=nums[3]
=nums[4]
=nums[5]
=nums[6]

指針解包

alien.tostring -- 把char* 轉換成LUA的string
alien.toint -- 把int* 轉換成Lua的numeric
alien.toshort, alien.tolong, alien.tofloat, and alien.todouble與toint類似
例如:
> fs = alien.tofloat(ptr, 4)
> =#fs
4
>
標簽

把userdata與metatable關聯(lián)起來,以便使用lua的垃圾回收器

alien.tag(*tagname*) -- 如果沒有,創(chuàng)建metatable的標簽,如果有則返回
alien.wrap(*tagname*, ...) -- 創(chuàng)建完整的userdata,并與metatable關聯(lián)起來,命名標簽,并且根據(jù)后面的參數(shù)賦值。
alien.unwrap(*tagname*, obj) -- 檢測標簽的對象,如果沒有就拋出錯誤,否則返回對象


alien.rewrap(*tagname*, obj, ...) -- 更新對象值

例如:
local tag_foo = alien.tag("libfoo_foo")
alien.foo.create_foo:types("pointer")
alien.foo.destroy_foo_types("void", "pointer")?

function new_foo()
local foo = alien.foo.create_foo()
return alien.wrap("libfoo_foo", foo)
end

tag_foo = {
__gc = function (obj)
local foo = alien.unwrap("libfoo_foo", obj)
alien.foo.destroy_foo(foo)
end
}

回調函數(shù)

在動態(tài)鏈接庫中回調LUA函數(shù)
local function cmp(a, b)
return a - b
end
local cmp_cb = alien.callback(sort, "int", "ref char", "ref char")

其他


alien.platform -- 檢測操作系統(tǒng)
alien.sizeof(*typename*) -- 類型長度
alien.align(*typename*)
alien.table(narray, nhash) -- 創(chuàng)建環(huán)隊列
local qsort = alien.default.qsort
qsort:types("void", "pointer", "int", "int", "callback")

local chars = alien.buffer("spam, spam, and spam")
qsort(chars, chars:len(), alien.sizeof("char"), cmp_cb)
assert(chars:tostring() == " ,,aaaadmmmnpppsss")

總結

以上是生活随笔為你收集整理的Lua直接调用动态链接库(DLL或so文件)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。