방법: WRL을 사용하여 Windows 런타임 구성 요소 활성화 및 사용

이 문서에서는 Windows 런타임 C++ WRL(템플릿 라이브러리)을 사용하여 Windows 런타임 초기화하는 방법과 Windows 런타임 구성 요소를 활성화하고 사용하는 방법을 보여 줍니다.

구성 요소를 사용하려면 구성 요소에서 구현하는 형식에 대한 인터페이스 포인터를 획득해야 합니다. 또한 Windows 런타임 기본 기술은 COM(구성 요소 개체 모델)이므로 COM 규칙을 따라 형식의 인스턴스를 기본 확인해야 합니다. 예를 들어 기본 형식이 메모리에서 삭제되는 시기를 결정하는 참조 수를 확인해야 합니다.

Windows 런타임 사용을 간소화하기 위해 Windows 런타임 C++ 템플릿 라이브러리는 참조 계산을 자동으로 수행하는 스마트 포인터 템플릿인 ComPtr<T>를 제공합니다. 변수를 선언할 때 인터페이스 이름>식별자를 지정ComPtr<합니다. 인터페이스 멤버에 액세스하려면 화살표 멤버 액세스 연산자(->)를 식별자에 적용합니다.

Important

인터페이스 함수를 호출할 때 항상 HRESULT 반환 값을 테스트합니다.

Windows 런타임 구성 요소 활성화 및 사용

다음 단계에서는 인터페이스를 Windows::Foundation::IUriRuntimeClass 사용하여 Windows 런타임 구성 요소에 대한 활성화 팩터리를 만들고, 해당 구성 요소의 인스턴스를 만들고, 속성 값을 검색하는 방법을 보여 줍니다. 또한 Windows 런타임 초기화하는 방법도 보여줍니다. 전체 예제는 다음과 같습니다.

Important

일반적으로 UWP(유니버설 Windows 플랫폼) 앱에서 Windows 런타임 C++ 템플릿 라이브러리를 사용하지만 이 예제에서는 그림에 콘솔 앱을 사용합니다. 이러한 wprintf_s 함수는 UWP 앱에서 사용할 수 없습니다. UWP 앱에서 사용할 수 있는 형식 및 함수에 대한 자세한 내용은 유니버설 Windows 플랫폼 앱에서 지원되지 않는 CRT 함수와 UWP 앱용 Win32 및 COM을 참조하세요.

Windows 런타임 구성 요소를 활성화하고 사용하려면

  1. #include필요한 Windows 런타임, Windows 런타임 C++ 템플릿 라이브러리 또는 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::Uninitialize 제거될 때 호출 Windows::Foundation::Initialize 합니다.

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

    두 번째 문 에서 RoInitializeWrapper::HRESULT 연산자는 HRESULT 호출 Windows::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"http://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. do기본 이름을 콘솔에 인쇄하고 반환합니다. 모든 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 문자열의 기본 유니코드 형식을 검색합니다.

전체 예제는 다음과 같습니다.

// 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"http://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-consume-component.cpp 파일에 붙여넣은 다음 Visual Studio 명령 프롬프트 창에서 다음 명령을 실행합니다.

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

참고 항목

Windows 런타임 C++ 템플릿 라이브러리(WRL)