To establish a connection using LuaCOM, the owner of the ActiveX object
must create a table to implement the connection interface,
whose description is provided by the ActiveX object (this interface
is called a source interface) and then call the API method
Connect, passing as arguments the LuaCOM object
for the ActiveX object and the implementation table. Doing this,
LuaCOM will automatically find the default source interface, create
a LuaCOM object implemented by the supplied table and then connect
this object to the ActiveX object. Here follows a sample:
-- Creates the COM object
--
calendar = luacom.CreateObject("MSCAL.Calendar")
if calendar == nil then
os.exit(1)
end
-- Creates implementation table
--
calendar_events = {}
function calendar_events:AfterUpdate()
print("Calendar updated!")
end
-- Connects object and table
--
res = luacom.Connect(calendar, calendar_events)
if res == nil then
exit(1)
end
-- This should trigger the AfterUpdate event
--
calendar:NextMonth()
It's also possible to separately create a LuaCOM object implementing the
connection point source interface and then connect it to the object using
AddConnection.
-- Instances the COM object
--
calendar = luacom.CreateObject("MSCAL.Calendar")
if calendar == nil then
print("Error instantiating calendar")
os.exit(1)
end
-- Creates implementation table
--
calendar_events = {}
function calendar_events:AfterUpdate()
print("Calendar updated!")
end
-- Creates LuaCOM object implemented by calendar_events
--
event_handler = luacom.ImplInterface(calendar_events,
"MSCAL.Calendar",
"DCalendarEvents")
if event_handler == nil then
print("Error implementing DCalendarEvents")
exit(1)
end
-- Connects both objects
--
luacom.addConnection(calendar, event_handler)
-- This should trigger the AfterUpdate event
--
calendar:NextMonth()
-- This disconnects the connection point established
--
luacom.releaseConnection(calendar)
-- This should NOT trigger the AfterUpdate event
--
calendar:NextMonth()