ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

atlbase.h
Go to the documentation of this file.
00001 /*
00002  * ReactOS ATL
00003  *
00004  * Copyright 2009 Andrew Hill <ash77@reactos.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #pragma once
00022 
00023 #include "atlcore.h"
00024 #include "statreg.h"
00025 
00026 #ifdef _MSC_VER
00027 // It is common to use this in ATL constructors. They only store this for later use, so the usage is safe.
00028 #pragma warning(disable:4355)
00029 #endif
00030 
00031 #ifndef _ATL_PACKING
00032 #define _ATL_PACKING 8
00033 #endif
00034 
00035 #ifndef _ATL_FREE_THREADED
00036 #ifndef _ATL_APARTMENT_THREADED
00037 #ifndef _ATL_SINGLE_THREADED
00038 #define _ATL_FREE_THREADED
00039 #endif
00040 #endif
00041 #endif
00042 
00043 #ifndef ATLTRY
00044 #define ATLTRY(x) x;
00045 #endif
00046 
00047 #ifdef _ATL_DISABLE_NO_VTABLE
00048 #define ATL_NO_VTABLE
00049 #else
00050 #define ATL_NO_VTABLE __declspec(novtable)
00051 #endif
00052 
00053 #define offsetofclass(base, derived) (reinterpret_cast<DWORD_PTR>(static_cast<base *>(reinterpret_cast<derived *>(_ATL_PACKING))) - _ATL_PACKING)
00054 
00055 namespace ATL
00056 {
00057 
00058 class CAtlModule;
00059 class CComModule;
00060 class CAtlComModule;
00061 __declspec(selectany) CAtlModule            *_pAtlModule = NULL;
00062 __declspec(selectany) CComModule            *_pModule = NULL;
00063 extern CAtlComModule _AtlComModule;
00064 
00065 typedef HRESULT (WINAPI _ATL_CREATORFUNC)(void *pv, REFIID riid, LPVOID *ppv);
00066 typedef LPCTSTR (WINAPI _ATL_DESCRIPTIONFUNC)();
00067 typedef const struct _ATL_CATMAP_ENTRY * (_ATL_CATMAPFUNC)();
00068 
00069 struct _ATL_OBJMAP_ENTRY30
00070 {
00071     const CLSID                             *pclsid;
00072     HRESULT (WINAPI *pfnUpdateRegistry)(BOOL bRegister);
00073     _ATL_CREATORFUNC                        *pfnGetClassObject;
00074     _ATL_CREATORFUNC                        *pfnCreateInstance;
00075     IUnknown                                *pCF;
00076     DWORD                                   dwRegister;
00077     _ATL_DESCRIPTIONFUNC                    *pfnGetObjectDescription;
00078     _ATL_CATMAPFUNC                         *pfnGetCategoryMap;
00079     void (WINAPI *pfnObjectMain)(bool bStarting);
00080 
00081     HRESULT WINAPI RevokeClassObject()
00082     {
00083         if (dwRegister == 0)
00084             return S_OK;
00085         return CoRevokeClassObject(dwRegister);
00086     }
00087 
00088     HRESULT WINAPI RegisterClassObject(DWORD dwClsContext, DWORD dwFlags)
00089     {
00090         IUnknown                            *p;
00091         HRESULT                             hResult;
00092 
00093         p = NULL;
00094         if (pfnGetClassObject == NULL)
00095             return S_OK;
00096 
00097         hResult = pfnGetClassObject(reinterpret_cast<LPVOID *>(pfnCreateInstance), IID_IUnknown, reinterpret_cast<LPVOID *>(&p));
00098         if (SUCCEEDED(hResult))
00099             hResult = CoRegisterClassObject(*pclsid, p, dwClsContext, dwFlags, &dwRegister);
00100 
00101         if (p != NULL)
00102             p->Release();
00103 
00104         return hResult;
00105     }
00106 };
00107 
00108 typedef _ATL_OBJMAP_ENTRY30 _ATL_OBJMAP_ENTRY;
00109 
00110 typedef void (__stdcall _ATL_TERMFUNC)(DWORD_PTR dw);
00111 
00112 struct _ATL_TERMFUNC_ELEM
00113 {
00114     _ATL_TERMFUNC                           *pFunc;
00115     DWORD_PTR                               dw;
00116     _ATL_TERMFUNC_ELEM                      *pNext;
00117 };
00118 
00119 struct _ATL_MODULE70
00120 {
00121     UINT                                    cbSize;
00122     LONG                                    m_nLockCnt;
00123     _ATL_TERMFUNC_ELEM                      *m_pTermFuncs;
00124     CComCriticalSection                     m_csStaticDataInitAndTypeInfo;
00125 };
00126 typedef _ATL_MODULE70 _ATL_MODULE;
00127 
00128 typedef HRESULT (WINAPI _ATL_CREATORARGFUNC)(void *pv, REFIID riid, LPVOID *ppv, DWORD_PTR dw);
00129 
00130 #define _ATL_SIMPLEMAPENTRY ((ATL::_ATL_CREATORARGFUNC *)1)
00131 
00132 struct _ATL_INTMAP_ENTRY
00133 {
00134     const IID                               *piid;
00135     DWORD_PTR                               dw;
00136     _ATL_CREATORARGFUNC                     *pFunc;
00137 };
00138 
00139 struct _AtlCreateWndData
00140 {
00141     void                                    *m_pThis;
00142     DWORD                                   m_dwThreadID;
00143     _AtlCreateWndData                       *m_pNext;
00144 };
00145 
00146 struct _ATL_COM_MODULE70
00147 {
00148     UINT                                    cbSize;
00149     HINSTANCE                               m_hInstTypeLib;
00150     _ATL_OBJMAP_ENTRY                       **m_ppAutoObjMapFirst;
00151     _ATL_OBJMAP_ENTRY                       **m_ppAutoObjMapLast;
00152     CComCriticalSection                     m_csObjMap;
00153 };
00154 typedef _ATL_COM_MODULE70 _ATL_COM_MODULE;
00155 
00156 struct _ATL_WIN_MODULE70
00157 {
00158     UINT                                    cbSize;
00159     CComCriticalSection                     m_csWindowCreate;
00160     _AtlCreateWndData                       *m_pCreateWndList;
00161 #ifdef NOTYET
00162     CSimpleArray<ATOM>                      m_rgWindowClassAtoms;
00163 #endif
00164 };
00165 typedef _ATL_WIN_MODULE70 _ATL_WIN_MODULE;
00166 
00167 struct _ATL_REGMAP_ENTRY
00168 {
00169     LPCOLESTR                               szKey;
00170     LPCOLESTR                               szData;
00171 };
00172 
00173 HRESULT __stdcall AtlWinModuleInit(_ATL_WIN_MODULE *pWinModule);
00174 HRESULT __stdcall AtlWinModuleTerm(_ATL_WIN_MODULE *pWinModule, HINSTANCE hInst);
00175 HRESULT __stdcall AtlInternalQueryInterface(void *pThis, const _ATL_INTMAP_ENTRY *pEntries, REFIID iid, void **ppvObject);
00176 void __stdcall AtlWinModuleAddCreateWndData(_ATL_WIN_MODULE *pWinModule, _AtlCreateWndData *pData, void *pObject);
00177 void *__stdcall AtlWinModuleExtractCreateWndData(_ATL_WIN_MODULE *pWinModule);
00178 HRESULT __stdcall AtlComModuleGetClassObject(_ATL_COM_MODULE *pComModule, REFCLSID rclsid, REFIID riid, LPVOID *ppv);
00179 
00180 template<class TLock>
00181 class CComCritSecLock
00182 {
00183 private:
00184     bool                                        m_bLocked;
00185     TLock                                       &m_cs;
00186 public:
00187     CComCritSecLock(TLock &cs, bool bInitialLock = true) : m_cs(cs)
00188     {
00189         HRESULT                                 hResult;
00190 
00191         m_bLocked = false;
00192         if (bInitialLock)
00193         {
00194             hResult = Lock();
00195             if (FAILED(hResult))
00196             {
00197                 ATLASSERT(false);
00198             }
00199         }
00200     }
00201 
00202     ~CComCritSecLock()
00203     {
00204         if (m_bLocked)
00205             Unlock();
00206     }
00207 
00208     HRESULT Lock()
00209     {
00210         HRESULT                                 hResult;
00211 
00212         ATLASSERT(!m_bLocked);
00213         hResult = m_cs.Lock();
00214         if (FAILED(hResult))
00215             return hResult;
00216         m_bLocked = true;
00217 
00218         return S_OK;
00219     }
00220 
00221     void Unlock()
00222     {
00223         HRESULT                                 hResult;
00224 
00225         ATLASSERT(m_bLocked);
00226         hResult = m_cs.Unlock();
00227         if (FAILED(hResult))
00228         {
00229             ATLASSERT(false);
00230         }
00231         m_bLocked = false;
00232     }
00233 };
00234 
00235 inline BOOL WINAPI InlineIsEqualUnknown(REFGUID rguid1)
00236 {
00237    return (
00238       ((unsigned long *)&rguid1)[0] == 0 &&
00239       ((unsigned long *)&rguid1)[1] == 0 &&
00240       ((unsigned long *)&rguid1)[2] == 0x000000C0 &&
00241       ((unsigned long *)&rguid1)[3] == 0x46000000);
00242 }
00243 
00244 class CComMultiThreadModelNoCS
00245 {
00246 public:
00247     typedef CComFakeCriticalSection AutoCriticalSection;
00248     typedef CComFakeCriticalSection CriticalSection;
00249     typedef CComMultiThreadModelNoCS ThreadModelNoCS;
00250     typedef CComFakeCriticalSection AutoDeleteCriticalSection;
00251 
00252     static ULONG WINAPI Increment(LPLONG p)
00253     {
00254         return InterlockedIncrement(p);
00255     }
00256 
00257     static ULONG WINAPI Decrement(LPLONG p)
00258     {
00259         return InterlockedDecrement(p);
00260     }
00261 };
00262 
00263 class CComMultiThreadModel
00264 {
00265 public:
00266     typedef CComAutoCriticalSection AutoCriticalSection;
00267     typedef CComCriticalSection CriticalSection;
00268     typedef CComMultiThreadModelNoCS ThreadModelNoCS;
00269     typedef CComAutoDeleteCriticalSection AutoDeleteCriticalSection;
00270 
00271     static ULONG WINAPI Increment(LPLONG p)
00272     {
00273         return InterlockedIncrement(p);
00274     }
00275 
00276     static ULONG WINAPI Decrement(LPLONG p)
00277     {
00278         return InterlockedDecrement(p);
00279     }
00280 };
00281 
00282 class CComSingleThreadModel
00283 {
00284 public:
00285     typedef CComFakeCriticalSection AutoCriticalSection;
00286     typedef CComFakeCriticalSection CriticalSection;
00287     typedef CComSingleThreadModel ThreadModelNoCS;
00288     typedef CComFakeCriticalSection AutoDeleteCriticalSection;
00289 
00290     static ULONG WINAPI Increment(LPLONG p)
00291     {
00292         return ++*p;
00293     }
00294 
00295     static ULONG WINAPI Decrement(LPLONG p)
00296     {
00297         return --*p;
00298     }
00299 };
00300 
00301 #if defined(_ATL_FREE_THREADED)
00302 
00303     typedef CComMultiThreadModel CComObjectThreadModel;
00304     typedef CComMultiThreadModel CComGlobalsThreadModel;
00305 
00306 #elif defined(_ATL_APARTMENT_THREADED)
00307 
00308     typedef CComSingleThreadModel CComObjectThreadModel;
00309     typedef CComMultiThreadModel CComGlobalsThreadModel;
00310 
00311 #elif defined(_ATL_SINGLE_THREADED)
00312 
00313     typedef CComSingleThreadModel CComObjectThreadModel;
00314     typedef CComSingleThreadModel CComGlobalsThreadModel;
00315 
00316 #else
00317 #error No threading model
00318 #endif
00319 
00320 class CAtlModule : public _ATL_MODULE
00321 {
00322 protected:
00323     static GUID                             m_libid;
00324 public:
00325     CAtlModule()
00326     {
00327         ATLASSERT(_pAtlModule == NULL);
00328         _pAtlModule = this;
00329         cbSize = sizeof(_ATL_MODULE);
00330         m_nLockCnt = 0;
00331     }
00332 
00333     virtual LONG GetLockCount()
00334     {
00335         return m_nLockCnt;
00336     }
00337 
00338     virtual LONG Lock()
00339     {
00340         return CComGlobalsThreadModel::Increment(&m_nLockCnt);
00341     }
00342 
00343     virtual LONG Unlock()
00344     {
00345         return CComGlobalsThreadModel::Decrement(&m_nLockCnt);
00346     }
00347 
00348     virtual HRESULT AddCommonRGSReplacements(IRegistrarBase* /*pRegistrar*/) = 0;
00349 
00350     HRESULT WINAPI UpdateRegistryFromResource(LPCTSTR lpszRes, BOOL bRegister, struct _ATL_REGMAP_ENTRY *pMapEntries = NULL)
00351     {
00352         CRegObject                              registrar;
00353         TCHAR                                   modulePath[MAX_PATH];
00354         HRESULT                                 hResult;
00355 
00356         hResult = CommonInitRegistrar(registrar, modulePath, sizeof(modulePath) / sizeof(modulePath[0]), pMapEntries);
00357         if (FAILED(hResult))
00358             return hResult;
00359 
00360         if (bRegister != FALSE)
00361             hResult = registrar.ResourceRegisterSz(modulePath, lpszRes, _T("REGISTRY"));
00362         else
00363             hResult = registrar.ResourceUnregisterSz(modulePath, lpszRes, _T("REGISTRY"));
00364 
00365         return hResult;
00366     }
00367 
00368     HRESULT WINAPI UpdateRegistryFromResource(UINT nResID, BOOL bRegister, struct _ATL_REGMAP_ENTRY *pMapEntries = NULL)
00369     {
00370         CRegObject                              registrar;
00371         TCHAR                                   modulePath[MAX_PATH];
00372         HRESULT                                 hResult;
00373 
00374         hResult = CommonInitRegistrar(registrar, modulePath, sizeof(modulePath) / sizeof(modulePath[0]), pMapEntries);
00375         if (FAILED(hResult))
00376             return hResult;
00377 
00378         if (bRegister != FALSE)
00379             hResult = registrar.ResourceRegister(modulePath, nResID, _T("REGISTRY"));
00380         else
00381             hResult = registrar.ResourceUnregister(modulePath, nResID, _T("REGISTRY"));
00382 
00383         return hResult;
00384     }
00385 
00386 private:
00387     HRESULT CommonInitRegistrar(CRegObject &registrar, TCHAR *modulePath, DWORD modulePathCount, struct _ATL_REGMAP_ENTRY *pMapEntries)
00388     {
00389         HINSTANCE                               hInstance;
00390         DWORD                                   dwFLen;
00391         HRESULT                                 hResult;
00392 
00393         hInstance = _AtlBaseModule.GetModuleInstance();
00394         dwFLen = GetModuleFileName(hInstance, modulePath, modulePathCount);
00395         if (dwFLen == modulePathCount)
00396             return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
00397         else if (dwFLen == 0)
00398             return HRESULT_FROM_WIN32(GetLastError());
00399 
00400         if (pMapEntries != NULL)
00401         {
00402             while (pMapEntries->szKey != NULL)
00403             {
00404                 ATLASSERT(pMapEntries->szData != NULL);
00405                 hResult = registrar.AddReplacement(pMapEntries->szKey, pMapEntries->szData);
00406                 if (FAILED(hResult))
00407                     return hResult;
00408                 pMapEntries++;
00409             }
00410         }
00411 
00412         hResult = AddCommonRGSReplacements(&registrar);
00413         if (FAILED(hResult))
00414             return hResult;
00415 
00416         hResult = registrar.AddReplacement(_T("Module"), modulePath);
00417         if (FAILED(hResult))
00418             return hResult;
00419 
00420         hResult = registrar.AddReplacement(_T("Module_Raw"), modulePath);
00421         if (FAILED(hResult))
00422             return hResult;
00423 
00424         return S_OK;
00425     }
00426 };
00427 
00428 __declspec(selectany) GUID                  CAtlModule::m_libid = {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} };
00429 
00430 template <class T>
00431 class CAtlModuleT : public CAtlModule
00432 {
00433 public:
00434 
00435     virtual HRESULT AddCommonRGSReplacements(IRegistrarBase *pRegistrar)
00436     {
00437         return pRegistrar->AddReplacement(L"APPID", T::GetAppId());
00438     }
00439 
00440     static LPCOLESTR GetAppId()
00441     {
00442         return L"";
00443     }
00444 };
00445 
00446 class CAtlComModule : public _ATL_COM_MODULE
00447 {
00448 public:
00449     CAtlComModule()
00450     {
00451         GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)this, &m_hInstTypeLib);
00452         m_ppAutoObjMapFirst = NULL;
00453         m_ppAutoObjMapLast = NULL;
00454         if (FAILED(m_csObjMap.Init()))
00455         {
00456             ATLASSERT(0);
00457             CAtlBaseModule::m_bInitFailed = true;
00458             return;
00459         }
00460         cbSize = sizeof(_ATL_COM_MODULE);
00461     }
00462 
00463     ~CAtlComModule()
00464     {
00465         Term();
00466     }
00467 
00468     void Term()
00469     {
00470         if (cbSize != 0)
00471         {
00472             ATLASSERT(m_ppAutoObjMapFirst == NULL);
00473             ATLASSERT(m_ppAutoObjMapLast == NULL);
00474             m_csObjMap.Term();
00475             cbSize = 0;
00476         }
00477     }
00478 };
00479 
00480 template <class T>
00481 class CAtlDllModuleT : public CAtlModuleT<T>
00482 {
00483 public:
00484     CAtlDllModuleT()
00485     {
00486     }
00487 
00488     HRESULT DllCanUnloadNow()
00489     {
00490         T                                   *pThis;
00491 
00492         pThis = static_cast<T *>(this);
00493         if (pThis->GetLockCount() == 0)
00494             return S_OK;
00495         return S_FALSE;
00496     }
00497 
00498     HRESULT DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
00499     {
00500         T                                   *pThis;
00501 
00502         pThis = static_cast<T *>(this);
00503         return pThis->GetClassObject(rclsid, riid, ppv);
00504     }
00505 
00506     HRESULT DllRegisterServer(BOOL bRegTypeLib = TRUE)
00507     {
00508         T                                   *pThis;
00509         HRESULT                             hResult;
00510 
00511         pThis = static_cast<T *>(this);
00512         hResult = pThis->RegisterServer(bRegTypeLib);
00513         return hResult;
00514     }
00515 
00516     HRESULT DllUnregisterServer(BOOL bUnRegTypeLib = TRUE)
00517     {
00518         T                                   *pThis;
00519         HRESULT                             hResult;
00520 
00521         pThis = static_cast<T *>(this);
00522         hResult = pThis->UnregisterServer(bUnRegTypeLib);
00523         return hResult;
00524     }
00525 
00526     HRESULT GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
00527     {
00528         return AtlComModuleGetClassObject(&_AtlComModule, rclsid, riid, ppv);
00529     }
00530 };
00531 
00532 class CComModule : public CAtlModuleT<CComModule>
00533 {
00534 public:
00535     _ATL_OBJMAP_ENTRY                       *m_pObjMap;
00536 public:
00537     CComModule()
00538     {
00539         ATLASSERT(_pModule == NULL);
00540         _pModule = this;
00541         _pModule->m_pObjMap = NULL;
00542     }
00543 
00544     ~CComModule()
00545     {
00546         _pModule = NULL;
00547     }
00548 
00549     HRESULT Init(_ATL_OBJMAP_ENTRY *p, HINSTANCE /* h */, const GUID *plibid)
00550     {
00551         _ATL_OBJMAP_ENTRY                   *objectMapEntry;
00552 
00553         if (plibid != NULL)
00554             m_libid = *plibid;
00555 
00556         if (p != reinterpret_cast<_ATL_OBJMAP_ENTRY *>(-1))
00557         {
00558             m_pObjMap = p;
00559             if (p != NULL)
00560             {
00561                 objectMapEntry = p;
00562                 while (objectMapEntry->pclsid != NULL)
00563                 {
00564                     objectMapEntry->pfnObjectMain(true);
00565                     objectMapEntry++;
00566                 }
00567             }
00568         }
00569         return S_OK;
00570     }
00571 
00572     void Term()
00573     {
00574         _ATL_OBJMAP_ENTRY                   *objectMapEntry;
00575 
00576         if (m_pObjMap != NULL)
00577         {
00578             objectMapEntry = m_pObjMap;
00579             while (objectMapEntry->pclsid != NULL)
00580             {
00581                 if (objectMapEntry->pCF != NULL)
00582                     objectMapEntry->pCF->Release();
00583                 objectMapEntry->pCF = NULL;
00584                 objectMapEntry->pfnObjectMain(false);
00585                 objectMapEntry++;
00586             }
00587         }
00588     }
00589 
00590     HRESULT GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
00591     {
00592         _ATL_OBJMAP_ENTRY                   *objectMapEntry;
00593         HRESULT                             hResult;
00594 
00595         ATLASSERT(ppv != NULL);
00596         if (ppv == NULL)
00597             return E_POINTER;
00598         hResult = S_OK;
00599         if (m_pObjMap != NULL)
00600         {
00601             objectMapEntry = m_pObjMap;
00602             while (objectMapEntry->pclsid != NULL)
00603             {
00604                 if (objectMapEntry->pfnGetClassObject != NULL && InlineIsEqualGUID(rclsid, *objectMapEntry->pclsid) != FALSE)
00605                 {
00606                     if (objectMapEntry->pCF == NULL)
00607                     {
00608                         CComCritSecLock<CComCriticalSection> lock(_AtlComModule.m_csObjMap, true);
00609 
00610                         if (objectMapEntry->pCF == NULL)
00611                             hResult = objectMapEntry->pfnGetClassObject(reinterpret_cast<void *>(objectMapEntry->pfnCreateInstance), IID_IUnknown, reinterpret_cast<LPVOID *>(&objectMapEntry->pCF));
00612                     }
00613                     if (objectMapEntry->pCF != NULL)
00614                         hResult = objectMapEntry->pCF->QueryInterface(riid, ppv);
00615                     break;
00616                 }
00617                 objectMapEntry++;
00618             }
00619         }
00620         return hResult;
00621     }
00622 
00623     HRESULT RegisterServer(BOOL bRegTypeLib = FALSE, const CLSID *pCLSID = NULL)
00624     {
00625         _ATL_OBJMAP_ENTRY                   *objectMapEntry;
00626         HRESULT                             hResult;
00627 
00628         hResult = S_OK;
00629         objectMapEntry = m_pObjMap;
00630         if (objectMapEntry != NULL)
00631         {
00632             while (objectMapEntry->pclsid != NULL)
00633             {
00634                 if (pCLSID == NULL || IsEqualGUID(*pCLSID, *objectMapEntry->pclsid) != FALSE)
00635                 {
00636                     hResult = objectMapEntry->pfnUpdateRegistry(TRUE);
00637                     if (FAILED(hResult))
00638                         break;
00639                 }
00640                 objectMapEntry++;
00641             }
00642         }
00643         return hResult;
00644     }
00645 
00646     HRESULT UnregisterServer(BOOL bUnRegTypeLib, const CLSID *pCLSID = NULL)
00647     {
00648         _ATL_OBJMAP_ENTRY                   *objectMapEntry;
00649         HRESULT                             hResult;
00650 
00651         hResult = S_OK;
00652         objectMapEntry = m_pObjMap;
00653         if (objectMapEntry != NULL)
00654         {
00655             while (objectMapEntry->pclsid != NULL)
00656             {
00657                 if (pCLSID == NULL || IsEqualGUID(*pCLSID, *objectMapEntry->pclsid) != FALSE)
00658                 {
00659                     hResult = objectMapEntry->pfnUpdateRegistry(FALSE); //unregister
00660                     if (FAILED(hResult))
00661                         break;
00662                 }
00663                 objectMapEntry++;
00664             }
00665         }
00666         return hResult;
00667     }
00668 
00669     HRESULT DllCanUnloadNow()
00670     {
00671         if (GetLockCount() == 0)
00672             return S_OK;
00673         return S_FALSE;
00674     }
00675 
00676     HRESULT DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
00677     {
00678         return GetClassObject(rclsid, riid, ppv);
00679     }
00680 
00681     HRESULT DllRegisterServer(BOOL bRegTypeLib = TRUE)
00682     {
00683         return RegisterServer(bRegTypeLib);
00684     }
00685 
00686     HRESULT DllUnregisterServer(BOOL bUnRegTypeLib = TRUE)
00687     {
00688         return UnregisterServer(bUnRegTypeLib);
00689     }
00690 
00691 };
00692 
00693 class CAtlWinModule : public _ATL_WIN_MODULE
00694 {
00695 public:
00696     CAtlWinModule()
00697     {
00698         HRESULT                             hResult;
00699 
00700         hResult = AtlWinModuleInit(this);
00701         if (FAILED(hResult))
00702         {
00703             CAtlBaseModule::m_bInitFailed = true;
00704             ATLASSERT(0);
00705         }
00706     }
00707 
00708     ~CAtlWinModule()
00709     {
00710         Term();
00711     }
00712 
00713     void Term()
00714     {
00715         AtlWinModuleTerm(this, _AtlBaseModule.GetModuleInstance());
00716     }
00717 
00718     void AddCreateWndData(_AtlCreateWndData *pData, void *pObject)
00719     {
00720         AtlWinModuleAddCreateWndData(this, pData, pObject);
00721     }
00722 
00723     void *ExtractCreateWndData()
00724     {
00725         return AtlWinModuleExtractCreateWndData(this);
00726     }
00727 };
00728 
00729 extern CAtlWinModule _AtlWinModule;
00730 
00731 template<class T>
00732 class CComPtr
00733 {
00734 public:
00735     T                                       *p;
00736 public:
00737     CComPtr()
00738     {
00739         p = NULL;
00740     }
00741 
00742     CComPtr(T *lp)
00743     {
00744         p = lp;
00745         if (p != NULL)
00746             p->AddRef();
00747     }
00748 
00749     CComPtr(const CComPtr<T> &lp)
00750     {
00751         p = lp.p;
00752         if (p != NULL)
00753             p->AddRef();
00754     }
00755 
00756     ~CComPtr()
00757     {
00758         if (p != NULL)
00759             p->Release();
00760     }
00761 
00762     T *operator = (T *lp)
00763     {
00764         if (p != NULL)
00765             p->Release();
00766         p = lp;
00767         if (p != NULL)
00768             p->AddRef();
00769         return *this;
00770     }
00771 
00772     T *operator = (const CComPtr<T> &lp)
00773     {
00774         if (p != NULL)
00775             p->Release();
00776         p = lp.p;
00777         if (p != NULL)
00778             p->AddRef();
00779         return *this;
00780     }
00781 
00782     void Release()
00783     {
00784         if (p != NULL)
00785         {
00786             p->Release();
00787             p = NULL;
00788         }
00789     }
00790 
00791     void Attach(T *lp)
00792     {
00793         if (p != NULL)
00794             p->Release();
00795         p = lp;
00796     }
00797 
00798     T *Detach()
00799     {
00800         T                                   *saveP;
00801 
00802         saveP = p;
00803         p = NULL;
00804         return saveP;
00805     }
00806 
00807     T **operator & ()
00808     {
00809         ATLASSERT(p == NULL);
00810         return &p;
00811     }
00812 
00813     operator T * ()
00814     {
00815         return p;
00816     }
00817 
00818     T *operator -> ()
00819     {
00820         ATLASSERT(p != NULL);
00821         return p;
00822     }
00823 };
00824 
00825 class CComBSTR
00826 {
00827 public:
00828     BSTR                                    m_str;
00829 public:
00830     CComBSTR(LPCOLESTR pSrc)
00831     {
00832         if (pSrc == NULL)
00833             m_str = NULL;
00834         else
00835             m_str = ::SysAllocString(pSrc);
00836     }
00837     ~CComBSTR()
00838     {
00839         ::SysFreeString(m_str);
00840         m_str = NULL;
00841     }
00842 };
00843 
00844 class CComVariant : public tagVARIANT
00845 {
00846 public:
00847     CComVariant()
00848     {
00849         ::VariantInit(this);
00850     }
00851 
00852     ~CComVariant()
00853     {
00854         Clear();
00855     }
00856 
00857     HRESULT Clear()
00858     {
00859         return ::VariantClear(this);
00860     }
00861 };
00862 
00863 inline HRESULT __stdcall AtlAdvise(IUnknown *pUnkCP, IUnknown *pUnk, const IID &iid, LPDWORD pdw)
00864 {
00865     CComPtr<IConnectionPointContainer>      container;
00866     CComPtr<IConnectionPoint>               connectionPoint;
00867     HRESULT                                 hResult;
00868 
00869     if (pUnkCP == NULL)
00870         return E_INVALIDARG;
00871     hResult = pUnkCP->QueryInterface(IID_IConnectionPointContainer, (void **)&container);
00872     if (FAILED(hResult))
00873         return hResult;
00874     hResult = container->FindConnectionPoint(iid, &connectionPoint);
00875     if (FAILED(hResult))
00876         return hResult;
00877     return connectionPoint->Advise(pUnk, pdw);
00878 }
00879 
00880 inline HRESULT __stdcall AtlUnadvise(IUnknown *pUnkCP, const IID &iid, DWORD dw)
00881 {
00882     CComPtr<IConnectionPointContainer>      container;
00883     CComPtr<IConnectionPoint>               connectionPoint;
00884     HRESULT                                 hResult;
00885 
00886     if (pUnkCP == NULL)
00887         return E_INVALIDARG;
00888     hResult = pUnkCP->QueryInterface(IID_IConnectionPointContainer, (void **)&container);
00889     if (FAILED(hResult))
00890         return hResult;
00891     hResult = container->FindConnectionPoint(iid, &connectionPoint);
00892     if (FAILED(hResult))
00893         return hResult;
00894     return connectionPoint->Unadvise(dw);
00895 }
00896 
00897 inline HRESULT __stdcall AtlInternalQueryInterface(void *pThis, const _ATL_INTMAP_ENTRY *pEntries, REFIID iid, void **ppvObject)
00898 {
00899     int                         i;
00900     IUnknown                    *resultInterface;
00901     HRESULT                     hResult;
00902 
00903     ATLASSERT(pThis != NULL && pEntries != NULL);
00904     if (pThis == NULL || pEntries == NULL)
00905         return E_INVALIDARG;
00906     ATLASSERT(ppvObject != NULL);
00907     if (ppvObject == NULL)
00908         return E_POINTER;
00909 
00910     if (InlineIsEqualUnknown(iid))
00911     {
00912         resultInterface = reinterpret_cast<IUnknown *>(reinterpret_cast<char *>(pThis) + pEntries[0].dw);
00913         *ppvObject = resultInterface;
00914         resultInterface->AddRef();
00915         return S_OK;
00916     }
00917 
00918     i = 0;
00919     while (pEntries[i].pFunc != 0)
00920     {
00921         if (pEntries[i].piid == NULL || InlineIsEqualGUID(iid, *pEntries[i].piid))
00922         {
00923             if (pEntries[i].pFunc == reinterpret_cast<_ATL_CREATORARGFUNC *>(1))
00924             {
00925                 ATLASSERT(pEntries[i].piid != NULL);
00926                 resultInterface = reinterpret_cast<IUnknown *>(reinterpret_cast<char *>(pThis) + pEntries[i].dw);
00927                 *ppvObject = resultInterface;
00928                 resultInterface->AddRef();
00929                 return S_OK;
00930             }
00931             else
00932             {
00933                 hResult = pEntries[i].pFunc(pThis, iid, ppvObject, 0);
00934                 if (hResult == S_OK || (FAILED(hResult) && pEntries[i].piid != NULL))
00935                     return hResult;
00936             }
00937             break;
00938         }
00939         i++;
00940     }
00941     *ppvObject = NULL;
00942     return E_NOINTERFACE;
00943 }
00944 
00945 inline HRESULT __stdcall AtlWinModuleInit(_ATL_WIN_MODULE *pWinModule)
00946 {
00947     if (pWinModule == NULL)
00948         return E_INVALIDARG;
00949     pWinModule->m_pCreateWndList = NULL;
00950     return pWinModule->m_csWindowCreate.Init();
00951 }
00952 
00953 inline HRESULT __stdcall AtlWinModuleTerm(_ATL_WIN_MODULE *pWinModule, HINSTANCE hInst)
00954 {
00955     if (pWinModule == NULL)
00956         return E_INVALIDARG;
00957     pWinModule->m_csWindowCreate.Term();
00958     return S_OK;
00959 }
00960 
00961 inline void __stdcall AtlWinModuleAddCreateWndData(_ATL_WIN_MODULE *pWinModule, _AtlCreateWndData *pData, void *pObject)
00962 {
00963     CComCritSecLock<CComCriticalSection>    lock(pWinModule->m_csWindowCreate, true);
00964 
00965     ATLASSERT(pWinModule != NULL);
00966     ATLASSERT(pObject != NULL);
00967 
00968     pData->m_pThis = pObject;
00969     pData->m_dwThreadID = ::GetCurrentThreadId();
00970     pData->m_pNext = pWinModule->m_pCreateWndList;
00971     pWinModule->m_pCreateWndList = pData;
00972 }
00973 
00974 inline void *__stdcall AtlWinModuleExtractCreateWndData(_ATL_WIN_MODULE *pWinModule)
00975 {
00976     CComCritSecLock<CComCriticalSection>    lock(pWinModule->m_csWindowCreate, true);
00977     void                                    *result;
00978     _AtlCreateWndData                       *currentEntry;
00979     _AtlCreateWndData                       **previousLink;
00980     DWORD                                   threadID;
00981 
00982     ATLASSERT(pWinModule != NULL);
00983 
00984     result = NULL;
00985     threadID = GetCurrentThreadId();
00986     currentEntry = pWinModule->m_pCreateWndList;
00987     previousLink = &pWinModule->m_pCreateWndList;
00988     while (currentEntry != NULL)
00989     {
00990         if (currentEntry->m_dwThreadID == threadID)
00991         {
00992             *previousLink = currentEntry->m_pNext;
00993             result = currentEntry->m_pThis;
00994             break;
00995         }
00996         previousLink = &currentEntry->m_pNext;
00997         currentEntry = currentEntry->m_pNext;
00998     }
00999     return result;
01000 }
01001 
01002 }; // namespace ATL
01003 
01004 #ifndef _ATL_NO_AUTOMATIC_NAMESPACE
01005 using namespace ATL;
01006 #endif //!_ATL_NO_AUTOMATIC_NAMESPACE

Generated on Sun May 27 2012 04:22:47 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.