3.3.3 Connection Points: handling events
The connection points are part of a standard ActiveX
mechanism whose primary objective is to allow the ActiveX object
to notify its owner of any kind of events. The connection point
works as an ``event sink'', where events and notifications go
through.
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, cookie = luacom.Connect(calendar, calendar_events) if res == nil then exit(1) end -- 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).
Subsections
Next: 3.3.3.0.1 Message loop Up: 3.3 Automation binding Previous: 3.3.2.4 Property Access in Contents Fabio Mascarenhas de Queiroz 2005-01-07