1.問題或需求描述:
lua 調用 windows api 或 dll 庫
2.測試環境:
Lua 5.1.5
win7 32
3.解決方法或原理:
--引入 alien 庫
require("alien")
--加載庫文件
user32 = alien.load("user32.dll")
--函數聲明
user32.GetClassNameA:types("int", "long", "pointer", "int")
user32.EnumWindows:types("int","callback", "uint")
user32.MessageBoxA:types("int", "long", "string", "string", "uint")
user32.FindWindowA:types("long", "string", "string")
user32.PostMessageA:types("int", "long", "uint", "uint", "uint")
local function EnumWnd(hwnd, lParam)
local buff = alien.buffer(64)
user32.GetClassNameA(hwnd, buff, 63)
print(tostring(buff))
return 1
end
local EnumWndFunc = alien.callback(EnumWnd, "int", "long", "long")
result = user32.MessageBoxA(0, "演示:關閉計算器", "請求確認", 0x00000004 + 0x00000020 + 0x00000100)
print("用戶響應鍵:" .. result)
if result == 6 then
hwnd = user32.FindWindowA("CalcFrame", "計算器")
if hwnd ~= 0 then
user32.PostMessageA(hwnd, 0x0010, 0, 0) --WM_CLOSE
end
end
result = user32.MessageBoxA(0, "演示:枚舉所有窗口類名", "請求確認", 0x00000004 + 0x00000020 + 0x00000100)
print("用戶響應鍵:" .. result)
if result == 6 then
user32.EnumWindows(EnumWndFunc,0)
end