Skip to content

Commit 2a5bb07

Browse files
authored
telegraphing the unsafeness (#3806)
1 parent 36b72d1 commit 2a5bb07

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

uwp/cpp-and-winrt-apis/interop-winrt-abi.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: This topic shows how to convert between application binary interface (ABI) and C++/WinRT objects.
33
title: Interop between C++/WinRT and the ABI
4-
ms.date: 11/30/2018
4+
ms.date: 09/29/2023
55
ms.topic: article
66
keywords: windows 10, uwp, standard, c++, cpp, winrt, projection, port, migrate, interop, ABI
77
ms.localizationpriority: medium
@@ -13,6 +13,9 @@ This topic shows how to convert between SDK application binary interface (ABI) a
1313

1414
In general, C++/WinRT exposes ABI types as **void\***, so that you don't need to include platform header files.
1515

16+
> [!NOTE]
17+
> In the code examples, we use `reinterpret_cast` (rather than `static_cast`) in an effort to *telegraph* what are inherently unsafe casts.
18+
1619
## What is the Windows Runtime ABI, and what are ABI types?
1720
A Windows Runtime class (runtime class) is really an abstraction. This abstraction defines a binary interface (the Application Binary Interface, or ABI) that allows various programming languages to interact with an object. Regardless of programming language, client code interaction with a Windows Runtime object happens at the lowest level, with client language constructs translated into calls into the object's ABI.
1821

@@ -149,11 +152,11 @@ For the lowest-level conversions, which only copy addresses, you can use the [**
149152
// Lowest-level conversions that only copy addresses
150153
151154
// Convert to a non-owning ABI object with get_abi.
152-
abi::IStringable* non_owning{ static_cast<abi::IStringable*>(winrt::get_abi(uriAsIStringable)) };
155+
abi::IStringable* non_owning{ reinterpret_cast<abi::IStringable*>(winrt::get_abi(uriAsIStringable)) };
153156
WINRT_ASSERT(non_owning);
154157
155158
// Avoid interlocks this way.
156-
owning = static_cast<abi::IStringable*>(winrt::detach_abi(uriAsIStringable));
159+
owning = reinterpret_cast<abi::IStringable*>(winrt::detach_abi(uriAsIStringable));
157160
WINRT_ASSERT(!uriAsIStringable);
158161
winrt::attach_abi(uriAsIStringable, owning);
159162
WINRT_ASSERT(uriAsIStringable);
@@ -350,7 +353,7 @@ void GetString(_Out_ HSTRING* value);
350353

351354
| Operation | How to do it | Notes |
352355
|-|-|-|
353-
| Extract **HSTRING** from **hstring** | `h = static_cast<HSTRING>(get_abi(s));` | *s* still owns the string. |
356+
| Extract **HSTRING** from **hstring** | `h = reinterpret_cast<HSTRING>(get_abi(s));` | *s* still owns the string. |
354357
| Detach **HSTRING** from **hstring** | `h = reinterpret_cast<HSTRING>(detach_abi(s));` | *s* no longer owns the string. |
355358
| Set **HSTRING** into **hstring** | `*put_abi(s) = h;` | *s* takes ownership of string. Any string previously owned by *s* is leaked (will assert in debug). |
356359
| Receive **HSTRING** into **hstring** | `GetString(reinterpret_cast<HSTRING*>(put_abi(s)));` | *s* takes ownership of string. Any string previously owned by *s* is leaked (will assert in debug). |

0 commit comments

Comments
 (0)