Is this a good way to manage initializations of COM?

Posted by BillyONeal on Stack Overflow See other posts from Stack Overflow or by BillyONeal
Published on 2010-03-12T03:58:05Z Indexed on 2010/03/12 4:07 UTC
Read the original article Hit count: 356

Filed under:
|
|
|
|

Hello everyone :)

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about com