LUACOM-IDL-VBS(二)
Object failed!");
end
end
function COM:UnRegister()
-- Fills table with registration information
local reginfo = {};
reginfo.VersionIndependentProgID = "Test.Test";
reginfo.ProgID = reginfo.VersionIndependentProgID .. ".1";
reginfo.TypeLib = "test.tlb";
reginfo.CoClass = "Test";
-- removes compnent information from the registry
local res = luacom.UnRegisterObject(reginfo);
if res == nil then
error ("UnRegisterObject failed!");
end
end
-- starts automation server
return luacom.DetectAutomation(COM);
--xCom = { luacom.DetectAutomation(COM); };
--for k,v in pairs(xCom) do
--print(k,v);
--end
--return unpack(xCom);
执行2个命名,一个是生成test.tlb,另一个是将COM注册到注册表中,执行完后,大家可到注册表中去看看它的注册内容
maketlb.bat
[
html]
@REM SET MIDLDir="c:\Program Files\Microsoft SDKs\Windows\v7.0A\bin"
SET MIDLDir="d:\Program Files\Microsoft SDKs\Windows\v6.0A\bin"
SET IDLFile="%loon%\..\lua\idl\test.idl"
SET TLBFile="%loon%\..\lua\idl\test.tlb"
@SET PATH="%loon%\..\lua\";%PATH%
chdir /d %MIDLDir%
midl.exe "%IDLFile%" /tlb "%TLBFile%" /out "%loon%\..\lua\"
chdir /d "%loon%\..\lua\idl"
lua testobj.lua /register
实现(纯Lua代码)和注册已经完成,下面就可以使用这个COM接口了。这里分别是用LuaCOM脚本和VBS脚本来调用它。
test.lua
[cpp]
package.cpath = os.getenv("loon") .. [[\..\lua\ .dll]]
require "luacom"
obj = {};
print("testlua.lua > luacom=" .. tostring(luacom));
x = luacom.CreateObject("TEST.Test");
print("testlua.lua > comobj=" .. tostring(x));
for k,v in pairs(x) do www.2cto.com
print(k,v);
end
print(x:Version(4,5));
test.vbs
[cpp]
ON ERROR RESUME NEXT
Set x = CreateObject("TEST.Test")
Dim a,b
a=4
b=5
x.Version a,b
MsgBox "Version: "+CStr(a)+"."+CStr(b), vbOK
可以看到输出结果 127和460,说明输进去的接收到了,修改后返回也正确接收到了。