"Unable to cast COM object" in WPF project

Ask software engineering and SDK questions for developers working on Mac OS X, Windows or Linux.
  • Author
  • Message
Offline

Scott Heimendinger

  • Posts: 3
  • Joined: Mon Dec 18, 2017 6:58 pm

"Unable to cast COM object" in WPF project

PostMon Dec 18, 2017 7:08 pm

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 :-)
Offline

Cameron Nichols

Blackmagic Design

  • Posts: 154
  • Joined: Mon Sep 04, 2017 4:05 am

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

PostTue Dec 19, 2017 7:02 am

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
Offline

wertolet

  • Posts: 4
  • Joined: Fri Mar 16, 2018 11:46 am
  • Real Name: Nikolay Bystrov

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

PostFri Mar 16, 2018 11:48 am

Get the same exception.

But my device have input interface.

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

Any ideas?
Offline

wertolet

  • Posts: 4
  • Joined: Fri Mar 16, 2018 11:46 am
  • Real Name: Nikolay Bystrov

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

PostFri Mar 16, 2018 11:55 am

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?
Offline

wertolet

  • Posts: 4
  • Joined: Fri Mar 16, 2018 11:46 am
  • Real Name: Nikolay Bystrov

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

PostFri Mar 16, 2018 12:26 pm

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.
Offline

wertolet

  • Posts: 4
  • Joined: Fri Mar 16, 2018 11:46 am
  • Real Name: Nikolay Bystrov

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

PostFri Mar 16, 2018 12:50 pm

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();
Offline

speack

  • Posts: 2
  • Joined: Thu May 03, 2018 3:37 pm
  • Real Name: Israel Perez

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

PostSun May 06, 2018 5:14 pm

Where is the CaturePreviewCSharp example?
Thanks
Offline

raahul.s

  • Posts: 4
  • Joined: Thu Jan 10, 2019 3:02 pm
  • Real Name: Raahul Shrivastava

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

PostThu Jan 10, 2019 3:14 pm

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
Offline

Cameron Nichols

Blackmagic Design

  • Posts: 154
  • Joined: Mon Sep 04, 2017 4:05 am

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

PostFri Jan 11, 2019 2:02 am

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
Offline

raahul.s

  • Posts: 4
  • Joined: Thu Jan 10, 2019 3:02 pm
  • Real Name: Raahul Shrivastava

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

PostFri Jan 11, 2019 12:42 pm

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
Offline

raahul.s

  • Posts: 4
  • Joined: Thu Jan 10, 2019 3:02 pm
  • Real Name: Raahul Shrivastava

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

PostTue Feb 05, 2019 9:20 am

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.

Return to Software Developers

Who is online

Users browsing this forum: Jonas Geduldig and 5 guests