Page 1 of 1

"Unable to cast COM object" in WPF project

PostPosted: Mon Dec 18, 2017 7:08 pm
by Scott Heimendinger
Hi there,

I'm attempting to call the DeckLinkAPI from within my WPF application. The code paths are essentially a clone of the CaturePreviewCSharp sample, which works just fine, but in my WPF project I'm hitting a failure right here that I can't seem to get past.

Code: Select all
public DeckLinkDevice(IDeckLink deckLink)
        {
            m_deckLink = deckLink;
            // Get input interface
             m_deckLinkInput = (IDeckLinkInput)m_deckLink;
...


This returns the error:
System.InvalidCastException: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'DeckLinkAPI.IDeckLinkInput'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{AF22762B-DFAC-4846-AA79-FA8883560995}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).'


My project references the Interop.DeckLinkAPI (v4.0.30319) that shipped with the SDK. The error occurs both when I try to run in x64 and x86. However, the exact same lines run just fine in the [modified, for my needs] CapturePreviewCSharp sample. Both my project and the SDK sample that works are targeting the .NET Framework 4.6.1.

Googling this general exception suggested that I need to use [ComImport] somewhere, but I want to keep the DeckLinkDevice class intact. and I believe those two are mutually exclusive. Or I just don't understand where I need to put that directive.

Any ideas how to unblock this issue? Thank you so much in advance - I've been pounding my head against the wall on this one :-)

Re: "Unable to cast COM object" in WPF project

PostPosted: Tue Dec 19, 2017 7:02 am
by Cameron Nichols
Hi Scott,

As the DeckLinkDeviceDiscover iterates through all devices, it is possible for this to occur if you have a DeckLink device installed without an input interface (eg DeckLink Mini Monitor)

Best to wrap in try-catch block:
Code: Select all
            try
            {
                // Get input interface
                m_deckLinkInput = (IDeckLinkInput)m_deckLink;
            }
            catch (InvalidCastException)
            {
                // No input interface found, eg in case of DeckLink Mini Monitor
                return;
            }


This will be fixed in CapturePreviewCSharp sample in future release.

Is this the likely cause for this exception?

Kind Regards
Cameron

Re: "Unable to cast COM object" in WPF project

PostPosted: Fri Mar 16, 2018 11:48 am
by wertolet
Get the same exception.

But my device have input interface.

It work correctly in WinForms, but throws exception in WPF.

Any ideas?

Re: "Unable to cast COM object" in WPF project

PostPosted: Fri Mar 16, 2018 11:55 am
by wertolet
Im got same exception, but my device have inputs.

In WinForms app it works fine, but in WPF in throws InvalidCastException.

Small code for check:

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using DeckLinkAPI;

namespace WpfBlackMagicTest
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DeckLinkDeviceDiscovery m_deckLinkDiscovery;

        public MainWindow()
        {
            InitializeComponent();

            m_deckLinkDiscovery = new DeckLinkDeviceDiscovery();

            m_deckLinkDiscovery.DeviceArrived += new DeckLinkDiscoveryHandler((d) => Dispatcher.Invoke((Action)(() => AddDevice(d))));
            m_deckLinkDiscovery.DeviceRemoved += new DeckLinkDiscoveryHandler((d) => Dispatcher.Invoke((Action)(() => RemoveDevice(d))));
        }

        void AddDevice(IDeckLink decklinkDevice)
        {
            DeckLinkDevice deckLink = new DeckLinkDevice(decklinkDevice);
            Console.WriteLine("ADD");
        }

        void RemoveDevice(IDeckLink decklinkDevice)
        {
            Console.WriteLine("REMOVE");
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            m_deckLinkDiscovery.Enable();
        }
    }
}


Any ideas?

Re: "Unable to cast COM object" in WPF project

PostPosted: Fri Mar 16, 2018 12:26 pm
by wertolet
Its more strange.

Im create a clear WinForm project, add same code - and got same exception.

But when im delete a bunch of code from CapturePreviewCSharp - and got same code, it works.

In result im have 2 indetical code, in two different projects. Im my it is not works, in sample it works fine.

Im check all properties and settings - same in both projects. But one woks, second not.

Im confused.

Re: "Unable to cast COM object" in WPF project

PostPosted: Fri Mar 16, 2018 12:50 pm
by wertolet
Got it.

Application must be runned in MTA thread to work with DeckLinkSDK.

WPF works in STA thread.

To use DeckLInk api in WPF you need to use different thread in MTA mode:


Code: Select all
_thread = new Thread(DeckLinkProcess);
            _thread.SetApartmentState(ApartmentState.MTA);
            _thread.Name = "DeckLinkProcess";
            _thread.Start();

Re: "Unable to cast COM object" in WPF project

PostPosted: Sun May 06, 2018 5:14 pm
by speack
Where is the CaturePreviewCSharp example?
Thanks

Re: "Unable to cast COM object" in WPF project

PostPosted: Thu Jan 10, 2019 3:14 pm
by raahul.s
Hello Team,
While using DecklinkAPI with WPF i am getting the following error :

Unable to cast COM object of type 'System.__ComObject' to interface type 'DeckLinkAPI.IDeckLinkInput'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{AF22762B-DFAC-4846-AA79-FA8883560995}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

Please help. It will be better if you provide the source code.

Regards,
Raahul Shrivastava

Re: "Unable to cast COM object" in WPF project

PostPosted: Fri Jan 11, 2019 2:02 am
by Cameron Nichols
Hi Raahul,

Which desktop video device are you targeting?

The most likely cause for HRESULT = E_NOINTERFACE is that you are attempting to perform QueryInterface with IID_IDeckLinkInput on a playback only device (such as the DeckLink Mini Monitor).

Regards
Cameron

Re: "Unable to cast COM object" in WPF project

PostPosted: Fri Jan 11, 2019 12:42 pm
by raahul.s
Hello Team,

We are using Decklink Mini Recorder. In addition, we are developing application in WPF in which DirectX is not Compatible with it. Therefore we are using SharpDx.
Please suggest WPF integration with DecklinkAPI.

Regards,
Raahul Shrivastava

Re: "Unable to cast COM object" in WPF project

PostPosted: Tue Feb 05, 2019 9:20 am
by raahul.s
Hello Team,
I am using Decklink Mini Recorder and have successfully capture images(stills) referring CaptureStills(CSharp) sample provided which uses old winform technology but i uses WPF MVVM Technology and convert old window form code to WPF. I got many errors and stuck many times in them but finally i got success.

Now i have to record video using same code and setup. As i refer your sample(CaptureStill(CSharp)) it is also not working properly in case of video capturing(Playback).I am getting same error and when i apply same solution that i have applied for capturing still, it does'nt work. I am unable to initialize DeckLinkOutputDevice class. In paramaterized constructor of DeckLinkOutputDevice class it throwing custom made DeckLinkOutputInvalidException exception and when jump the code to next line i.e.,
m_deckLinkOutput = (IDeckLinkOutput)deckLink;
It throws following error :
"Unable to cast COM object of type 'System.__ComObject' to interface type 'DeckLinkAPI.IDeckLinkOutput'.

I also put the code in MTA Thread as below :
Thread mta = new Thread(()=> {
m_deckLinkOutput = (IDeckLinkOutput)deckLink;
// Provide the delegate to the audio and video output interfaces
m_deckLinkOutput.SetScheduledFrameCompletionCallback(this);
m_deckLinkOutput.SetAudioCallback(this);
});
mta.SetApartmentState(ApartmentState.MTA);
mta.Start();

But then also getting same error.
Also i want to add that deckLink is not null.

Please help and provide some solution as soon as possible.

Regards,
Raahul S.

Re: "Unable to cast COM object" in WPF project

PostPosted: Tue Aug 04, 2020 4:46 am
by walrus811
It's happened to me too.
I found the solution.
I think it's the problem for aparament of COM.
When I checked apartment of DeckLink API methods by using System.Threading.Thread.CurrentThread.GetApartmentState().
It's running on MTA(IDeckLinkDeviceNotificationCallback.DeckLinkDeviceArrived), but you know, WPF is on STA.
After I run the code throws the exception with the below code, It works!

Code: Select all
public static void RunOnMtaThread(Action method)
{
   var evnt = new ManualResetEvent(false);

  var thread = new Thread(delegate ()
  {
    method();
    evnt.Set();
  });

  thread.SetApartmentState(ApartmentState.MTA);
  thread.Start();
  evnt.WaitOne();
}


img1.png
img1.png (47.92 KiB) Viewed 6672 times


I am so happy to solve this one!
I did this all day long...

Re: "Unable to cast COM object" in WPF project

PostPosted: Tue Aug 04, 2020 11:35 pm
by Cameron Nichols
Hi Ray,

Thanks for sharing this solution.

Although probably too late for you, as well Desktop Video SDK 11.6 adds sample DeviceStatusCSharp, a WPF sample that has apartment helpers for Action<T> and Func<TResult> methods.

Regards
Cameron

Re: "Unable to cast COM object" in WPF project

PostPosted: Mon Apr 12, 2021 8:53 am
by jhip
Hi walrus811

we try to apply your approach...unsucessfully
can you post a small wpf project anywhere please ?

Thanks in advance.

Re: "Unable to cast COM object" in WPF project

PostPosted: Fri Apr 23, 2021 6:15 am
by Cameron Nichols
Hi jhip,

Please have a look at Desktop Video SDK sample DeviceStatusCSharp, this was added in 11.6 release and implements WPF.

Regards
Cameron

Re: "Unable to cast COM object" in WPF project

PostPosted: Thu Feb 23, 2023 4:14 pm
by robotor
Hi,

resurecting this thread...
I have the very same issue as many here. Sadly enough not even using MTAAction() from the Desktop Video SDK's class ApartmentHelper is actually helping- still getting the E_NOINTERFACE...
I'm trying to create a remote control for Atem Mixer within our main app (Startup project: WinForms VB x64).

I extracted what I have for our SDK implementation together with some dependencies from the main application. Now, one thing that I see is that I get a BadImageException when the Build Configurations of the involved project-dependencise aren't all the same... but everything set the same then it is running smoothly (executing the parts in question from a console tester app) even without the ApartmentHelper.

What else could I be looking for?