2 COM questions

Started by
0 comments, last by remi 20 years ago
Hi all. I have 2 questions concerning COM. 1)Is it any problem Initializing COM many times without Uninitializing it in an application? 2)Is it a way of knowing whether COM has already been initialized?
"...and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces."----------Scott Meyers, "Effective C++"
Advertisement
quote:To close the COM library gracefully, each successful call to CoInitialize or CoInitializeEx, including those that return S_FALSE, must be balanced by a corresponding call to CoUninitialize.


As far as I know there is no decent way of knowing wether COM has already been initialized or not.

Personally I use a small helper-class for making sure the initializations/uninitializations match up:
class Com {public:  Com() {    HRESULT hr = CoInitialize();    if ( FAILED ( hr ) )      throw hr;  }  ~Com() {    CoUninitialize();  }};


Depending on where you use COM calls, you can get away with creating a single global instance of this class in your application, and COM will initialize and uninitialize properly.

If you use any COM calls in the constructors of any global objects though, you should probably give those global objects a Com object as a member object, as the order of initialization of global objects across compilation-units is undefined.

- Neophyte

This topic is closed to new replies.

Advertisement