question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ICompositorDesktopInterop throws exception in TFM .NET 5

See original GitHub issue

I was trying to port a C# code from .NET 4.8 to .NET 5, older version of .NET was using Microsoft.Windows.SDK.Contracts, In .NET 5 Microsoft.Windows.SDK.Contracts is replaced by Target Framework Monikers (TFMs) I used the same code an tried to cast the compositor to ICompositorDesktopInterop but it throws exception :

System.InvalidCastException: 'Invalid cast from ‘Windows.UI.Composition.Compositor’ to ‘ICompositorDesktopInterop’

Here is the code:

private void InitComposition(IntPtr hwnd)
{
    ICompositorDesktopInterop interop;
    object iunknown = Compositor as object;
    interop = (ICompositorDesktopInterop)iunknown;
    IntPtr raw;
    interop.CreateDesktopWindowTarget(hwnd, true, out raw);

    object rawObject = Marshal.GetObjectForIUnknown(raw);
    var compositionTarget = (ICompositionTarget)rawObject;

    f (raw == null) { throw new Exception("QI Failed"); }
}

Since this method throws exception I tried a different method:

ICompositorDesktopInterop interop;
object iunknown = Compositor as object;
interop = iunknown.As<ICompositorDesktopInterop>();
interop.CreateDesktopWindowTarget(hwnd, true, out ICompositionTarget target);

For this there was no exception but whenever I use ICompositionTarget the Application closes without any messages.

Why is it not working with TFM ? Is there something I missed ?

I have the same issue in Cs/WinRT package also.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lindexicommented, Aug 26, 2021

@Extrimis Thank you.

1reaction
Extrimiscommented, Aug 25, 2021

I just find a fix now, and it is working for me, I will post the code on GitHub once I clean it up

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

ICompositorDesktopInterop throws exception in TFM .NET 5
The issue with TFM can be solved by using DirectN or by using InteropCompositor. For InteropCompositor: Set the TargetFramework in the ...
Read more >
ICompositorDesktopInterop throws exception in TFM .NET 5-wpf
The issue with TFM can be solved by using DirectN or by using InteropCompositor. For InteropCompositor: Set the TargetFramework in the project file...
Read more >
Casting from Compositor to ICompositorDesktopInterop ...
This is the exception thrown: System.InvalidCastException: 'Invalid cast from 'Windows.UI.Composition.Compositor' to 'Acrylic.
Read more >
c#-ICompositorDesktopInterop throws exception in TFM .NET 5
c# ICompositorDesktopInterop throws exception in TFM .NET 5 I was trying to port a C# code from .NET 4.8 to .NET 5, older...
Read more >
Handling and throwing exceptions in .NET
Learn how to handle and throw exceptions in .NET. Exceptions are how .NET operations indicate failure to applications.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found