Long __hook (
&sourceclass::eventmethod,
source,
&receiverclass::handlermethod
[, Receiver = this]
);
Long __hook (
interface,
source
);
Parameters
&sourceclass::eventmethod
A pointer to the event method to hook the event handler method to:
Native C + + events: Sourceclass is an event source class, Eventmethod is an event.
COM Event: Sourceclass is an event source interface, Eventmethod is one of its methods.
Managed Event: Sourceclass is an event source class, Eventmethod is an event.
Interface
The name of the interface to hook to receiver, which applies only to COM event sinks, where the layout_dependent parameter of the event_receiver attribute is true.
Source
A pointer to an instance of the event source. Depending on the code specified in event_receiver, Type,source can be one of the following:
The native event source object pointer.
Pointer based on IUnknown (COM source).
Managed object pointers (for managed events).
&receiverclass::handlermethod
A pointer to the event handler method to hook to the event. The handler will specify either a method for the class or a reference to the same method, or __hook assume that the class is the class from which it is invoked if you do not specify a class name.
Native C + + events: Receiverclass is the event sink class, Handlermethod is the handler.
COM Event: Receiverclass is an event sink interface, Handlermethod is one of its handlers.
Managed Event: Receiverclass is an event receiver class, Handlermethod is a handler.
Receiver (optional)
A pointer to an instance of the event sink class. If you do not specify a sink, the default is the sink class or structure in which the __hook is invoked.
Usage
Can be used in any function scope (including main) outside of the event receiver class.
Note
Use the internal function in the event receiver to associate or hook the handler method with the event method (__hook). The specified event handler is then invoked when the source throws the specified event. You can hook multiple handlers to a single event, or hook multiple events to a single handler.
There are two forms of __hook. In most cases, you can use the first (four-parameter) form, specifically, a COM event receiver for which the layout_dependent parameter of the event_receiver attribute is false.
In these cases, you do not need to hook all the methods in the interface until the event is fired on one of the methods, just the method that handles the event. You can only use the second (two-parameter) Form of __hook for Layout_dependent=true COM event sinks.
__hook will return a long value. A non-0 return value indicates an error occurred (the managed event throws an exception).
The compiler checks to see if an event exists and whether the event signature is consistent with the delegate signature.
In addition to COM events, you can invoke __hook and __unhook outside of the event receiver.
An alternative to using __hook is to use the + = operator.
Attention
A template class or struct cannot contain events.