RAD Studio
ContentsIndex
PreviousUpNext
Handling Events in an Automation Controller

When you generate a Component wrapper for an object whose type library you import, you can respond to events simply using the events that are added to the generated component. If you do not use a Component wrapper, however, (or if the server uses COM+ events), you must write the event sink code yourself.

Before you can handle events, you must define an event sink. This is a class that implements the event dispatch interface that is defined in the server's type library. 

To write the event sink, create an object that implements the event dispatch interface:

TServerEventsSink = class(TObject, _TheServerEvents)
...{ declare the methods of _TheServerEvents here }
end;

 

class MyEventSinkClass: TEventDispatcher<MyEventSinkClass, DIID_TheServerEvents>
{
...// declare the methods of DIID_TheServerEvents here
}

Once you have an instance of your event sink, you must inform the server object of its existence so that the server can call it. To do this, you call the global InterfaceConnect procedure, passing it

  • The interface to the server that generates events.
  • The GUID for the event interface that your event sink handles.
  • An IUnknown interface for your event sink.
  • A variable that receives a Longint that represents the connection between the server and your event sink.

{MyInterface is the server interface you got when you connected to the server }
InterfaceConnect(MyInterface, DIID_TheServerEvents,
                MyEventSinkObject as IUnknown, cookievar);

 

pInterface = CoServerClassName.CreateRemote("Machine1");
MyEventSinkClass ES;
ES.ConnectEvents(pInterface);

After calling InterfaceConnect, your event sink is connected and receives calls from the server when events occur. 

You must terminate the connection before you free your event sink. To do this, call the global InterfaceDisconnect procedure, passing it all the same parameters except for the interface to your event sink (and the final parameter is ingoing rather than outgoing):

InterfaceDisconnect(MyInterface, DIID_TheServerEvents, cookievar);

 

ES.DisconnectEvents(pInterface);

Note: You must be certain that the server has released its connection to your event sink before you free it. Because you don't know how the server responds to the disconnect notification initiated by InterfaceDisconnect, this may lead to a race condition if you free your event sink immediately after the call. The easiest way to guard against problems is to have your event sink maintain its own reference count that is not decremented until the server releases the event sink's interface.

Under COM+, servers use a special helper object to generate events rather than a set of special interfaces (IConnectionPointContainer and IConnectionPoint). Because of this, you can't use an event sink that descends from TEventDispatcher. TEventDispatcher is designed to work with those interfaces, not COM+ event objects. 

Instead of defining an event sink, your client application defines a subscriber object. Subscriber objects, like event sinks, provide the implementation of the event interface. They differ from event sinks in that they subscribe to a particular event object rather than connecting to a server's connection point. 

To define a subscriber object, use the COM Object wizard, selecting the event object's interface as the one you want to implement. The wizard generates an implementation unit with skeletal methods that you can fill in to create your event handlers.

Note: You may need to add the event object's interface to the registry using the wizard if it does not appear in the list of interfaces you can implement.
Once you create the subscriber object, you must subscribe to the event object's interface or to individual methods (events) on that interface. There are three types of subscriptions from which you can choose:
  • Transient subscriptions. Like traditional event sinks, transient subscriptions are tied to the lifetime of an object instance. When the subscriber object is freed, the subscription ends and COM+ no longer forwards events to it.
  • Persistent subscriptions. These are tied to the object class rather than a specific object instance. When the event occurs, COM locates or launches an instance of the subscriber object and calls its event handler. In-process objects (DLLs) use this type of subscription.
  • Per-user subscriptions. These subscriptions provide a more secure version of transient subscriptions. Both the subscriber object and the server object that fires events must be running under the same user account on the same machine.
Note: Objects that subscribe to COM+ events must be installed in a COM+ application.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!