smourier / DirectN

Direct interop Code for .NET Framework, .NET Core and .NET 5+ : DXGI, WIC, DirectX 9 to 12, Direct2D, Direct Write, Direct Composition, Media Foundation, WASAPI, CodecAPI, GDI, Spatial Audio, DVD, Windows Media Player, UWP DXInterop, WinUI3, etc.
MIT License
311 stars 28 forks source link

`Exception thrown: 'System.InvalidCastException' in WinRT.Runtime.dll` (Again?) #37

Closed vmx17 closed 1 year ago

vmx17 commented 1 year ago

Hi! I'm using DirectNCore, downloaded as an NuGet package in WindowsAppSDK desktop project. Now I hit to an exception saying Exception thrown: 'System.InvalidCastException' in WinRT.Runtime.dll before calling SetSwapChain(). If you have any suggestions, please let me know. The code below;

public override void Initialize(SwapChainPanel _pnl)
{
    lock (m_CriticalLock) {
        ....

        var scpDesc = new DXGI_SWAP_CHAIN_DESC1();
        scpDesc.Width = (uint)_pnl.ActualWidth;
        ....
        IDXGIDevice1 dxgiDevice = m_device.As<IDXGIDevice1>(true);
        m_dxgiDevice = new ComObject<IDXGIDevice1>(dxgiDevice);
        m_swapChain = fac.CreateSwapChainForComposition<IDXGISwapChain1>(m_dxgiDevice, scpDesc);
        ....
        var nativepanel = _pnl.As<ISwapChainPanelNative>(); // <<<< Exception thrown here.
        if (nativepanel != null) {
            nativepanel.SetSwapChain(m_swapChain.Object);
        } else {
            throw new InvalidCastException("a SwapChainPanel could not be cast to DirectN.ISwapChainPanelNative");
        }
        ....
    }
}

The _pnl is SwapChainPanel I specified in XAML as Microsoft.UI.Xaml.Controls.SwapChainPanel. Though there was similar subject, I could not figure out how microsoft.ui.xaml.media.dxinterop.h concerns. The \<\<peek definition>> shows decompiled result;

[ComImport]
[Guid("f92f19d2-3ade-45a6-a20c-f6f1ea90554b")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ISwapChainPanelNative
{
    [PreserveSig]
    HRESULT SetSwapChain(IDXGISwapChain swapChain);
}

The Guid seems differ from the article. My environment VS2022Pro 17.6.1, .Net 6.0, WindowsAppSDK, all packages are latest fixed.

smourier commented 1 year ago

Hi,

You can find a full DirectN + WinAppSDK WinUI3 sample here https://github.com/smourier/DirectN/tree/master/DirectN/DirectN.WinUI3.MinimalD3D11

In the main file MainWindow.xaml.cs you will see I have redeclared ISwapChainPanelNative manually as it's not the same as the one from UWP's SwapChainPanel, they only differ by the GUID (it makes sense, sort of, thanks Microsoft for that nice trick :-)): https://github.com/smourier/DirectN/blob/master/DirectN/DirectN.WinUI3.MinimalD3D11/MainWindow.xaml.cs#L309

Here is how you should declare it:

[ComImport, Guid("63aad0b8-7c24-40ff-85a8-640d944cc325"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public partial interface ISwapChainPanelNative
{
    [PreserveSig]
    HRESULT SetSwapChain(IDXGISwapChain swapChain);
}
vmx17 commented 1 year ago

Thank you so much. I could get compiled. So, is the Guid depends on "microsoft.ui.xaml.media.dxinterop.h" header with which DirectN compiled? When and how should I looking for the changed Guid? It seems not parmanent.

I appreciate your many help.

smourier commented 1 year ago

DirectN's ISwapChainPanelNativedepends on the Windows SDK headers (windows.ui.xaml.media.dxinterop.h), so it will have IID set to F92F19D2-3ADE-45A6-A20C-F6F1EA90554B. This is for UWP projects.

ISwapChainPanelNative with IID set to 63aad0b8-7c24-40ff-85a8-640d944cc325 depends on Windows App SDK headers (microsoft.ui.xaml.media.dxinterop.h). DirectN currently doesn't include Windows App SDK headers support. This is something I'm considering to add in the future (but in another namespace because of name collisions).

vmx17 commented 1 year ago

I see. I'll try to take care when same issue occur. Thank you.