__hook and __unhook usage in C + + event handling _c language

Source: Internet
Author: User

__hook
__hook Associates a handler method with an event.
Grammar

  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.

__unhook
cancels the association of the handler method with the event.
Grammar

   Long __unhook (
  &sourceclass::eventmethod,
  source,
  &receiverclass::handlermethod
  [, Receiver = this] 
);
Long __unhook ( 
  interface,
  source
);
Long __unhook (
  source 
);

Parameters
&sourceclass::eventmethod
A pointer to the event method from which the hook event handler method is lifted:
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 that will be lifted from the receiver only for 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 be lifted from the event. The handler will specify either a method for the class or a reference to the same method, or __unhook 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 __unhook 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 __unhook the association of the handler method with the event method or to engage in the piece method "Unbind" handler method.
There are three forms of __unhook. In most cases, the first form of (four-argument) can be used. You can only use the second (two-argument) Form of __unhook for COM event sinks, which will unlock the entire event interface. You can use the third (one-argument) Form to disassociate all delegates from the specified source.
A non-0 return value indicates that an error has occurred (the managed event throws an exception).
If __unhook is invoked on events and event handlers that have not been hooked, it will not work.
At compile time, the compiler verifies that the event exists and performs a parameter type check with the specified handler.
In addition to COM events, you can invoke __hook and __unhook outside of the event receiver.
As an alternative to using __unhook, you can use the-= operator.
For information about encoding managed events in the new syntax, see event (C + + component extensions).
Attention
A template class or struct cannot contain events.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.