Share via


如何:直接執行個體化 WRL 元件

瞭解如何使用 Windows 執行階段 C++ 樣板庫 (WRL) Microsoft::WRL::Make 和 Microsoft::WRL::D etails::MakeAndInitialize 函式,從定義它的模組具現化元件。

藉由直接具現化元件,您可以在不需要類別處理站或其他機制時降低額外負荷。 您可以直接在通用 Windows 平臺應用程式和傳統型應用程式中具現化元件。

若要瞭解如何使用 Windows 執行階段 C++ 樣板程式庫來建立傳統 COM 元件,並從外部傳統型應用程式具現化它,請參閱 如何:建立傳統 COM 元件

本檔顯示兩個範例。 第一個範例會使用 函 Make 式來具現化元件。 第二個範例會使用 函 MakeAndInitialize 式來具現化建構期間可能會失敗的元件。 (因為 COM 通常會使用 HRESULT 值,而不是例外狀況來表示錯誤,因此 COM 類型通常不會從其建構函式擲回。 MakeAndInitialize 可讓元件透過 RuntimeClassInitialize 方法驗證其建構引數。這兩個範例都會定義基本的記錄器介面,並定義將訊息寫入主控台的類別,以實作該介面。

重要

您無法使用 new 運算子來具現化 C++ 樣板庫元件Windows 執行階段。 因此,我們建議您一律使用 MakeMakeAndInitialize 來直接具現化元件。

建立和具現化基本記錄器元件

  1. 在 Visual Studio 中,建立 Win32 主控台應用程式 專案。 將專案命名為 ,例如 WRLLogger

  2. 將 Midl 檔案 (.idl) 檔案新增至專案、將檔案 ILogger.idl 命名為 ,然後新增下列程式碼:

    import "ocidl.idl";
    
    // Prints text to the console.
    [uuid(AFDB9683-F18A-4B85-90D1-B6158DAFA46C)]
    interface ILogger : IUnknown
    {
        HRESULT Log([in] LPCWSTR text);
    }
    
  3. 使用下列程式碼取代 的內容 WRLLogger.cpp

    #include "pch.h" // Use stdafx.h in Visual Studio 2017 and earlier
    #include <wrl\implements.h>
    #include <comutil.h>
    
    #include "ILogger_h.h"
    
    using namespace Microsoft::WRL;
    
    // Writes logging messages to the console.
    class CConsoleWriter : public RuntimeClass<RuntimeClassFlags<ClassicCom>, ILogger>
    {
    public:
        STDMETHODIMP Log(_In_ PCWSTR text)
        {
            wprintf_s(L"%s\n", text);
            return S_OK;
        }
    
    private:
        // Make destroyable only through Release.
        ~CConsoleWriter()
        {
        }
    };
    
    int wmain()
    {
        ComPtr<CConsoleWriter> writer = Make<CConsoleWriter>();
        HRESULT hr = writer->Log(L"Logger ready.");
        return hr;
    }
    
    /* Output:
    Logger ready.
    */
    

處理基本記錄器元件的建構失敗

  1. 使用下列程式碼取代 類別的定義 CConsoleWriter 。 此版本會保存私用字串成員變數,並覆寫 RuntimeClass::RuntimeClassInitialize 方法。 RuntimeClassInitialize 如果呼叫 SHStrDup 失敗,就會失敗。

    // Writes logging messages to the console.
    class CConsoleWriter : public RuntimeClass<RuntimeClassFlags<ClassicCom>, ILogger>
    {
    public:
        // Initializes the CConsoleWriter object.
        // Failure here causes your object to fail construction with the HRESULT you choose.
        HRESULT RuntimeClassInitialize(_In_ PCWSTR category)
        {
            return SHStrDup(category, &m_category);
        }
    
        STDMETHODIMP Log(_In_ PCWSTR text)
        {
            wprintf_s(L"%s: %s\n", m_category, text);
            return S_OK;
        }
    
    private:
        PWSTR m_category;
    
        // Make destroyable only through Release.
        ~CConsoleWriter()
        {
            CoTaskMemFree(m_category);
        }
    };
    
  2. 使用下列程式碼取代 的定義 wmain 。 此版本會使用 MakeAndInitialize 來具現化 CConsoleWriter 物件,並檢查 HRESULT 結果。

    int wmain()
    {
        ComPtr<CConsoleWriter> writer;
        HRESULT hr = MakeAndInitialize<CConsoleWriter>(&writer, L"INFO");
        if (FAILED(hr))
        {
            wprintf_s(L"Object creation failed. Result = 0x%x", hr);
            return hr;
        }
        hr = writer->Log(L"Logger ready.");
        return hr;
    }
    
    /* Output:
    INFO: Logger ready.
    */
    

另請參閱

Windows 執行階段 C++ 範本庫 (WRL)
Microsoft::WRL::Make
Microsoft::WRL::D etails::MakeAndInitialize