How does a Windows Runtime component written in C ++ refer to a class library written in C #?

I am working on a WP8 project that includes a class library project as C # source code and a Windows Runtime component as C ++ source code. Does anyone know if it is possible to create a C # class library that will reference a Windows Runtime component? The end result should be a .NET assembly and a .WIMND / .DLL runtime component that can be used for the application. Currently, I cannot create the class library because it does not see the Windows Runtime component, although I added it to the project.

More specific. I have, say, MyNs.MyClass.MyMethod (), which is defined in the C ++ runtime component and is used from the C # class library. Currently, I cannot compile C # due to a missing method, although I have a Windows runtime component component attached to the same solution.

+4
source share
3 answers

I solved this by adding a link to the Windows runtime component manually in the .csproj file of the C # class library as follows

...
    <ItemGroup>
        <Reference Include="WindowsRuntimeComponent.winmd" />
    </ItemGroup>
...
0
source

, , Googling " # call windows". , /, . https://msdn.microsoft.com/en-us/library/hh755833.aspx.

?

0

++ WRL #, . Wrl ( ++/CX, ) WRL, - . wrl .idl, , .dll .winmd. , :

Wrl:

#include "pch.h"

#include "WrlTestClass2_h.h"
#include <wrl.h>

using namespace Microsoft::WRL;
using namespace Windows::Foundation;

namespace ABI
{
    namespace WrlTestClass2
    {
        class WinRTClass: public RuntimeClass<IWinRTClass>
        {
            InspectableClass(RuntimeClass_WrlTestClass2_WinRTClass, BaseTrust)

            public:
            WinRTClass()
            {
            }

            // http://msdn.microsoft.com/en-us/library/jj155856.aspx
            // Walkthrough: Creating a Basic Windows Runtime Component Using WRL
            HRESULT __stdcall Add(_In_ int a, _In_ int b, _Out_ int* value)
            {
                if (value == nullptr)
                {
                    return E_POINTER;
                }
                *value = a + b;
                return S_OK;
            }

        };

        ActivatableClass(WinRTClass);
    }
}

#, :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;


namespace CSharpClientToWrl
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            WrlTestClass2.WinRTClass _winRtTestClass = new WrlTestClass2.WinRTClass();

            int _answer = _winRtTestClass.Add(4, 6);

            Assert.AreEqual(_answer, 10);
        }
    }
}

.idl wrl:

import "inspectable.idl"; import "Windows.Foundation.idl";

#define COMPONENT_VERSION 1.0

namespace WrlTestClass2 {
    interface IWinRTClass;
    runtimeclass WinRTClass;

    [uuid(0be9429f-2c7a-40e8-bb0a-85bcb1749367), version(COMPONENT_VERSION)] 
    interface IWinRTClass : IInspectable
    {       // http://msdn.microsoft.com/en-us/library/jj155856.aspx        // Walkthrough: Creating a Basic Windows Runtime Component Using WRL        HRESULT Add([in] int a, [in] int b, [out, retval] int* value);
    }

    [version(COMPONENT_VERSION), activatable(COMPONENT_VERSION)]
    runtimeclass WinRTClass
    {
        [default] interface IWinRTClass;
    } }
0

All Articles