locked
Exception when calling a winrt component from static lib

    Question

  • Hi,

    I have created a WinRT component and I am trying to call it from a static lib :

    namespace TestWinRT
    {
        public sealed class Class1
        {
            public IAsyncOperation<int> GetInt()
            {
                return AsyncInfo.Run((ct) => allo());
            }
    
            private async Task<int> allo()
            {
                return 25;
            }
    
        }
    }

    and I have added a refernce to this winrt component from my static lib and I am calling it like that :

    using namespace TestWinRT;
    
    dpio::FileStream::FileStream( Partition * partition, const dp::String& fileURL, StreamClient * client,
    					unsigned int caps )
    : m_client(client), m_file(NULL), m_mapping(NULL), m_fileSize(0), m_ptr(NULL)
    {
    	Class1^ class1 = ref new Class1();
    	auto x =class1->GetInt();
    
    ...
    }

    But as soon as ref new Class1 is called I get a _COMPlusExceptionCode -532462766, when I try to step into new ,I get

     hr = __getActivationFactoryByHSTRING(className, riid, ppActivationFactory);

    and hr = 0x80040154 Class not registered 

    Any idea why ?

    Tuesday, July 24, 2012 9:11 AM

Answers

  • From C++ Xaml you can, there is MS online sample "Creating a Windows Runtime in-process component sample (C#)" with C# "ProxyStubsForWinRTComponents_server".

    What's the diff C++ app and static lib for runtime component? It would work for both. I've checked and had only 1 warning"

    warning LNK4264: archiving object file compiled with /ZW into a static library; note that when authoring Windows Runtime types it is not recommended to link with a static library that contains Windows Runtime metadata

    "


    PS but it's not easy, not clear as for c# client
    PPS Thanks MS for samples

    • Edited by Valera_ Wednesday, January 09, 2013 1:11 PM sample name
    • Marked as answer by Vincent Rich Tuesday, January 15, 2013 2:07 PM
    Wednesday, January 09, 2013 11:53 AM

All replies

  • Class1 is not a runtime class, so you can't ref new an instance of it. If you change your Class1 declaration to the following, you can ref new an instance. See the Quick Reference (C++/CX) for more details on using ref and ref new.

    public ref  class Class1 sealed


    David Lamb

    • Proposed as answer by Jesse Jiang Wednesday, July 25, 2012 7:49 AM
    Tuesday, July 24, 2012 11:26 PM
    Moderator
  • My Class1 is written in C# not in C++/CX so don't think ref is a recognized keyword
    Wednesday, July 25, 2012 8:11 AM
  • So is possible or not to call a WinRT component written in C# from a static lib project ?
    Thursday, July 26, 2012 9:45 AM
  • From C++ Xaml you can, there is MS online sample "Creating a Windows Runtime in-process component sample (C#)" with C# "ProxyStubsForWinRTComponents_server".

    What's the diff C++ app and static lib for runtime component? It would work for both. I've checked and had only 1 warning"

    warning LNK4264: archiving object file compiled with /ZW into a static library; note that when authoring Windows Runtime types it is not recommended to link with a static library that contains Windows Runtime metadata

    "


    PS but it's not easy, not clear as for c# client
    PPS Thanks MS for samples

    • Edited by Valera_ Wednesday, January 09, 2013 1:11 PM sample name
    • Marked as answer by Vincent Rich Tuesday, January 15, 2013 2:07 PM
    Wednesday, January 09, 2013 11:53 AM