Share via


如何:使用 WRL 建立傳統 COM 元件

除了將它用於 通用 Windows 平臺 (UWP) 應用程式之外,您還可以使用 Windows 執行階段 C++ 樣板程式庫 (WRL) 來建立基本的傳統 COM 元件,以用於傳統型應用程式。 若要建立 COM 元件,Windows 執行階段 C++ 樣板庫可能需要比 ATL 更少的程式碼。 如需Windows 執行階段 C++ 樣板庫所支援 COM 子集的相關資訊,請參閱 Windows 執行階段 C++ 樣板庫 (WRL)

本檔說明如何使用 Windows 執行階段 C++ 樣板程式庫來建立基本 COM 元件。 雖然您可以使用最符合您需求的部署機制,這份文件也顯示註冊和使用桌面應用程式之 COM 元件的基本方法。

若要使用 Windows 執行階段 C++ 樣板庫來建立基本傳統 COM 元件

  1. 在 Visual Studio 中,建立空白 方案 專案。 將專案命名為 WRLClassicCOM ,例如 。

  2. 將 Win32 專案 新增至方案。 將專案命名為 CalculatorComponent ,例如 。 在 [ 應用程式設定] 索引標籤上,選取 [DLL ]。

  3. 將 Midl 檔案 (.idl) 檔案新增至專案。 將檔案命名為 , CalculatorComponent.idl 例如 。

  4. 將此程式碼加入至 CalculatorComponent.idl:

    import "ocidl.idl";
    
    [uuid(0DBABB94-CE99-42F7-ACBD-E698B2332C60), version(1.0)] 
    interface ICalculatorComponent : IUnknown
    {
        HRESULT Add([in] int a, [in] int b, [out, retval] int* value);
    }
    
    [uuid(9D3E6826-CB8E-4D86-8B14-89F0D7EFCD01), version(1.0)]
    library CalculatorComponentLib
    {
        [uuid(E68F5EDD-6257-4E72-A10B-4067ED8E85F2), version(1.0)]
        coclass CalculatorComponent
        {
            [default] interface ICalculatorComponent;
        }
    };
    
  5. 在 CalculatorComponent.cpp 中定義 CalculatorComponent 類別。 類別 CalculatorComponent 繼承自 Microsoft::WRL::RuntimeClass Microsoft::WRL::RuntimeClassFlags < ClassicCom > 指定類別衍生自 IUnknown ,而不是 IInspectable 。 ( IInspectable 僅適用于Windows 執行階段應用程式元件。) CoCreatableClass 會為類別建立一個處理站,可與 CoCreateInstance 函式搭配使用。

    #include "pch.h" // Use stdafx.h in Visual Studio 2017 and earlier
    
    #include "CalculatorComponent_h.h"
    #include <wrl.h>
    
    using namespace Microsoft::WRL;
    
    class CalculatorComponent: public RuntimeClass<RuntimeClassFlags<ClassicCom>, ICalculatorComponent>
    {
    public:
        CalculatorComponent()
        {
        }
    
        STDMETHODIMP Add(_In_ int a, _In_ int b, _Out_ int* value)
        {
            *value = a + b;
            return S_OK;
        }
    };
    
    CoCreatableClass(CalculatorComponent);
    
  6. 使用下列程式碼取代 中的 dllmain.cpp 程式碼。 此檔案會定義 DLL 匯出函式。 這些函式會使用 Microsoft::WRL::Module 類別來管理模組的類別處理站。

    #include "pch.h" // Use stdafx.h in Visual Studio 2017 and earlier
    #include <wrl\module.h>
    
    using namespace Microsoft::WRL;
    
    #if !defined(__WRL_CLASSIC_COM__)
    STDAPI DllGetActivationFactory(_In_ HSTRING activatibleClassId, _COM_Outptr_ IActivationFactory** factory)
    {
        return Module<InProc>::GetModule().GetActivationFactory(activatibleClassId, factory);
    }
    #endif
    
    #if !defined(__WRL_WINRT_STRICT__)
    STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, _COM_Outptr_ void** ppv)
    {
        return Module<InProc>::GetModule().GetClassObject(rclsid, riid, ppv);
    }
    #endif
    
    STDAPI DllCanUnloadNow()
    {
        return Module<InProc>::GetModule().Terminate() ? S_OK : S_FALSE;
    }
    
    STDAPI_(BOOL) DllMain(_In_opt_ HINSTANCE hinst, DWORD reason, _In_opt_ void*)
    {
        if (reason == DLL_PROCESS_ATTACH)
        {
            DisableThreadLibraryCalls(hinst);
        }
        return TRUE;
    }
    
  7. Module-Definition 檔案 (.def) 檔案新增至專案。 將檔案命名為 , CalculatorComponent.def 例如 。 此檔案可將連結器命名為要匯出的函式名稱。 開啟專案的 [ 屬性頁] 對話方塊,然後在 [組態屬性 > 連結器 > 輸入 ] 下 ,將 [模組定義檔 ] 屬性設定為 DEF 檔案。

  8. 將此程式碼加入至 CalculatorComponent.def:

    LIBRARY
    
    EXPORTS
        DllGetActivationFactory PRIVATE
        DllGetClassObject       PRIVATE
        DllCanUnloadNow         PRIVATE
    
  9. 將 runtimeobject.lib 加入至連結器列。 若要瞭解如何,請參閱 .Lib 檔案作為連結器輸入

使用桌面應用程式的 COM 元件

  1. 使用 Windows 登錄註冊 COM 元件。 若要這樣做,請建立註冊專案檔案、將它命名為 RegScript.reg ,然後新增下列文字。 以 DLL 的路徑取代 dll-path > ,例如 C:\temp\WRLClassicCOM\Debug\CalculatorComponent.dll 。 <

    Windows Registry Editor Version 5.00
    
    [HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{E68F5EDD-6257-4E72-A10B-4067ED8E85F2}]
    @="CalculatorComponent Class"
    
    [HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{E68F5EDD-6257-4E72-A10B-4067ED8E85F2}\InprocServer32]
    @="<dll-path>"
    "ThreadingModel"="Apartment"
    
    [HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{E68F5EDD-6257-4E72-A10B-4067ED8E85F2}\Programmable]
    
    [HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{E68F5EDD-6257-4E72-A10B-4067ED8E85F2}\TypeLib]
    @="{9D3E6826-CB8E-4D86-8B14-89F0D7EFCD01}"
    
    [HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{E68F5EDD-6257-4E72-A10B-4067ED8E85F2}\Version]
    @="1.0"
    
  2. 執行 RegScript.reg,或將它新增至專案的 建置後事件 。 如需詳細資訊,請參閱 建置前事件/建置後事件命令列對話方塊

  3. 將 Win32 主控台應用程式 專案新增至解決方案。 將專案命名為 Calculator ,例如 。

  4. 使用此程式碼取代 的內容 Calculator.cpp

    #include "pch.h" // Use stdafx.h in Visual Studio 2017 and earlier
    
    #include "..\CalculatorComponent\CalculatorComponent_h.h"
    
    const IID IID_ICalculatorComponent = {0x0DBABB94,0xCE99,0x42F7,0xAC,0xBD,0xE6,0x98,0xB2,0x33,0x2C,0x60};
    const CLSID CLSID_CalculatorComponent = {0xE68F5EDD,0x6257,0x4E72,0xA1,0x0B,0x40,0x67,0xED,0x8E,0x85,0xF2};
    
    // 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()
    {
        HRESULT hr;
    
        // Initialize the COM library.
        hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
        if (FAILED(hr))
        {
            return PrintError(__LINE__, hr);
        }
    
        ICalculatorComponent* calc = nullptr; // Interface to COM component.
    
        // Create the CalculatorComponent object.
        hr = CoCreateInstance(CLSID_CalculatorComponent, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&calc));
        if (SUCCEEDED(hr))
        {
            // Test the component by adding two numbers.
            int result;
            hr = calc->Add(4, 5, &result);
            if (FAILED(hr))
            {
                PrintError(__LINE__, hr);
            }
            else
            {
                wprintf_s(L"result = %d\n", result);
            }
    
            // Free the CalculatorComponent object.
            calc->Release();
        }
        else
        {
            // Object creation failed. Print a message.
            PrintError(__LINE__, hr);
        }
    
        // Free the COM library.
        CoUninitialize();
    
        return hr;
    }
    /* Output:
    result = 9
    */
    

穩固程式設計

本檔使用標準 COM 函式來示範您可以使用 Windows 執行階段 C++ 樣板庫來撰寫 COM 元件,並讓它可供任何啟用 COM 的技術使用。 您也可以在傳統型應用程式中使用Windows 執行階段 C++ 樣板程式庫類型,例如 Microsoft::WRL::ComPtr 來管理 COM 和其他物件的存留期。 下列程式碼會使用 Windows 執行階段 C++ 樣板庫來管理指標的 ICalculatorComponent 存留期。 CoInitializeWrapper 類別是 RAII 包裝函式,可保證釋放 COM 程式庫,也可保證 COM 程式庫的存留期超過 ComPtr 智慧型指標物件的存留期。

#include "pch.h" // Use stdafx.h in Visual Studio 2017 and earlier
#include <wrl.h>

#include "..\CalculatorComponent\CalculatorComponent_h.h"

using namespace Microsoft::WRL;

const IID IID_ICalculatorComponent = {0x0DBABB94,0xCE99,0x42F7,0xAC,0xBD,0xE6,0x98,0xB2,0x33,0x2C,0x60};
const CLSID CLSID_CalculatorComponent = {0xE68F5EDD,0x6257,0x4E72,0xA1,0x0B,0x40,0x67,0xED,0x8E,0x85,0xF2};

// 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()
{
    HRESULT hr;

    // RAII wrapper for managing the lifetime of the COM library.
    class CoInitializeWrapper
    {
        HRESULT _hr;
    public:
        CoInitializeWrapper(DWORD flags)
        {
            _hr = CoInitializeEx(nullptr, flags);
        }
        ~CoInitializeWrapper()
        {
            if (SUCCEEDED(_hr))
            {
                CoUninitialize();
            }
        }
        operator HRESULT()
        {
            return _hr;
        }

    };

    // Initialize the COM library.
    CoInitializeWrapper initialize(COINIT_APARTMENTTHREADED);
    if (FAILED(initialize))
    {
        return PrintError(__LINE__, initialize);
    }

    ComPtr<ICalculatorComponent> calc; // Interface to COM component.

    // Create the CalculatorComponent object.
    hr = CoCreateInstance(CLSID_CalculatorComponent, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(calc.GetAddressOf()));
    if (SUCCEEDED(hr))
    {
        // Test the component by adding two numbers.
        int result;
        hr = calc->Add(4, 5, &result);
        if (FAILED(hr))
        {
            return PrintError(__LINE__, hr);
        }
        wprintf_s(L"result = %d\n", result);
    }
    else
    {
        // Object creation failed. Print a message.
        return PrintError(__LINE__, hr);
    }

    return 0;
}

另請參閱

Windows 執行階段 C++ 範本庫 (WRL)