繁体   English   中英

如何用C#编写的类库引用用C ++编写的Windows运行时组件?

[英]How can Windows Runtime Component written in C++ be referenced from Class Library written in C#?

我正在开发WP8项目,其中包括作为C#源代码的类库项目和作为C ++源代码的Windows运行时组件。 有谁知道是否有可能创建引用Windows运行时组件的C#类库? 最终结果应该是.NET程序集和可用于应用程序的.WIMND / .DLL运行时组件。 目前我无法构建类库,因为它没有看到Windows运行时组件,即使我将它添加到项目中。

更加具体。 我有,例如,MyNs.MyClass.MyMethod(),它在C ++运行时组件中定义,并在C#类库中使用。 目前我因为缺少方法而无法编译C#,尽管我将Windows运行时组件项目附加到同一解决方案中。

虽然我正在接受因为这不是我的区域,但我尝试使用Google搜索“c#call windows runtime component”。 似乎有很多点击/例子,例如第一个是https://msdn.microsoft.com/en-us/library/hh755833.aspx

这对你有帮助吗?

我通过手动将对Windows运行时组件的引用添加到C#类库.csproj文件中解决了这个问题,如下所示

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

我设法制作一个C ++ WRL项目,并通过以正常方式添加引用,从C#项目中使用该项目中的类。 Wrl项目(不是C ++ / 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);
    }
}

使用此类的C#代码:

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);
        }
    }
}

wrl项目的.idl文件:

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;
    } }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM