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