windows C++-直接实例化 WRL 组件

通过直接实例化组件,可以在不需要类工厂或其他机制时降低开销。 可以直接在通用 Windows 平台应用和桌面应用中实例化组件。

本文档演示了两个示例。 第一个示例使用 Make 函数实例化组件。 第二个示例使用 MakeAndInitialize 函数实例化可能在构造过程中失败的组件。 由于 COM 通常使用 HRESULT 值(而不是异常)来指示错误,因此 COM 类型通常不会从其构造函数引发。MakeAndInitialize 使组件能够通过 RuntimeClassInitialize 方法验证其构造自变量。这两个示例通过定义将消息写入控制台的类来定义基本记录器接口并实现该接口。

不能使用 new 运算符实例化 Windows 运行时 C++ 模板库组件。 因此,建议始终使用 Make 或 MakeAndInitialize 直接实例化组件。

创建和实例化基本记录器组件

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.
*/
处理基本记录器组件的构造失败

使用以下代码替换 CConsoleWriter 类的定义。 此版本具有一个私有字符串成员变量并重写 RuntimeClass::RuntimeClassInitialize 方法。 如果调用 SHStrDup 失败,则 RuntimeClassInitialize 失败。

// 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);
    }
};

使用下面的代码替换 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.
*/

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用OSG库读取wrl文件并显示,可以按照以下步骤操作: 1. 安装OSG库。可以从OSG官网下载安装包进行安装。 2. 编写C++程序,包含以下头文件: ```c++ #include <osg/Group> #include <osgDB/ReadFile> #include <osgViewer/Viewer> ``` 3. 在程序中创建osgViewer::Viewer对象,并设置窗口的大小和标题: ```c++ osgViewer::Viewer viewer; viewer.setUpViewInWindow(50, 50, 800, 600); viewer.setCameraManipulator(new osgGA::TrackballManipulator()); viewer.getCamera()->setClearColor(osg::Vec4(0.9f, 0.9f, 0.9f, 1.0f)); viewer.realize(); ``` 4. 使用osgDB::readNodeFile函数读取wrl文件,并将其添加到osg::Group对象中: ```c++ osg::ref_ptr<osg::Node> root = osgDB::readNodeFile("path/to/file.wrl"); if (root.valid()) { osg::ref_ptr<osg::Group> scene = new osg::Group; scene->addChild(root.get()); viewer.setSceneData(scene.get()); } ``` 5. 运行程序,即可在窗口中显示wrl文件的内容。 完整的代码示例: ```c++ #include <osg/Group> #include <osgDB/ReadFile> #include <osgViewer/Viewer> #include <osgGA/TrackballManipulator> int main(int argc, char** argv) { osgViewer::Viewer viewer; viewer.setUpViewInWindow(50, 50, 800, 600); viewer.setCameraManipulator(new osgGA::TrackballManipulator()); viewer.getCamera()->setClearColor(osg::Vec4(0.9f, 0.9f, 0.9f, 1.0f)); viewer.realize(); osg::ref_ptr<osg::Node> root = osgDB::readNodeFile("path/to/file.wrl"); if (root.valid()) { osg::ref_ptr<osg::Group> scene = new osg::Group; scene->addChild(root.get()); viewer.setSceneData(scene.get()); } return viewer.run(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值