WRL 类库项目模板

如果您使用 Visual Studio 编写 Windows 运行时 C++ 模板库 (WRL) 项目中,可以通过下载 WRL 选件类库项目模板大大简化您的任务。

说明说明

如果必须手动更新现有项目的项目设置,请参见 DLL (C++/CX)

下载 WRL 项目模板

Visual Studio 为 WRL 项目不提供了一个模板。这是如何下载用 WRL创建 Windows 应用商店 apps 的基本选件类库的项目模板。

下载 WRL 项目模板

  1. 在菜单栏上,选择**“文件”“新建项目”**。

  2. 新建项目 对话框的左窗格中,选择" 联机,然后选择 模板

  3. 在右上角的 联机搜索模板 框中,键入 WRL 选件类库。当模板显示搜索结果时,请选择 确定 按钮。

  4. 下载并安装 对话框,因此,如果同意权限时间限制,请选择 安装 按钮。

  5. 在模板中安装后,通过选择 文件新建项目,然后选择创建项目 WRLClassLibrary 模板。项目创建 DLL。

使用项目模板的示例

读取使用此模板创建 Windows 运行时 元素的示例 演练:使用 WRL 创建基本 Windows 运行时组件

哪些项目模板提供

项目模板提供:

  • 声明一个基本接口的 MIDL 属性其选件类实现的 .idl 文件。这是一个示例。

    import "inspectable.idl";
    import "Windows.Foundation.idl";
    
    #define COMPONENT_VERSION 1.0
    
    namespace WRLClassLibrary
    {
        interface IWinRTClass;
        runtimeclass WinRTClass;
    
        [uuid(89656677-8679-477c-aff1-f724c64b70f3), version(COMPONENT_VERSION), exclusiveto(WinRTClass)] 
        interface IWinRTClass : IInspectable
        {
        }
    
        [version(COMPONENT_VERSION), activatable(COMPONENT_VERSION)]
        runtimeclass WinRTClass
        {
            [default] interface IWinRTClass;
        }
    }
    
  • 定义选件类实现的 .cpp 文件。这是一个示例。

    #include "pch.h"
    
    #include "WRLClassLibrary_h.h"
    #include <wrl.h>
    
    using namespace Microsoft::WRL;
    using namespace Windows::Foundation;
    
    namespace ABI
    {
        namespace WRLClassLibrary
        {
            class WinRTClass: public RuntimeClass<IWinRTClass>
            {
                InspectableClass(L"WRLClassLibrary.WinRTClass", BaseTrust)
    
            public:
                WinRTClass()
                {
                }
            };
    
            ActivatableClass(WinRTClass);
        }
    }
    

    RuntimeClass 基类帮助管理全局引用模块中的所有对象并声明 IUnknownIInspectable 接口的方法。InspectableClass 宏实现 IUnknownIInspectableActivatableClass 宏创建选件类的实例的选件类工厂。

  • 文件名为定义库导出 DllMainDllCanUnloadNowDllGetActivationFactoryDllGetClassObject的 module.cpp。

请参见

概念

Windows 运行时 C++ 模板库 (WRL)