这是管理 COM 初始化的好方法吗?
我对涉及组件对象模型的任何事情都很陌生,我想知道这种管理对 CoInitalize/CoUninitalize 调用的方法是否有意义:
COM.hpp:
#pragma once
namespace WindowsAPI { namespace ComponentObjectModel {
class COM
{
COM();
~COM();
public:
static void Setup();
};
}}
COM.cpp:
#include <Windows.h>
#include "COM.hpp"
namespace WindowsAPI { namespace ComponentObjectModel {
COM::COM()
{
if (CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) != S_OK) throw std::runtime_error("Couldn't start COM!");
}
COM::~COM()
{
CoUninitialize();
}
void COM::Setup()
{
static COM instance;
}
}}
然后任何需要 COM 的组件都只需调用 COM ::Setup()
并忘记了它。
这有意义还是我违反了 COM 的任何“规则”?
I'm very new to anything involving Component Object Model, and I'm wondering if this method of managing calls to CoInitalize/CoUninitalize makes sense:
COM.hpp:
#pragma once
namespace WindowsAPI { namespace ComponentObjectModel {
class COM
{
COM();
~COM();
public:
static void Setup();
};
}}
COM.cpp:
#include <Windows.h>
#include "COM.hpp"
namespace WindowsAPI { namespace ComponentObjectModel {
COM::COM()
{
if (CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) != S_OK) throw std::runtime_error("Couldn't start COM!");
}
COM::~COM()
{
CoUninitialize();
}
void COM::Setup()
{
static COM instance;
}
}}
Then any component that needs COM just calls COM::Setup()
and forgets about it.
Does this make sense or am I breaking any "rules" of COM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不相信静态存储变量会在 dll 卸载时被破坏,但无论如何你都不应该从 dll 中使用它。
我通常会做类似的事情,但我不关心静态函数,我只是将 ctor/dtor 公开并在 main() 中删除一个实例:
I don't believe that static storage variables are destroyed on dll unload, but you shouldn't be using this from a dll anyway.
I generally do something similar, but I don't bother with a function static, I just make the ctor/dtor public and drop an instance in my main():