Skip to content

Commit 067e0db

Browse files
authored
Merge pull request #3296 from corob-msft/docs/corob/cpp-docs-2609
Update to address 2609 event handling /permissive- compat
2 parents efcdfab + a70ccb1 commit 067e0db

File tree

10 files changed

+282
-253
lines changed

10 files changed

+282
-253
lines changed

docs/cpp/event-handling-in-com.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
---
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
45
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
66
---
7-
# Event Handling in COM
7+
# Event handling in COM
88

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.
1010

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
1215

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.
1417

15-
## Defining Event Handlers
18+
## Defining event handlers
1619

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.
1821

19-
## Hooking Event Handlers to Events
22+
## Hooking event handlers to events
2023

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.
2225

2326
> [!NOTE]
2427
> 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.
2528
26-
## Firing Events
29+
## Firing events
2730

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.
2932

30-
### COM Event Code
33+
### COM event code
3134

3235
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.
3336

@@ -138,13 +141,13 @@ MyHandler1 was called with value 123.
138141
MyHandler2 was called with value 123.
139142
```
140143

141-
## <a name="vcconeventhandlingincomanchorlayoutdependentcomevents"></a> Layout Dependent COM Events
144+
## <a name="vcconeventhandlingincomanchorlayoutdependentcomevents"></a> Layout-dependent COM events
142145

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.
144147

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.
146149

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`**.
148151

149152
For example, suppose `IEventSource` is defined to have the following methods:
150153

@@ -196,4 +199,4 @@ public:
196199
197200
## See also
198201
199-
[Event Handling](../cpp/event-handling.md)
202+
[Event handling](../cpp/event-handling.md)

docs/cpp/event-handling-in-native-cpp.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
---
2-
title: "Event Handling in Native C++"
3-
ms.date: "05/07/2019"
2+
title: "Event handling in native C++"
3+
description: "Learn how to use the Microsoft C++ extensions for native C++ event handling."
4+
ms.date: 11/20/2020
45
helpviewer_keywords: ["event handling [C++]"]
5-
ms.assetid: e4b9219a-15d8-42fb-83c8-6d2e4e087c8d
66
---
7-
# Event Handling in Native C++
7+
# Event handling in native C++
88

9-
In native C++ 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`=`native`. These attributes allow the classes to which they are applied to fire events and handle events in a native, non-COM context.
9+
In native C++ 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`=`native`. These attributes allow the classes they're applied on to fire events and handle events in a native, non-COM context.
1010

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.
1213
13-
In an event source class, use the [__event](../cpp/event.md) keyword on a method declaration to declare the method as an event. Make sure to declare the method, but do not define it; to do so will generate a compiler error, because the compiler defines the method implicitly when it is made into an event. Native events can be methods with zero or more parameters. The return type can be void or any integral type.
14+
## Declaring events
1415

15-
## Defining Event Handlers
16+
In an event source class, use the [`__event`](../cpp/event.md) keyword on a method declaration to declare the method as an event. Make sure to declare the method, but don't define it. If you do, it generates a compiler error, because the compiler defines the method implicitly when it's made into an event. Native events can be methods with zero or more parameters. The return type can be **`void`** or any integral type.
1617

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.
18+
## Defining event handlers
1819

19-
## Hooking Event Handlers to Events
20+
In an event receiver class, you define event handlers. Event handlers are methods with signatures (return types, calling conventions, and arguments) that match the event that they'll handle.
2021

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.
22+
## Hooking event handlers to events
2223

23-
## Firing Events
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.
2425

25-
To fire an event, simply call the method declared as an event in the event source class. If handlers have been hooked to the event, the handlers will be called.
26+
## Firing events
2627

27-
### Native C++ Event Code
28+
To fire an event, call the method declared as an event in the event source class. If handlers have been hooked to the event, the handlers will be called.
2829

29-
The following example shows how to fire an event in native C++. To compile and run the example, refer to the comments in the code.
30+
### Native C++ event code
31+
32+
The following example shows how to fire an event in native C++. To compile and run the example, refer to the comments in the code. To build the code in the Visual Studio IDE, verify that the **`/permissive-`** option is turned off.
3033

3134
## Example
3235

3336
### Code
3437

3538
```cpp
3639
// evh_native.cpp
40+
// compile by using: cl /EHsc /W3 evh_native.cpp
3741
#include <stdio.h>
3842

3943
[event_source(native)]
@@ -83,4 +87,4 @@ MyHandler1 was called with value 123.
8387

8488
## See also
8589

86-
[Event Handling](../cpp/event-handling.md)
90+
[Event handling](../cpp/event-handling.md)

docs/cpp/event-handling.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
---
2-
title: "Event Handling"
3-
ms.date: "05/07/2019"
2+
title: "Event handling"
3+
description: "Learn how Microsoft C++ extensions support both COM and native event handling."
4+
ms.date: 11/20/2020
45
helpviewer_keywords: ["event handling [C++]"]
5-
ms.assetid: 82de3f9a-2d88-470c-9527-8a5b54c8ced4
66
---
7-
# Event Handling
7+
# Event handling
88

9-
Event handling is primarily supported for COM classes (C++ classes that implement COM objects, typically using ATL classes or the [coclass](../windows/attributes/coclass.md) attribute). For more information, see [Event Handling in COM](../cpp/event-handling-in-com.md).
9+
Event handling is primarily supported for COM classes (C++ classes that implement COM objects, typically using ATL classes or the [coclass](../windows/attributes/coclass.md) attribute). For more information, see [Event handling in COM](../cpp/event-handling-in-com.md).
1010

11-
Event handling is also supported for native C++ classes (C++ classes that do not implement COM objects), however, that support is deprecated and will be removed in a future release. For more information, see [Event Handling in Native C++](../cpp/event-handling-in-native-cpp.md).
11+
Event handling is also supported for native C++ classes (C++ classes that don't implement COM objects). Native C++ event handling support is deprecated and will be removed in a future release. For more information, see [Event handling in native C++](../cpp/event-handling-in-native-cpp.md).
1212

13-
Event handling supports single- and multithreaded usage and protects data from simultaneous multithread access. It also allows you to derive subclasses from event source or receiver classes and support extended event sourcing/receiving in the derived class.
13+
> [!NOTE]
14+
> 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.
15+
16+
Event handling supports both single- and multithreaded usage. It protects data from simultaneous multithread access. You can derive subclasses from event source or receiver classes. These subclasses support extended event sourcing and receiving.
1417

1518
The Microsoft C++ compiler includes attributes and keywords for declaring events and event handlers. The event attributes and keywords can be used in CLR programs and in native C++ programs.
1619

17-
|Topic|Description|
18-
|-----------|-----------------|
19-
|[event_source](../windows/attributes/event-source.md)|Creates an event source.|
20-
|[event_receiver](../windows/attributes/event-receiver.md)|Creates an event receiver (sink).|
21-
|[__event](../cpp/event.md)|Declares an event.|
22-
|[__raise](../cpp/raise.md)|Emphasizes the call site of an event.|
23-
|[__hook](../cpp/hook.md)|Associates a handler method with an event.|
24-
|[__unhook](../cpp/unhook.md)|Dissociates a handler method from an event.|
20+
| Article | Description |
21+
|--|--|
22+
| [`event_source`](../windows/attributes/event-source.md) | Creates an event source. |
23+
| [`event_receiver`](../windows/attributes/event-receiver.md) | Creates an event receiver (sink). |
24+
| [`__event`](../cpp/event.md) | Declares an event. |
25+
| [`__raise`](../cpp/raise.md) | Emphasizes the call site of an event. |
26+
| [`__hook`](../cpp/hook.md) | Associates a handler method with an event. |
27+
| [`__unhook`](../cpp/unhook.md) | Disassociates a handler method from an event. |
2528

2629
## See also
2730

28-
[C++ Language Reference](../cpp/cpp-language-reference.md)<br/>
31+
[C++ language reference](../cpp/cpp-language-reference.md)\
2932
[Keywords](../cpp/keywords-cpp.md)

0 commit comments

Comments
 (0)