Download LuaCOM User Manual

Transcript
-- This should trigger the AfterUpdate event
-calendar:NextMonth()
The cookie returned by Connect identifies this connection, and can later be used to release the
Connection. A COM object can have several event sinks connected to it simultaneously.
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
-cookie = luacom.addConnection(calendar, event_handler)
-- This should trigger the AfterUpdate event
-calendar:NextMonth()
-- This disconnects the connection point established
-luacom.releaseConnection(calendar, event_handler, cookie)
-- This should NOT trigger the AfterUpdate event
-calendar:NextMonth()
Notice that addConnection also returns a cookie. A call to releaseConnection needs
both the event sink and the cookie to release the connection. The old (pre-1.3) syntax of releaseConnection
(ommiting the event sink and cookie) still works, but will only release the last connection made (but
there will not be leaks, all connections are released when the object is garbage-collected).
20