Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project COM interop APIs #830

Merged
merged 24 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
268b3c1
add COM interop API projections and tests
angelazhangmsft Apr 27, 2021
52600df
revising tests
angelazhangmsft Apr 29, 2021
5d97b7c
Merge branch 'master' into angzhang/interop
angelazhangmsft Apr 29, 2021
888a8cd
Merge branch 'angzhang/interop' of https://github.com/microsoft/CsWin…
angelazhangmsft Apr 29, 2021
94de3f6
Added cswinrt support for interop interfaces
Scottj1s May 4, 2021
2aa1e40
remove UAC check on IPrinting3DManagerInterop
Scottj1s May 4, 2021
664fc15
clarified wording
Scottj1s May 4, 2021
6b1be39
updated to latest cppwinrt to disable mdmerge validation
Scottj1s May 4, 2021
304fe6a
Removed Printing3DManager, as not in universal contract
Scottj1s May 5, 2021
5cc28fa
minor tweaks
Scottj1s May 10, 2021
c5370c2
typo
Scottj1s May 10, 2021
6dd85c7
merge conflicts
angelazhangmsft May 10, 2021
ad150be
fix incorrect merge conflict
angelazhangmsft May 10, 2021
e9d89a4
add comment on unit tests
angelazhangmsft May 10, 2021
276e027
restored cswinrt nuget package reference to resolve NETSDK1130 errors
Scottj1s May 12, 2021
5cacbf5
merge conflicts
angelazhangmsft May 17, 2021
02d174f
add COM interop wrappers
Scottj1s May 17, 2021
56d05bc
Merge branch 'angzhang/interop' of https://github.com/microsoft/CsWin…
Scottj1s May 17, 2021
6eb42e4
Merge branch 'master' into angzhang/interop
Scottj1s Jun 9, 2021
f2376ad
fully automate COM interop support for Windows SDK projection
Scottj1s Jun 9, 2021
32d92f2
Merge branch 'master' into angzhang/interop
Scottj1s Jun 11, 2021
7090e0f
PR feedback
Scottj1s Jun 11, 2021
761bbeb
remove bad checkin
Scottj1s Jun 11, 2021
30384cd
Merge branch 'master' into angzhang/interop
Scottj1s Jun 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/interop.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ SetString(MarshalString.GetAbi(marshalStr));
```

#### Interop Interfaces
**Note:** The CLR still supports marshaling COM (IUnknown), but not WinRT (IInspectable), interop interfaces. There are two approaches to marshaling IInspectable interfaces in C#/WinRT.
The CLR still supports marshaling COM (IUnknown), but not WinRT (IInspectable), interop interfaces. The Windows SDK projection provides definitions for several COM interop interfaces, including ***Windows.Storage.Streams.IBufferByteAccess***, ***WinRT.Interop.IWindowNative***, and ***WinRT.Interop.IInitializeWithWindow***. The Windows SDK projection also provides wrappers for all WinRT interop interfaces included in the Universal API Contract, such as ***Windows.Security.Credentials.UI.UserConsentVerifierInterop***. For custom or extension SDK interop interfaces, C#/WinRT supports two marshaling techniques.

##### Projected
If possible, the interop interface should be defined in IDL and a C#/WinRT projection produced for it. This automatically generates all marshaling logic so that calling code can pass and receive projected types. This definition of `IUserConsentVerifierInterop` from one of our test components is an example of this:
Expand Down
2 changes: 1 addition & 1 deletion src/Projections/Test/Test.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
Expand Down
333 changes: 333 additions & 0 deletions src/Projections/Windows/ComInteropHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
#if UAC_VERSION_13
#define UAC_VERSION_12
#endif
#if UAC_VERSION_12
#define UAC_VERSION_11
#endif
#if UAC_VERSION_11
#define UAC_VERSION_10
#endif
#if UAC_VERSION_10
#define UAC_VERSION_9
#endif
#if UAC_VERSION_9
#define UAC_VERSION_8
#endif
#if UAC_VERSION_8
#define UAC_VERSION_7
#endif
#if UAC_VERSION_7
#define UAC_VERSION_6
#endif
#if UAC_VERSION_6
#define UAC_VERSION_5
#endif
#if UAC_VERSION_5
#define UAC_VERSION_4
#endif
#if UAC_VERSION_4
#define UAC_VERSION_3
#endif
#if UAC_VERSION_3
#define UAC_VERSION_2
#endif
#if UAC_VERSION_2
#define UAC_VERSION_1
#endif
#if !UAC_VERSION_1
#error Unsupported Universal API Contract version
#endif

using System;
using System.Runtime.InteropServices;
using Windows.Foundation;
using Windows.Security.Credentials;
using WinRT;
using WinRT.Interop;

namespace WinRT.Interop
{
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
public interface IWindowNative
{
IntPtr WindowHandle { get; }
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("3E68D4BD-7135-4D10-8018-9FB6D9F33FA1")]
public interface IInitializeWithWindow
{
void Initialize(IntPtr hwnd);
}
}

namespace Windows.ApplicationModel.DataTransfer.DragDrop.Core
{
#if UAC_VERSION_1
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.10240.0")]
#endif
public static class DragDropManagerInterop
{
private static IDragDropManagerInterop dragDropManagerInterop = CoreDragDropManager.As<IDragDropManagerInterop>();

public static CoreDragDropManager GetForWindow(IntPtr appWindow)
{
Guid iid = GuidGenerator.CreateIID(typeof(ICoreDragDropManager));
return (CoreDragDropManager)dragDropManagerInterop.GetForWindow(appWindow, iid);
}
}
#endif
}

namespace Windows.Graphics.Printing
{
#if UAC_VERSION_1
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.10240.0")]
#endif
public static class PrintManagerInterop
{
private static IPrintManagerInterop printManagerInterop = PrintManager.As<IPrintManagerInterop>();

public static PrintManager GetForWindow(IntPtr appWindow)
{
Guid iid = GuidGenerator.CreateIID(typeof(IPrintManager));
return (PrintManager)printManagerInterop.GetForWindow(appWindow, iid);
}

public static IAsyncOperation<bool> ShowPrintUIForWindowAsync(IntPtr appWindow)
{
Guid iid = GuidGenerator.CreateIID(typeof(IAsyncOperation<bool>));
return (IAsyncOperation<bool>)printManagerInterop.ShowPrintUIForWindowAsync(appWindow, iid);
}
}
#endif
}

namespace Windows.Media
{
#if UAC_VERSION_1
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.10240.0")]
#endif
public static class SystemMediaTransportControlsInterop
{
private static ISystemMediaTransportControlsInterop systemMediaTransportControlsInterop = SystemMediaTransportControls.As<ISystemMediaTransportControlsInterop>();

public static SystemMediaTransportControls GetForWindow(IntPtr appWindow)
{
Guid iid = GuidGenerator.CreateIID(typeof(ISystemMediaTransportControls));
return (SystemMediaTransportControls)systemMediaTransportControlsInterop.GetForWindow(appWindow, iid);
}
}
#endif
}

namespace Windows.Media.PlayTo
{
#if UAC_VERSION_1
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.10240.0")]
#endif
public static class PlayToManagerInterop
{
private static IPlayToManagerInterop playToManagerInterop = PlayToManager.As<IPlayToManagerInterop>();

public static PlayToManager GetForWindow(IntPtr appWindow)
{
Guid iid = GuidGenerator.CreateIID(typeof(IPlayToManager));
return (PlayToManager)playToManagerInterop.GetForWindow(appWindow, iid);
}

public static void ShowPlayToUIForWindow(IntPtr appWindow)
{
playToManagerInterop.ShowPlayToUIForWindow(appWindow);
}
}
#endif
}

namespace Windows.Security.Credentials.UI
{
#if UAC_VERSION_1
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.10240.0")]
#endif
public static class UserConsentVerifierInterop
{
private static IUserConsentVerifierInterop userConsentVerifierInterop = UserConsentVerifier.As<IUserConsentVerifierInterop>();

public static IAsyncOperation<UserConsentVerificationResult> RequestVerificationForWindowAsync(IntPtr appWindow, string message)
{
var iid = GuidGenerator.CreateIID(typeof(IAsyncOperation<UserConsentVerificationResult>));
return (IAsyncOperation<UserConsentVerificationResult>)userConsentVerifierInterop.RequestVerificationForWindowAsync(appWindow, message, iid);
}
}
#endif
}

namespace Windows.Security.Authentication.Web.Core
{
#if UAC_VERSION_1
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.10240.0")]
#endif
public static class WebAuthenticationCoreManagerInterop
{
private static IWebAuthenticationCoreManagerInterop webAuthenticationCoreManagerInterop = WebAuthenticationCoreManager.As<IWebAuthenticationCoreManagerInterop>();

public static IAsyncInfo RequestTokenForWindowAsync(IntPtr appWindow, WebTokenRequest request)
{
var iid = GuidGenerator.CreateIID(typeof(IAsyncOperation<WebTokenRequestResult>));
return (IAsyncInfo)webAuthenticationCoreManagerInterop.RequestTokenForWindowAsync(appWindow, request, iid);
}

public static IAsyncInfo RequestTokenWithWebAccountForWindowAsync(IntPtr appWindow, WebTokenRequest request, WebAccount webAccount)
{
var iid = GuidGenerator.CreateIID(typeof(IAsyncOperation<WebTokenRequestResult>));
return (IAsyncInfo)webAuthenticationCoreManagerInterop.RequestTokenWithWebAccountForWindowAsync(appWindow, request, webAccount, iid);
}
}
#endif
}

namespace Windows.UI.ApplicationSettings
{
#if UAC_VERSION_1
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.10240.0")]
#endif
public static class AccountsSettingsPaneInterop
{
private static IAccountsSettingsPaneInterop accountsSettingsPaneInterop = AccountsSettingsPane.As<IAccountsSettingsPaneInterop>();

public static AccountsSettingsPane GetForWindow(IntPtr appWindow)
{
Guid iid = GuidGenerator.CreateIID(typeof(IAccountsSettingsPane));
return (AccountsSettingsPane)accountsSettingsPaneInterop.GetForWindow(appWindow, iid);
}

public static IAsyncAction ShowManageAccountsForWindowAsync(IntPtr appWindow)
{
Guid iid = GuidGenerator.CreateIID(typeof(IAsyncAction));
return (IAsyncAction)accountsSettingsPaneInterop.ShowManageAccountsForWindowAsync(appWindow, iid);
}

public static IAsyncAction ShowAddAccountForWindowAsync(IntPtr appWindow)
{
Guid iid = GuidGenerator.CreateIID(typeof(IAsyncAction));
return (IAsyncAction)accountsSettingsPaneInterop.ShowAddAccountForWindowAsync(appWindow, iid);
}
}
#endif
}

namespace Windows.UI.Input
{
#if UAC_VERSION_3
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.14393.0")]
#endif
public static class RadialControllerConfigurationInterop
{
private static IRadialControllerConfigurationInterop radialControllerConfigurationInterop
= RadialControllerConfiguration.As<IRadialControllerConfigurationInterop>();

public static RadialControllerConfiguration GetForWindow(IntPtr hwnd)
{
Guid iid = GuidGenerator.CreateIID(typeof(IRadialControllerConfiguration));
return (RadialControllerConfiguration)radialControllerConfigurationInterop.GetForWindow(hwnd, iid);
}
}

#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.14393.0")]
#endif
public static class RadialControllerInterop
{
private static IRadialControllerInterop radialControllerInterop = RadialController.As<IRadialControllerInterop>();

public static RadialController CreateForWindow(IntPtr hwnd)
{
Guid iid = GuidGenerator.CreateIID(typeof(IRadialController));
return (RadialController)radialControllerInterop.CreateForWindow(hwnd, iid);
}
}
#endif
}

namespace Windows.UI.Input.Core
{
#if UAC_VERSION_4
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.15063.0")]
#endif
public static class RadialControllerIndependentInputSourceInterop
{
private static IRadialControllerIndependentInputSourceInterop radialControllerIndependentInputSourceInterop
= RadialControllerIndependentInputSource.As<IRadialControllerIndependentInputSourceInterop>();

public static RadialControllerIndependentInputSource CreateForWindow(IntPtr hwnd)
{
Guid iid = GuidGenerator.CreateIID(typeof(IRadialControllerIndependentInputSource));
return (RadialControllerIndependentInputSource)radialControllerIndependentInputSourceInterop.CreateForWindow(hwnd, iid);
}
}
#endif
}

namespace Windows.UI.Input.Spatial
{
#if UAC_VERSION_2
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.10586.0")]
#endif
public static class SpatialInteractionManagerInterop
{
private static ISpatialInteractionManagerInterop spatialInteractionManagerInterop = SpatialInteractionManager.As<ISpatialInteractionManagerInterop>();

public static SpatialInteractionManager GetForWindow(IntPtr window)
{
Guid iid = GuidGenerator.CreateIID(typeof(ISpatialInteractionManager));
return (SpatialInteractionManager)spatialInteractionManagerInterop.GetForWindow(window, iid);
}
}
#endif
}

namespace Windows.UI.ViewManagement
{
#if UAC_VERSION_1
#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.10240.0")]
#endif
public static class InputPaneInterop
{
private static IInputPaneInterop inputPaneInterop = InputPane.As<IInputPaneInterop>();

public static InputPane GetForWindow(IntPtr appWindow)
{
Guid iid = GuidGenerator.CreateIID(typeof(IInputPane));
return (InputPane)inputPaneInterop.GetForWindow(appWindow, iid);
}
}

#if !NETSTANDARD2_0
[global::System.Runtime.Versioning.SupportedOSPlatform("Windows10.0.10240.0")]
#endif
public static class UIViewSettingsInterop
{
private static IUIViewSettingsInterop uIViewSettingsInterop = UIViewSettings.As<IUIViewSettingsInterop>();

public static UIViewSettings GetForWindow(IntPtr hwnd)
{
var iid = GuidGenerator.CreateIID(typeof(IUIViewSettings));
return (UIViewSettings)uIViewSettingsInterop.GetForWindow(hwnd, iid);
}
}
#endif
}
Loading