Skip to content

Commit 4d9bcfb

Browse files
authored
Merge pull request #4599 from MicrosoftEdge/api-specs-com-conversion
API Review: Interop between COM and WinRT
2 parents 977d00b + 817bb8b commit 4d9bcfb

1 file changed

Lines changed: 89 additions & 1 deletion

File tree

specs/CoreWebView2.CreateFromCOMObject.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ To help implement this for Unity, we are adding a new static factory function on
1717
class that will allow it to wrap an existing ICoreWebView2 COM object, instead of creating a new
1818
one.
1919

20+
To round out the more general scenario of interoperating between libraries written in different
21+
languages, we add the ability to convert a CoreWebView2 to and from .NET and COM, as well as
22+
to and from WinRT and COM.
23+
2024
# Examples
21-
## CoreWebView2.CreateFromComICoreWebView2
25+
26+
## COM to .NET
27+
2228
```c#
2329
public class MyWebView2Control
2430
{
@@ -47,8 +53,52 @@ public class MyWebView2Control
4753
}
4854
```
4955

56+
## WinRT to COM
57+
58+
```c++
59+
winrt::com_ptr<ICoreWebView2> GetComICoreWebView2FromCoreWebView2(
60+
winrt::Microsoft::Web::WebView2::Core::CoreWebView2 coreWebView2WinRT)
61+
{
62+
// Get the COM interop interface from the WinRT CoreWebView2.
63+
auto interop = coreWebView2WinRT.as<ICoreWebView2Interop2>();
64+
65+
// Get the COM ICoreWebView2 object from the COM interop interface.
66+
winrt::com_ptr<ICoreWebView2> coreWebView2Com;
67+
winrt::check_hresult(interop->GetComICoreWebView2(coreWebView2Com.put()));
68+
69+
return coreWebView2Com;
70+
}
71+
```
72+
73+
## COM to WinRT
74+
75+
```c++
76+
winrt::Microsoft::Web::WebView2::Core::CoreWebView2 CreateCoreWebView2FromComICoreWebView2(
77+
winrt::com_ptr<ICoreWebView2> coreWebView2Com)
78+
{
79+
auto factory = winrt::get_activation_factory<
80+
winrt::Microsoft::Web::WebView2::Core::CoreWebView2>();
81+
82+
// Get the COM interop interface from the WinRT factory.
83+
auto interop = factory.try_as<ICoreWebView2ActivationFactoryInterop>();
84+
85+
// Get the WinRT CoreWebView2 object from the COM interop interface as
86+
// its ABI interface.
87+
winrt::com_ptr<IUnknown> coreWebView2WinRTAsIUnknown;
88+
winrt::check_hresult(interop->CreateFromComICoreWebView2(
89+
coreWebView2Com.get(), coreWebView2WinRTAsIUnknown.put()));
90+
91+
// Convert from the WinRT CoreWebView2 object API interface to C++/WinRT type
92+
return coreWebView2WinRTAsIUnknown.as<
93+
winrt::Microsoft::Web::WebView2::Core::CoreWebView2>();
94+
}
95+
```
96+
5097

5198
# API Details
99+
100+
## .NET API
101+
52102
```c#
53103
namespace Microsoft.Web.WebView2.Core
54104
{
@@ -64,10 +114,48 @@ namespace Microsoft.Web.WebView2.Core
64114
/// <exception cref="ArgumentNullException">Thrown when the provided COM pointer is null.</exception>
65115
/// <exception cref="InvalidComObjectException">Thrown when the value is not an ICoreWebView2 COM object and cannot be wrapped.</exception>
66116
public static CoreWebView2 CreateFromComICoreWebView2(IntPtr value);
117+
118+
/// <summary>
119+
/// Returns the existing COM ICoreWebView2 object underlying this .NET CoreWebView2 object.
120+
/// This allows interacting with the WebView2 control using COM APIs,
121+
/// even if the control was originally created using .NET.
122+
/// </summary>
123+
/// <returns>Pointer to a COM object that implements the ICoreWebView2 COM interface.</returns>
124+
public IntPtr GetComICoreWebView2();
67125
}
68126
}
69127
```
70128

129+
## WinRT COM Interop API
130+
131+
```c# (but really COM IDL)
132+
/// Interop interface for the CoreWebView2 WinRT object to allow WinRT end
133+
/// developers to be able to use COM interfaces as parameters for some methods.
134+
/// This interface is implemented by the Microsoft.Web.WebView2.Core.CoreWebView2
135+
/// runtime class.
136+
[uuid(B151AD7C-CFB0-4ECF-B9B2-AFCA868581A6), object, pointer_default(unique)]
137+
interface ICoreWebView2Interop2 : IUnknown {
138+
/// Get a COM ICoreWebView2 interface corresponding to this WinRT CoreWebView2
139+
/// object.
140+
HRESULT GetComICoreWebView2([out, retval] ICoreWebView2** coreWebView2);
141+
}
142+
143+
/// Interop interface for the CoreWebView2 WinRT activation factory object to allow
144+
/// WinRT end developers to be able to use COM interfaces as parameters for some
145+
/// methods.
146+
/// This interface is implemented by the Microsoft.Web.WebView2.Core.CoreWebView2
147+
/// activation factory runtime class.
148+
[uuid(BABBED43-D40E-40CF-B106-8ED65FAE2E7C), object, pointer_default(unique)]
149+
interface ICoreWebView2ActivationFactoryInterop : IUnknown {
150+
/// Creates a CoreWebView2 WinRT object that wraps an existing COM ICoreWebView2 object.
151+
/// This allows interacting with the WebView2 control using WinRT,
152+
/// even if the control was originally created using COM.
153+
HRESULT CreateFromComICoreWebView2([in] ICoreWebView2* coreWebView2Com,
154+
[out, retval] IUnknown** coreWebView2WinRt);
155+
}
156+
157+
```
158+
71159
# Appendix
72160
We have a couple of other options to accomplish this, including moving the "CreateFromComICoreWebView2" function to the
73161
CoreWebView2Controller class instead. CoreWebView2Controller could then be used to get the CoreWebView2 through

0 commit comments

Comments
 (0)