"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: 93
  • 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

Return to Software Developers

Who is online

Users browsing this forum: No registered users and 1 guest