|
1 | 1 | ---
|
2 |
| -title: "Event Handling in COM" |
3 |
| -ms.date: "11/04/2016" |
| 2 | +title: "Event handling in COM" |
| 3 | +description: "Learn how to use the Microsoft C++ extensions for COM event handling." |
| 4 | +ms.date: 11/20/2020 |
4 | 5 | helpviewer_keywords: ["event handling [C++], COM", "event handling [C++], about event handling", "declaring events", "event handlers [C++], COM", "event handlers", "COM, events", "event receivers, in event handling", "event handling [C++]", "hooking events", "event receivers, name and signature matching", "event sources, in event handling", "declaring events, in COM", "declaring events, event handling in COM"]
|
5 |
| -ms.assetid: 6b4617d4-a58e-440c-a8a6-1ad1c715b2bb |
6 | 6 | ---
|
7 |
| -# Event Handling in COM |
| 7 | +# Event handling in COM |
8 | 8 |
|
9 |
| -In COM event handling, you set up an event source and event receiver using the [event_source](../windows/attributes/event-source.md) and [event_receiver](../windows/attributes/event-receiver.md) attributes, respectively, specifying `type`=`com`. These attributes inject the appropriate code for custom, dispatch, and dual interfaces to allow the classes to which they are applied to fire events and handle events through COM connection points. |
| 9 | +In COM event handling, you set up an event source and event receiver using the [`event_source`](../windows/attributes/event-source.md) and [`event_receiver`](../windows/attributes/event-receiver.md) attributes, respectively, specifying `type`=`com`. These attributes inject appropriate code for custom, dispatch, and dual interfaces. The injected code allows the attributed classes to fire events and handle events through COM connection points. |
10 | 10 |
|
11 |
| -## Declaring Events |
| 11 | +> [!NOTE] |
| 12 | +> Event attributes in native C++ are incompatible with Standard C++. They don't compile when you specify [`/permissive-`](../build/reference/permissive-standards-conformance.md) conformance mode. |
| 13 | +
|
| 14 | +## Declaring events |
12 | 15 |
|
13 |
| -In an event source class, use the [__event](../cpp/event.md) keyword on an interface declaration to declare that interface's methods as events. The events of that interface are fired when you call them as interface methods. Methods on event interfaces can have zero or more parameters (which should all be *in* parameters). The return type can be void or any integral type. |
| 16 | +In an event source class, use the [`__event`](../cpp/event.md) keyword on an interface declaration to declare that interface's methods as events. The events of that interface are fired when you call them as interface methods. Methods on event interfaces can have zero or more parameters (which should all be *in* parameters). The return type can be void or any integral type. |
14 | 17 |
|
15 |
| -## Defining Event Handlers |
| 18 | +## Defining event handlers |
16 | 19 |
|
17 |
| -In an event receiver class, you define event handlers, which are methods with signatures (return types, calling conventions, and arguments) that match the event that they will handle. For COM events, calling conventions do not have to match; see [Layout Dependent COM Events](#vcconeventhandlingincomanchorlayoutdependentcomevents) below for details. |
| 20 | +You define event handlers in an event receiver class. Event handlers are methods with signatures (return types, calling conventions, and arguments) that match the event that they'll handle. For COM events, calling conventions don't have to match. For more information, see [Layout-dependent COM events](#vcconeventhandlingincomanchorlayoutdependentcomevents) below. |
18 | 21 |
|
19 |
| -## Hooking Event Handlers to Events |
| 22 | +## Hooking event handlers to events |
20 | 23 |
|
21 |
| -Also in an event receiver class, you use the intrinsic function [__hook](../cpp/hook.md) to associate events with event handlers and [__unhook](../cpp/unhook.md) to dissociate events from event handlers. You can hook several events to an event handler, or several event handlers to an event. |
| 24 | +Also in an event receiver class, you use the intrinsic function [`__hook`](../cpp/hook.md) to associate events with event handlers and [`__unhook`](../cpp/unhook.md) to disassociate events from event handlers. You can hook several events to an event handler, or several event handlers to an event. |
22 | 25 |
|
23 | 26 | > [!NOTE]
|
24 | 27 | > Typically, there are two techniques to allow a COM event receiver to access event source interface definitions. The first, as shown below, is to share a common header file. The second is to use [#import](../preprocessor/hash-import-directive-cpp.md) with the `embedded_idl` import qualifier, so that the event source type library is written to the .tlh file with the attribute-generated code preserved.
|
25 | 28 |
|
26 |
| -## Firing Events |
| 29 | +## Firing events |
27 | 30 |
|
28 |
| -To fire an event, simply call a method in the interface declared with the **`__event`** keyword in the event source class. If handlers have been hooked to the event, the handlers will be called. |
| 31 | +To fire an event, call a method in the interface declared with the **`__event`** keyword in the event source class. If handlers have been hooked to the event, the handlers will be called. |
29 | 32 |
|
30 |
| -### COM Event Code |
| 33 | +### COM event code |
31 | 34 |
|
32 | 35 | The following example shows how to fire an event in a COM class. To compile and run the example, refer to the comments in the code.
|
33 | 36 |
|
@@ -138,13 +141,13 @@ MyHandler1 was called with value 123.
|
138 | 141 | MyHandler2 was called with value 123.
|
139 | 142 | ```
|
140 | 143 |
|
141 |
| -## <a name="vcconeventhandlingincomanchorlayoutdependentcomevents"></a> Layout Dependent COM Events |
| 144 | +## <a name="vcconeventhandlingincomanchorlayoutdependentcomevents"></a> Layout-dependent COM events |
142 | 145 |
|
143 |
| -Layout dependency is only an issue for COM programming. In native and managed event handling, the signatures (return type, calling convention, and arguments) of the handlers must match their events, but the handler names do not have to match their events. |
| 146 | +Layout dependency is only an issue for COM programming. In native and managed event handling, the signatures (return type, calling convention, and arguments) of the handlers must match their events, but the handler names don't have to match their events. |
144 | 147 |
|
145 |
| -However, in COM event handling, when you set the *layout_dependent* parameter of `event_receiver` to **`true`**, the name and signature matching is enforced. This means that the names and signatures of the handlers in the event receiver must exactly match the names and signatures of the events to which they are hooked. |
| 148 | +However, in COM event handling, when you set the *`layout_dependent`* parameter of `event_receiver` to **`true`**, the name and signature matching is enforced. The names and signatures of the handlers in the event receiver and in the hooked events must exactly match. |
146 | 149 |
|
147 |
| -When *layout_dependent* is set to **`false`**, the calling convention and storage class (virtual, static, and so on) can be mixed and matched between the firing event method and the hooking methods (its delegates). It is slightly more efficient to have *layout_dependent*=**`true`**. |
| 150 | +When *`layout_dependent`* is set to **`false`**, the calling convention and storage class (virtual, static, and so on) can be mixed and matched between the firing event method and the hooking methods (its delegates). It's slightly more efficient to have *`layout_dependent`*=**`true`**. |
148 | 151 |
|
149 | 152 | For example, suppose `IEventSource` is defined to have the following methods:
|
150 | 153 |
|
@@ -196,4 +199,4 @@ public:
|
196 | 199 |
|
197 | 200 | ## See also
|
198 | 201 |
|
199 |
| -[Event Handling](../cpp/event-handling.md) |
| 202 | +[Event handling](../cpp/event-handling.md) |
0 commit comments