RAD Studio
|
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
{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);
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.
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|