locked
WinRT C++ Component InvalidCastException

    Question

  • I have a C++ WinRT Component. I call it from a view's codebehind in a C# project. Then in another view's codebehind and I get this exception below. What can be the problem, and the solution for it?

    Additional information: Unable to cast COM object of type 'WinRTComponentDll1.WinRTComponent' to interface type 'WinRTComponentDll1.__IWinRTComponentPublicNonVirtuals'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{E42A7AB9-F87C-3A87-8F44-564F09CBA7AD}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

    thanks,

    Laci



    • Edited by ladeak Monday, January 16, 2012 10:11 AM
    Thursday, January 12, 2012 2:08 PM

Answers

  • Thanks Laci. Did you encounter any problems submitting the bug with the above steps? In the Developer Preview, you can try to work around this by creating the object within the lambda

                Task.Run(() => {
                    WinRTComponent c2 = new WinRTComponent();
                    var result2 = c2.Method(2);
                });
    

     

    Thanks,

    -David

     


    Thursday, February 2, 2012 1:39 AM
    Moderator

All replies

  • Hi Laci,

     

    Could you provide the snippets of relevant code showing how you are using the WinRT component in the working and non working code? Are the views noted in the same project?

     

    Thanks,

    -David

    Wednesday, January 25, 2012 7:08 PM
    Moderator
  • Hi!

    When the WinRT component's constructor has an argument, it fails on a new Page. Without the argument, it works well. Calling with argument:

     

    var t = new WinRTComponent(new List<string>());

     

    without arg:

    var t = new WinRTComponent();


    And the constructor does nothing, just returns.

    Friday, January 27, 2012 2:23 PM
  • Hi Laci,

     

    I am not finding a bug with this problem description reported to date. Could you please submit a bug here. It would really help if you could add a sample repro project for the VS team to investigate as well.

     

    Thanks,

    -David

     

    Wednesday, February 1, 2012 12:32 AM
    Moderator
  • Steps to reproduce:

    1) Create a new solution.

    2) Add a new c++ WinRT library project.

    3) Add a XAML Application project.

    4) Reference the WinRT project.

    5) Reference platform.winrt from Visual Studio VC/bin directory.

    6) Modify MainPage.xaml.cs:

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Threading.Tasks;

    using Windows.Foundation;

    using Windows.UI.Xaml;

    using Windows.UI.Xaml.Controls;

    using Windows.UI.Xaml.Data;

    using WinRTComponentDll1;

     

    namespace Application1

    {

        partial class MainPage

        {

            public MainPage()

            {

                InitializeComponent();

     

                WinRTComponent c = new WinRTComponent();

                var result = c.Method(1);

     

                Task.Run(() => { var result2 = c.Method(2); });

            }

        }

    }

     

     

    7) Build and run.

     

    And finally InvalidCastException occures on the second call:

    Unable to cast COM object of type 'WinRTComponentDll1.WinRTComponent' to interface type 'WinRTComponentDll1.__IWinRTComponentPublicNonVirtuals'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{E42A7AB9-F87C-3A87-8F44-564F09CBA7AD}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

     

    Wednesday, February 1, 2012 5:10 PM
  • Thanks Laci. Did you encounter any problems submitting the bug with the above steps? In the Developer Preview, you can try to work around this by creating the object within the lambda

                Task.Run(() => {
                    WinRTComponent c2 = new WinRTComponent();
                    var result2 = c2.Method(2);
                });
    

     

    Thanks,

    -David

     


    Thursday, February 2, 2012 1:39 AM
    Moderator
  • Hi David!

    Thanks for your fast reply!

    I have submitted the problem to the bugtracker as well :)

    We have tried this workaround, it works, however creating the component this way is too slow for us.

    Laci

    Thursday, February 2, 2012 9:29 AM
  • Thanks Laci. Submitting bugs like this will help ensure we have a solid product at release. Please keep in mind the Developer Preview is a very early pre-beta release. Watch for the next public release for improvements.

    -David

    Thursday, February 2, 2012 5:13 PM
    Moderator
  • Thank you for your help!

    We are waiting for the next release ;)

    Thursday, February 2, 2012 5:26 PM