locked
How can I consume a c# windows runtime component in a WRL (C++) class?

    Question

  • I would like to instantiate a class of a C# windows runtime project, from a WRL project.

    In order to make the C# class I have created a WRL project to generate an interface for it to implement. This has worked, but I would rather do it all in the one C# project. I don't know how to do this, and I also don't know how to finally consume the C# project (and the resulting .winmd file) from a Wrl project.  

    For that matter, I don't know how to use a C++/CX project from a wrl project either. So basically I am hoping for a bit of help understanding WinRT components. 

    Tuesday, March 24, 2015 6:34 PM

Answers

  • I looked at the WRL template that is shown here:
    https://msdn.microsoft.com/en-us/library/hh973463.aspx?f=255&MSPPError=-2147217396

    Unfortunately, the instructions to download it didn't work for me, so I wasn't able to test it.

    I think you simply create your C# component first, then add a reference to it in the C++ project.  In the .cpp file, you should be able to add a "using namespace" declaration that points to your component, then instantiate and use it normally.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, March 24, 2015 7:34 PM
    Moderator

All replies

  • I looked at the WRL template that is shown here:
    https://msdn.microsoft.com/en-us/library/hh973463.aspx?f=255&MSPPError=-2147217396

    Unfortunately, the instructions to download it didn't work for me, so I wasn't able to test it.

    I think you simply create your C# component first, then add a reference to it in the C++ project.  In the .cpp file, you should be able to add a "using namespace" declaration that points to your component, then instantiate and use it normally.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, March 24, 2015 7:34 PM
    Moderator
  • Thanks for the response. I have been able to get the C# object from within  a WRL (not C++/CX) project using this:

    		template<typename T> void CreateComponent(const wchar_t* activableClassId,
    			::Microsoft::WRL::Details::ComPtrRef<ComPtr<T>> pInstance)
    		{
    			HStringReference runtimeClass(activableClassId);
    
    			HRESULT _hr = Windows::Foundation::ActivateInstance< ComPtr<T>>(runtimeClass.Get(), pInstance);
    
    		}

    I added a reference to the C# project, but as I said, the C# class implements a c++ WRL interface from a third project which was also referenced. 

    I think that we need a sample to show how to instantiate a runtime component from C++ without having a WRL interface.  This is possible in C++/CX, so it should be possible in C++. For example, I used nuget to add the microsoft ocr to my non-/CX C++ project. Nuget seems to provide a windmd file and a dll, but no header file. 

    So I think my question boils down to: how do I use C++ to instantiate when all I have is the winmd file and dll? This is a bit of a mystery to me.

    Edit: Looks like I've got a lead: How to: Use winmdidl.exe and midlrt.exe to create .h files from windows metadata



    • Edited by Elliot_ Tuesday, April 7, 2015 4:07 PM
    Tuesday, April 7, 2015 2:41 PM