次の方法で共有


.NET アプリから相互運用 API を呼び出す

C# デスクトップ アプリケーション開発者は、.NETで、複数の相互運用性関数と Windows Runtime (WinRT) COM 相互運用性インターフェイスを表す C# 相互運用クラスを使用できます。 これには、 IWindowNativeIInitializeWithWindowGetWindowIdFromWindow 関数などを表す C# クラスが含まれます。

このトピックでは、使用可能な C# 相互運用クラスとその使用方法を示します。 トピックの最後の「Background」セクションでは、以前のバージョンの.NETで相互運用インターフェイスがどのように使用されたか、および変更が行われた理由について説明します。

C# 相互運用クラスを使用するように .NET デスクトップ projectを構成する

次のセクションに示す C# 相互運用クラス (Available C# 相互運用クラス) は、Windows App SDK の一部として、または特定の Target Framework Moniker を使用して.NETで使用できます。

WinUI 3 C# デスクトッププロジェクト

Visual Studioで新しい WinUI projectを作成すると (最初の WinUI project)、projectは既に構成されており、すべての C# 相互運用クラスの使用をすぐに開始できます。

その他のC#デスクトップ プロジェクトのタイプ (WPF または WinForms)

Windows Presentation Foundation (WPF)Windows Forms (WinForms) など、その他の.NETデスクトップ projectの種類の場合は、C# 相互運用クラスをaccessする前にprojectを構成する必要があります。 以下に示すクラスの最初のセットでは、Windows App SDKを参照する必要があります。 2 番目のセットでは、次のように、Windows 10 バージョン 1809 以降を対象とする Target Framework Moniker を構成する必要があります。

  1. C# .NET デスクトップ projectのproject ファイルを開きます。

  2. .csproj ファイルで、TargetFramework 要素を変更して、特定の.NETと Windows SDK のバージョンをターゲットにします。 たとえば、次の要素は、バージョン 2004 Windows 10を対象とする .NET 6 projectに適しています。

    <PropertyGroup>
      <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
    </PropertyGroup>
    

サポートされているその他の値の一覧など、詳細については、「ターゲット フレームワーク モニカー オプションを使用する」を参照してください。

使用可能な C# 相互運用クラス

Note

以下のクラスには、.NET 6 SDK 以降が必要です。

基になる相互運用機能または WinRT COM 相互運用インターフェイスからマップされた、使用可能な C# 相互運用クラスを次に示します。 リストされている各クラスは、基になる相互運用 API の関数/メソッドを実装し、パラメーターと戻り値の型セーフ ラッパーを提供します。 たとえば、 Windows.ApplicationModel.DataTransfer.DragDrop.Core.DragDropManagerInterop.GetForWindow には IntPtr ウィンドウ ハンドル (HWND) パラメーターが必要であり、 CoreDragDropManager オブジェクトが返されます。 以下のすべての C# 相互運用クラスと関連するメソッドは静的です。

Windows App SDKの一部として使用可能

Microsoft.UI.Win32Interop クラスは、次の表に示す C# 相互運用メソッドを実装します。 コード例については、「アプリ ウィンドウ管理する」を参照してください。

相互運用機能 C# 相互運用メソッド
GetDisplayIdFromMonitor (Microsoft.UI) DisplayId Win32Interop.GetDisplayIdFromMonitor(IntPtr hmonitor)
GetIconFromIconId (Microsoft.UI) IntPtr Win32Interop.GetIconFromIconId(IconId iconId)
GetIconIdFromIcon (Microsoft.UI) IconId Win32Interop.GetIconIdFromIcon(IntPtr hicon)
GetMonitorFromDisplayId (Microsoft.UI) IntPtr Win32Interop.GetMonitorFromDisplayId(DisplayId displayId)
GetWindowFromWindowId (Microsoft.UI) IntPtr Win32Interop.GetWindowFromWindowId(WindowId windowId)
GetWindowIdFromWindow (Microsoft.UI) WindowId Win32Interop.GetWindowIdFromWindow(IntPtr hwnd)

ターゲット フレームワーク モニカー経由で使用可能

WinRT COM 相互運用インターフェイス C# 相互運用機能クラス
IAccountsSettingsPaneInterop (Windows.UI.ApplicationSettings) AccountsSettingsPaneInterop
IDisplayInformationStaticsInterop TFM net6.0-windows10.0.22621.0 および .NET 6.0.7 で導入されました。

(Windows.Graphics.Display) DisplayInformationInterop
IDragDropManagerInterop (Windows.ApplicationModel.DataTransfer.DragDrop.Core) DragDropManagerInterop
IInitializeWithWindow (WinRT.Interop) InitializeWithWindow
IInputPaneInterop (Windows.UI.ViewManagement) InputPaneInterop
IPlayToManagerInterop (Windows.Media.PlayTo) PlayToManagerInterop
IPrintManagerInterop (Windows.Graphics.Printing) PrintManagerInterop
IRadialControllerConfigurationInterop (Windows.UI.Input) RadialControllerConfigurationInterop
IRadialControllerIndependentInputSourceInterop (Windows.UI.Input.Core) RadialControllerIndependentInputSourceInterop
IRadialControllerInterop (Windows.UI.Input) RadialControllerInterop
ISpatialInteractionManagerInterop (Windows.UI.Input.Spatial) SpatialInteractionManagerInterop
ISystemMediaTransportControlsInterop (Windows.Media) SystemMediaTransportControlsInterop
IUIViewSettingsInterop (Windows.UI.ViewManagement) UIViewSettingsInterop
IUserConsentVerifierInterop (Windows.Security.Credentials.UI) UserConsentVerifierInterop
IWebAuthenticationCoreManagerInterop (Windows.Security.Authentication.Web.Core) WebAuthenticationCoreManagerInterop
IWindowNative WinUI のみ

(WinRT.Interop) WindowNative

WPFと WinForms の代替方法については、ウィンドウ ハンドル (HWND)を参照してください。

コード例

このコード例では、WinUI アプリケーションで 2 つの C# 相互運用クラスを使用する方法を示します (最初の WinUI projectを参照)。 シナリオの例では、Windows.Storage.Pickers.FolderPicker を表示します。 ただし、デスクトップ アプリでピッカーを表示する前に、所有者ウィンドウのハンドル (HWND) を使用してピッカーを初期化する必要があります。

  1. ウィンドウ ハンドル (HWND) は、 IWindowNative WinRT COM 相互運用機能インターフェイスを使用して取得できます。 また(前のセクションの表を参照)、そのインターフェイスは WinRT.Interop.WindowNative C# 相互運用クラスによって表されます。 ここでは、 this オブジェクトは、メイン ウィンドウ分離コード ファイルの Microsoft.UI.Xaml.Window オブジェクトへの参照です。
  2. 所有者ウィンドウを使用して UI の一部を初期化するには、 IInitializeWithWindow WinRT COM 相互運用インターフェイスを使用します。 また、そのインターフェイスは WinRT.Interop.InitializeWithWindow C# 相互運用クラスによって表されます。
private async void myButton_Click(object sender, RoutedEventArgs e)
{
    // Create a folder picker.
    var folderPicker = new Windows.Storage.Pickers.FolderPicker();

    // 1. Retrieve the window handle (HWND) of the current WinUI window.
    var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

    // 2. Initialize the folder picker with the window handle (HWND).
    WinRT.Interop.InitializeWithWindow.Initialize(folderPicker, hWnd);

    // Use the folder picker as usual.
    folderPicker.FileTypeFilter.Add("*");
    var folder = await folderPicker.PickSingleFolderAsync();
}

ウィンドウ ハンドル (HWND) の取得と、CoreWindowに依存する WinRT UI オブジェクトの表示 も参照してください。

Background

以前のバージョンの .NET Framework と .NET Core には、WinRT に関する知識が組み込まれています。 以前のバージョンでは、 ComImport 属性を使用して C# で相互運用インターフェイスを直接定義し、投影されたクラスを直接その相互運用インターフェイスにキャストできます。

WinRT は Windows 固有のテクノロジであるため、 .NETの移植性と効率性の目標をサポートするために、C# コンパイラと .NET ランタイムから WinRT プロジェクション のサポートを解除し、C#/WinRT ツールキットに移動しました ( WinRT の組み込みサポートは .NET から削除されます)。

ComImport 手法は引き続き IUnknown ベースの相互運用インターフェイスで機能しますが、WinRT との相互運用に使用される IInspectable ベースのインターフェイスでは機能しなくなりました。

そのため、代わりに、.NETでは、このトピックで説明する C# 相互運用クラスを使用できます。

トラブルシューティングと既知の問題

現在、C# 相互運用クラスに関する既知の問題はありません。 フィードバックを提供したり、他の問題を報告したりするには、既存の問題にフィードバックを追加するか、WindowsAppSDK GitHub リポジトリに新しい問題を提出します。