如何:使用 WRL 激活和使用 Windows 运行时组件

本文档演示如何使用 Windows 运行时 C++ 模板库 (WRL) 初始化 Windows 运行时 和激活和使用 Windows 运行时 元素。

若要使用元素,必须先获取接口指针到该组件实现的类型。并且,由于 Windows 运行时 的基础技术是组件对象模型 (COM),必须遵循 COM 规则维护该类型的实例。例如,必须维护确定的 引用计数 该类型时从内存中删除。

为了简化使用 Windows 运行时,WRL 提供了智能指针模板,ComPtr<T>,自动执行引用计数。在声明变量时,请指定 ComPtr<接口名称>标识符。访问接口成员,应用箭头成员访问运算符 (->) 于该标识符。

重要说明重要事项

当您调用界面功能时,始终测试 HRESULT 返回值。

激活和使用窗口运行时组件。

以下步骤使用 Windows::Foundation::IUriRuntimeClass 接口演示如何创建 Windows 运行时 元素开始工厂,创建该元素实例,并检索属性值。它们还演示如何初始化 Windows 运行时。下面是完整的示例。

警告说明警告

虽然可以在 Windows 应用商店 应用程序通常使用 WRL,此示例使用图的控件个 app。功能 (如 wprintf_s 从 Windows 应用商店 app 不可用。有关在 Windows 应用商店 app 可以使用和函数的类型的更多 inforomation,请参见 CRT 函数不支持与 /ZWWin32 和 COM windows 中的 apps

  1. 包括 (#include) 所需的 Windows 运行时、WRL或标准 C++ 库标头。

    #include <Windows.Foundation.h>
    #include <wrl\wrappers\corewrappers.h>
    #include <wrl\client.h>
    #include <stdio.h>
    
    using namespace ABI::Windows::Foundation;
    using namespace Microsoft::WRL;
    using namespace Microsoft::WRL::Wrappers;
    

    建议您使用中的 .cpp 文件中 using namespace 命名空间指令使代码更具可读性。

  2. 初始化该应用程序执行的线程。每个应用程序必须初始化其线程和线程模型。此示例使用 Microsoft::WRL::Wrappers::RoInitializeWrapper 选件类初始化 Windows 运行时 并指定 RO_INIT_MULTITHREADED 作为线程模型。当销毁时,RoInitializeWrapper 选件类在构造调用 Windows::Foundation::InitializeWindows::Foundation::Uninitialize 它。

    // Initialize the Windows Runtime.
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
    if (FAILED(initialize))
    {
        return PrintError(__LINE__, initialize);
    }
    

    在第二个语句,RoInitializeWrapper::HRESULT 运算符调用返回的 HRESULTWindows::Foundation::Initialize

  3. 创建 ABI::Windows::Foundation::IUriRuntimeClassFactory 接口的 启动工厂。

    // Get the activation factory for the IUriRuntimeClass interface.
    ComPtr<IUriRuntimeClassFactory> uriFactory;
    HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Foundation_Uri).Get(), &uriFactory);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }
    

    Windows 运行时 使用完全限定名来标识类型。RuntimeClass_Windows_Foundation_Uri 参数是 Windows 运行时 提供并包含需的运行时类名的字符串。

  4. 初始化表示 URI **"https://www.microsoft.com"**的 Microsoft::WRL::Wrappers::HString 变量。

    // Create a string that represents a URI.
    HString uriHString;
    hr = uriHString.Set(L"https://www.microsoft.com");
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }
    

    在 Windows 运行时,则不分配 Windows 运行时 要使用的字符串的内存。相反,Windows 运行时 在为操作维护和使用的缓冲区创建自己的字符串的副本,然后将处理返回到其创建的缓冲区。

  5. 使用 IUriRuntimeClassFactory::CreateUri 工厂方法创建 ABI::Windows::Foundation::IUriRuntimeClass 对象。

    // Create the IUriRuntimeClass object.
    ComPtr<IUriRuntimeClass> uri;
    hr = uriFactory->CreateUri(uriHString.Get(), &uri);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }
    
  6. 调用 IUriRuntimeClass::get_Domain 方法检索 Domain 属性的值。

    // Get the ___domain part of the URI.
    HString domainName;
    hr = uri->get_Domain(domainName.GetAddressOf());
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }
    
  7. 打印域名到控件中并返回。所有 ComPtr 和 RAII 对象离开范围和自动释放。

    // Print the ___domain name and return.
    wprintf_s(L"Domain name: %s\n", domainName.GetRawBuffer(nullptr));
    
    // All smart pointers and RAII objects go out of scope here.
    

    函数 WindowsGetStringRawBuffer 检索 URI 字符串的基础 Unicode 窗体。

这是完整示例:

// wrl-consume-component.cpp
// compile with: runtimeobject.lib
#include <Windows.Foundation.h>
#include <wrl\wrappers\corewrappers.h>
#include <wrl\client.h>
#include <stdio.h>

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

// Prints an error string for the provided source code line and HRESULT
// value and returns the HRESULT value as an int.
int PrintError(unsigned int line, HRESULT hr)
{
    wprintf_s(L"ERROR: Line:%d HRESULT: 0x%X\n", line, hr);
    return hr;
}

int wmain()
{
    // Initialize the Windows Runtime.
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
    if (FAILED(initialize))
    {
        return PrintError(__LINE__, initialize);
    }

    // Get the activation factory for the IUriRuntimeClass interface.
    ComPtr<IUriRuntimeClassFactory> uriFactory;
    HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Foundation_Uri).Get(), &uriFactory);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    // Create a string that represents a URI.
    HString uriHString;
    hr = uriHString.Set(L"https://www.microsoft.com");
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    // Create the IUriRuntimeClass object.
    ComPtr<IUriRuntimeClass> uri;
    hr = uriFactory->CreateUri(uriHString.Get(), &uri);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    // Get the ___domain part of the URI.
    HString domainName;
    hr = uri->get_Domain(domainName.GetAddressOf());
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    // Print the ___domain name and return.
    wprintf_s(L"Domain name: %s\n", domainName.GetRawBuffer(nullptr));

    // All smart pointers and RAII objects go out of scope here.
}
/*
Output:
Domain name: microsoft.com
*/

编译代码

若要编译代码,请将其复制并粘贴到 Visual Studio 项目或一个名为 wrl 使用 component.cpp 然后运行在 Visual Studio 命令提示符窗口中运行以下命令的文件。

cl.exe wrl-consume-component.cpp runtimeobject.lib

请参见

概念

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