让 Lua 访问 COM 组件

  首先到下载页面下载 Lua 的扩展库 luacom,把其中的扩展库解压缩到 MUSHclient 的安装目录。然后用执行 loadlib ("luacom.dll","luaopen_luacom") () 语句载入一个名为 luacom 的对象,最后用 luacom 对象的 CreateObject  方法创建一个你想使用的 COM 组件的实例,就可以像平时一样使用这个实例的所有方法和属性了。


下面是一些例子

调用 SAPI 对象让电脑读出单词。

-- 载入 luacom 对象
assert (loadlib ("luacom.dll","luaopen_luacom")) ()

-- 创建一个 SAPI 语音对象的实例
talk = assert (luacom.CreateObject ("SAPI.SpVoice"), "无法创建 SAPI 对象")

-- 调用相应的方法读出一些单词
talk:Speak ("hi there")

 

使用微软的日历组件

-- 载入 luacom 对象
assert (loadlib ("luacom-lua5-1.3.dll","luacom_openlib")) ()

-- 创建一个微软的日历组件对象实例
calendar = luacom.CreateObject("MSCAL.Calendar")

-- 检查错误
if calendar == nil then
  error ("创建对象失败")
end

-- 调用日历对象的方法
calendar:AboutBox()

-- 设置属性
current_day = calendar.Day

-- 获取属性
calendar.Month = calendar.Month + 1

print(current_day)
print(calendar.Month)