1 #ifndef _COMINITIALIZER_H_8B5ACB47_D183_469b_AE67_B8785F9177EF
2 #define _COMINITIALIZER_H_8B5ACB47_D183_469b_AE67_B8785F9177EF
4 #include "ErrorReporter.h"
7 #define _WIN32_DCOM // Required for CoInitializeEx()
17 #ifndef COM_THREADING_MODEL
18 #define COM_THREADING_MODEL COINIT_APARTMENTTHREADED
84 Initializer& operator=(
const Initializer&);
86 enum INIT_STATE {NOTYET = -1, INITFAILED = 0, INITSUCCEEDED = 1};
88 INIT_STATE m_nInitState;
90 inline Initializer(
const Initializer& other)
92 m_nInitState = NOTYET;
97 m_nInitState = NOTYET;
99 inline void InitializeCOM(DWORD dwThreadID)
102 if(INITFAILED == (m_nInitState = SUCCEEDED(hr = CoInitializeEx(NULL, COM_THREADING_MODEL)) ? INITSUCCEEDED : INITFAILED))
104 if(hr == E_OUTOFMEMORY)
105 Err::ErrorMessage(_T(
"Out of Memory Error Occured While Initializing COM !!"));
107 Err::ErrorMessage(_T(
"Unable to Initialize COM !!"));
110 inline ~Initializer()
112 if(m_nInitState == INITSUCCEEDED)
115 m_nInitState = NOTYET;
118 inline INIT_STATE InitState()
const {
return m_nInitState; }
125 typedef std::map<DWORD, Initializer> INITOBJECTMAP;
127 static INITOBJECTMAP m_InitObjectMap;
129 DWORD dwCurrentThreadID = GetCurrentThreadId();
131 Initializer& Init = m_InitObjectMap[dwCurrentThreadID];
133 if(Init.InitState() == Initializer::NOTYET)
134 Init.InitializeCOM(dwCurrentThreadID);
162 m_bInitialized = SUCCEEDED(hr = CoInitializeEx(NULL, COM_THREADING_MODEL));
164 if(m_bInitialized ==
false)
166 if(hr == E_OUTOFMEMORY)
167 Err::ErrorMessage(_T(
"Out of Memory Error Occured While Initializing COM !!"));
169 Err::ErrorMessage(_T(
"Unable to Initialize COM !!"));
177 m_bInitialized =
false;
184 static Initializer Init;