4 * Copyright 2009 Andrew Hill <ash77@reactos.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 // It is common to use this in ATL constructors. They only store this for later use, so the usage is safe.
29 #pragma warning(disable:4355)
33 #define _ATL_PACKING 8
36 #ifndef _ATL_FREE_THREADED
37 #ifndef _ATL_APARTMENT_THREADED
38 #ifndef _ATL_SINGLE_THREADED
39 #define _ATL_FREE_THREADED
48 #ifdef _ATL_DISABLE_NO_VTABLE
51 #define ATL_NO_VTABLE __declspec(novtable)
54 #define offsetofclass(base, derived) (reinterpret_cast<DWORD_PTR>(static_cast<base *>(reinterpret_cast<derived *>(_ATL_PACKING))) - _ATL_PACKING)
62 __declspec(selectany
) CAtlModule
*_pAtlModule
= NULL
;
63 __declspec(selectany
) CComModule
*_pModule
= NULL
;
64 extern CAtlComModule _AtlComModule
;
66 typedef HRESULT (WINAPI _ATL_CREATORFUNC
)(void *pv
, REFIID riid
, LPVOID
*ppv
);
67 typedef LPCTSTR (WINAPI _ATL_DESCRIPTIONFUNC
)();
68 typedef const struct _ATL_CATMAP_ENTRY
* (_ATL_CATMAPFUNC
)();
70 struct _ATL_OBJMAP_ENTRY30
73 HRESULT (WINAPI
*pfnUpdateRegistry
)(BOOL bRegister
);
74 _ATL_CREATORFUNC
*pfnGetClassObject
;
75 _ATL_CREATORFUNC
*pfnCreateInstance
;
78 _ATL_DESCRIPTIONFUNC
*pfnGetObjectDescription
;
79 _ATL_CATMAPFUNC
*pfnGetCategoryMap
;
80 void (WINAPI
*pfnObjectMain
)(bool bStarting
);
82 HRESULT WINAPI
RevokeClassObject()
86 return CoRevokeClassObject(dwRegister
);
89 HRESULT WINAPI
RegisterClassObject(DWORD dwClsContext
, DWORD dwFlags
)
95 if (pfnGetClassObject
== NULL
)
98 hResult
= pfnGetClassObject(reinterpret_cast<LPVOID
*>(pfnCreateInstance
), IID_IUnknown
, reinterpret_cast<LPVOID
*>(&p
));
99 if (SUCCEEDED(hResult
))
100 hResult
= CoRegisterClassObject(*pclsid
, p
, dwClsContext
, dwFlags
, &dwRegister
);
109 typedef _ATL_OBJMAP_ENTRY30 _ATL_OBJMAP_ENTRY
;
111 typedef void (__stdcall _ATL_TERMFUNC
)(DWORD_PTR dw
);
113 struct _ATL_TERMFUNC_ELEM
115 _ATL_TERMFUNC
*pFunc
;
117 _ATL_TERMFUNC_ELEM
*pNext
;
124 _ATL_TERMFUNC_ELEM
*m_pTermFuncs
;
125 CComCriticalSection m_csStaticDataInitAndTypeInfo
;
127 typedef _ATL_MODULE70 _ATL_MODULE
;
129 typedef HRESULT (WINAPI _ATL_CREATORARGFUNC
)(void *pv
, REFIID riid
, LPVOID
*ppv
, DWORD_PTR dw
);
131 #define _ATL_SIMPLEMAPENTRY ((ATL::_ATL_CREATORARGFUNC *)1)
133 struct _ATL_INTMAP_ENTRY
137 _ATL_CREATORARGFUNC
*pFunc
;
140 struct _AtlCreateWndData
144 _AtlCreateWndData
*m_pNext
;
147 struct _ATL_COM_MODULE70
150 HINSTANCE m_hInstTypeLib
;
151 _ATL_OBJMAP_ENTRY
**m_ppAutoObjMapFirst
;
152 _ATL_OBJMAP_ENTRY
**m_ppAutoObjMapLast
;
153 CComCriticalSection m_csObjMap
;
155 typedef _ATL_COM_MODULE70 _ATL_COM_MODULE
;
157 struct _ATL_WIN_MODULE70
160 CComCriticalSection m_csWindowCreate
;
161 _AtlCreateWndData
*m_pCreateWndList
;
163 CSimpleArray
<ATOM
> m_rgWindowClassAtoms
;
166 typedef _ATL_WIN_MODULE70 _ATL_WIN_MODULE
;
168 struct _ATL_REGMAP_ENTRY
174 HRESULT __stdcall
AtlWinModuleInit(_ATL_WIN_MODULE
*pWinModule
);
175 HRESULT __stdcall
AtlWinModuleTerm(_ATL_WIN_MODULE
*pWinModule
, HINSTANCE hInst
);
176 HRESULT __stdcall
AtlInternalQueryInterface(void *pThis
, const _ATL_INTMAP_ENTRY
*pEntries
, REFIID iid
, void **ppvObject
);
177 void __stdcall
AtlWinModuleAddCreateWndData(_ATL_WIN_MODULE
*pWinModule
, _AtlCreateWndData
*pData
, void *pObject
);
178 void *__stdcall
AtlWinModuleExtractCreateWndData(_ATL_WIN_MODULE
*pWinModule
);
179 HRESULT __stdcall
AtlComModuleGetClassObject(_ATL_COM_MODULE
*pComModule
, REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
);
181 template<class TLock
>
182 class CComCritSecLock
188 CComCritSecLock(TLock
&cs
, bool bInitialLock
= true) : m_cs(cs
)
213 ATLASSERT(!m_bLocked
);
214 hResult
= m_cs
.Lock();
226 ATLASSERT(m_bLocked
);
227 hResult
= m_cs
.Unlock();
236 inline BOOL WINAPI
InlineIsEqualUnknown(REFGUID rguid1
)
239 ((unsigned long *)&rguid1
)[0] == 0 &&
240 ((unsigned long *)&rguid1
)[1] == 0 &&
241 ((unsigned long *)&rguid1
)[2] == 0x000000C0 &&
242 ((unsigned long *)&rguid1
)[3] == 0x46000000);
245 class CComMultiThreadModelNoCS
248 typedef CComFakeCriticalSection AutoCriticalSection
;
249 typedef CComFakeCriticalSection CriticalSection
;
250 typedef CComMultiThreadModelNoCS ThreadModelNoCS
;
251 typedef CComFakeCriticalSection AutoDeleteCriticalSection
;
253 static ULONG WINAPI
Increment(LPLONG p
)
255 return InterlockedIncrement(p
);
258 static ULONG WINAPI
Decrement(LPLONG p
)
260 return InterlockedDecrement(p
);
264 class CComMultiThreadModel
267 typedef CComAutoCriticalSection AutoCriticalSection
;
268 typedef CComCriticalSection CriticalSection
;
269 typedef CComMultiThreadModelNoCS ThreadModelNoCS
;
270 typedef CComAutoDeleteCriticalSection AutoDeleteCriticalSection
;
272 static ULONG WINAPI
Increment(LPLONG p
)
274 return InterlockedIncrement(p
);
277 static ULONG WINAPI
Decrement(LPLONG p
)
279 return InterlockedDecrement(p
);
283 class CComSingleThreadModel
286 typedef CComFakeCriticalSection AutoCriticalSection
;
287 typedef CComFakeCriticalSection CriticalSection
;
288 typedef CComSingleThreadModel ThreadModelNoCS
;
289 typedef CComFakeCriticalSection AutoDeleteCriticalSection
;
291 static ULONG WINAPI
Increment(LPLONG p
)
296 static ULONG WINAPI
Decrement(LPLONG p
)
302 #if defined(_ATL_FREE_THREADED)
304 typedef CComMultiThreadModel CComObjectThreadModel
;
305 typedef CComMultiThreadModel CComGlobalsThreadModel
;
307 #elif defined(_ATL_APARTMENT_THREADED)
309 typedef CComSingleThreadModel CComObjectThreadModel
;
310 typedef CComMultiThreadModel CComGlobalsThreadModel
;
312 #elif defined(_ATL_SINGLE_THREADED)
314 typedef CComSingleThreadModel CComObjectThreadModel
;
315 typedef CComSingleThreadModel CComGlobalsThreadModel
;
318 #error No threading model
321 class CAtlModule
: public _ATL_MODULE
328 ATLASSERT(_pAtlModule
== NULL
);
330 cbSize
= sizeof(_ATL_MODULE
);
334 virtual LONG
GetLockCount()
341 return CComGlobalsThreadModel::Increment(&m_nLockCnt
);
344 virtual LONG
Unlock()
346 return CComGlobalsThreadModel::Decrement(&m_nLockCnt
);
349 virtual HRESULT
AddCommonRGSReplacements(IRegistrarBase
* /*pRegistrar*/) = 0;
351 HRESULT WINAPI
UpdateRegistryFromResource(LPCTSTR lpszRes
, BOOL bRegister
, struct _ATL_REGMAP_ENTRY
*pMapEntries
= NULL
)
353 CRegObject registrar
;
354 TCHAR modulePath
[MAX_PATH
];
357 hResult
= CommonInitRegistrar(registrar
, modulePath
, sizeof(modulePath
) / sizeof(modulePath
[0]), pMapEntries
);
361 if (bRegister
!= FALSE
)
362 hResult
= registrar
.ResourceRegisterSz(modulePath
, lpszRes
, _T("REGISTRY"));
364 hResult
= registrar
.ResourceUnregisterSz(modulePath
, lpszRes
, _T("REGISTRY"));
369 HRESULT WINAPI
UpdateRegistryFromResource(UINT nResID
, BOOL bRegister
, struct _ATL_REGMAP_ENTRY
*pMapEntries
= NULL
)
371 CRegObject registrar
;
372 TCHAR modulePath
[MAX_PATH
];
375 hResult
= CommonInitRegistrar(registrar
, modulePath
, sizeof(modulePath
) / sizeof(modulePath
[0]), pMapEntries
);
379 if (bRegister
!= FALSE
)
380 hResult
= registrar
.ResourceRegister(modulePath
, nResID
, _T("REGISTRY"));
382 hResult
= registrar
.ResourceRegister(modulePath
, nResID
, _T("REGISTRY"));
388 HRESULT
CommonInitRegistrar(CRegObject
®istrar
, TCHAR
*modulePath
, DWORD modulePathCount
, struct _ATL_REGMAP_ENTRY
*pMapEntries
)
394 hInstance
= _AtlBaseModule
.GetModuleInstance();
395 dwFLen
= GetModuleFileName(hInstance
, modulePath
, modulePathCount
);
396 if (dwFLen
== modulePathCount
)
397 return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER
);
398 else if (dwFLen
== 0)
399 return HRESULT_FROM_WIN32(GetLastError());
401 if (pMapEntries
!= NULL
)
403 while (pMapEntries
->szKey
!= NULL
)
405 ATLASSERT(pMapEntries
->szData
!= NULL
);
406 hResult
= registrar
.AddReplacement(pMapEntries
->szKey
, pMapEntries
->szData
);
413 hResult
= AddCommonRGSReplacements(®istrar
);
417 hResult
= registrar
.AddReplacement(_T("Module"), modulePath
);
421 hResult
= registrar
.AddReplacement(_T("Module_Raw"), modulePath
);
429 __declspec(selectany
) GUID
CAtlModule::m_libid
= {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} };
432 class CAtlModuleT
: public CAtlModule
436 virtual HRESULT
AddCommonRGSReplacements(IRegistrarBase
*pRegistrar
)
438 return pRegistrar
->AddReplacement(L
"APPID", T::GetAppId());
441 static LPCOLESTR
GetAppId()
447 class CAtlComModule
: public _ATL_COM_MODULE
452 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (LPCWSTR
)this, &m_hInstTypeLib
);
453 m_ppAutoObjMapFirst
= NULL
;
454 m_ppAutoObjMapLast
= NULL
;
455 if (FAILED(m_csObjMap
.Init()))
458 CAtlBaseModule::m_bInitFailed
= true;
461 cbSize
= sizeof(_ATL_COM_MODULE
);
473 ATLASSERT(m_ppAutoObjMapFirst
== NULL
);
474 ATLASSERT(m_ppAutoObjMapLast
== NULL
);
482 class CAtlDllModuleT
: public CAtlModuleT
<T
>
489 HRESULT
DllCanUnloadNow()
493 pThis
= static_cast<T
*>(this);
494 if (pThis
->GetLockCount() == 0)
499 HRESULT
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
503 pThis
= static_cast<T
*>(this);
504 return pThis
->GetClassObject(rclsid
, riid
, ppv
);
507 HRESULT
DllRegisterServer(BOOL bRegTypeLib
= TRUE
)
512 pThis
= static_cast<T
*>(this);
513 hResult
= pThis
->RegisterServer(bRegTypeLib
);
517 HRESULT
DllUnregisterServer(BOOL bUnRegTypeLib
= TRUE
)
522 pThis
= static_cast<T
*>(this);
523 hResult
= pThis
->UnregisterServer(bUnRegTypeLib
);
527 HRESULT
GetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
529 return AtlComModuleGetClassObject(&_AtlComModule
, rclsid
, riid
, ppv
);
533 class CComModule
: public CAtlModuleT
<CComModule
>
536 _ATL_OBJMAP_ENTRY
*m_pObjMap
;
540 ATLASSERT(_pModule
== NULL
);
549 HRESULT
Init(_ATL_OBJMAP_ENTRY
*p
, HINSTANCE
/* h */, const GUID
*plibid
)
551 _ATL_OBJMAP_ENTRY
*objectMapEntry
;
556 if (p
!= reinterpret_cast<_ATL_OBJMAP_ENTRY
*>(-1))
562 while (objectMapEntry
->pclsid
!= NULL
)
564 objectMapEntry
->pfnObjectMain(true);
574 _ATL_OBJMAP_ENTRY
*objectMapEntry
;
576 if (m_pObjMap
!= NULL
)
578 objectMapEntry
= m_pObjMap
;
579 while (objectMapEntry
->pclsid
!= NULL
)
581 if (objectMapEntry
->pCF
!= NULL
)
582 objectMapEntry
->pCF
->Release();
583 objectMapEntry
->pCF
= NULL
;
584 objectMapEntry
->pfnObjectMain(false);
590 HRESULT
GetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
592 _ATL_OBJMAP_ENTRY
*objectMapEntry
;
595 ATLASSERT(ppv
!= NULL
);
599 if (m_pObjMap
!= NULL
)
601 objectMapEntry
= m_pObjMap
;
602 while (objectMapEntry
->pclsid
!= NULL
)
604 if (objectMapEntry
->pfnGetClassObject
!= NULL
&& InlineIsEqualGUID(rclsid
, *objectMapEntry
->pclsid
) != FALSE
)
606 if (objectMapEntry
->pCF
== NULL
)
608 CComCritSecLock
<CComCriticalSection
> lock(_AtlComModule
.m_csObjMap
, true);
610 if (objectMapEntry
->pCF
== NULL
)
611 hResult
= objectMapEntry
->pfnGetClassObject(reinterpret_cast<void *>(objectMapEntry
->pfnCreateInstance
), IID_IUnknown
, reinterpret_cast<LPVOID
*>(&objectMapEntry
->pCF
));
613 if (objectMapEntry
->pCF
!= NULL
)
614 hResult
= objectMapEntry
->pCF
->QueryInterface(riid
, ppv
);
623 HRESULT
RegisterServer(BOOL bRegTypeLib
= FALSE
, const CLSID
*pCLSID
= NULL
)
625 _ATL_OBJMAP_ENTRY
*objectMapEntry
;
629 objectMapEntry
= m_pObjMap
;
630 if (objectMapEntry
!= NULL
)
632 while (objectMapEntry
->pclsid
!= NULL
)
634 if (pCLSID
== NULL
|| IsEqualGUID(*pCLSID
, *objectMapEntry
->pclsid
) != FALSE
)
636 hResult
= objectMapEntry
->pfnUpdateRegistry(TRUE
);
646 HRESULT
UnregisterServer(BOOL bUnRegTypeLib
, const CLSID
*pCLSID
= NULL
)
648 _ATL_OBJMAP_ENTRY
*objectMapEntry
;
652 objectMapEntry
= m_pObjMap
;
653 if (objectMapEntry
!= NULL
)
655 while (objectMapEntry
->pclsid
!= NULL
)
657 if (pCLSID
== NULL
|| IsEqualGUID(*pCLSID
, *objectMapEntry
->pclsid
) != FALSE
)
659 hResult
= objectMapEntry
->pfnUpdateRegistry(FALSE
); //unregister
669 HRESULT
DllCanUnloadNow()
671 if (GetLockCount() == 0)
676 HRESULT
DllGetClassObject(REFCLSID rclsid
, REFIID riid
, LPVOID
*ppv
)
678 return GetClassObject(rclsid
, riid
, ppv
);
681 HRESULT
DllRegisterServer(BOOL bRegTypeLib
= TRUE
)
683 return RegisterServer(bRegTypeLib
);
686 HRESULT
DllUnregisterServer(BOOL bUnRegTypeLib
= TRUE
)
688 return UnregisterServer(bUnRegTypeLib
);
693 class CAtlWinModule
: public _ATL_WIN_MODULE
700 hResult
= AtlWinModuleInit(this);
703 CAtlBaseModule::m_bInitFailed
= true;
715 AtlWinModuleTerm(this, _AtlBaseModule
.GetModuleInstance());
718 void AddCreateWndData(_AtlCreateWndData
*pData
, void *pObject
)
720 AtlWinModuleAddCreateWndData(this, pData
, pObject
);
723 void *ExtractCreateWndData()
725 return AtlWinModuleExtractCreateWndData(this);
729 extern CAtlWinModule _AtlWinModule
;
749 CComPtr(const CComPtr
<T
> &lp
)
762 T
*operator = (T
*lp
)
772 T
*operator = (const CComPtr
<T
> &lp
)
809 ATLASSERT(p
== NULL
);
820 ATLASSERT(p
!= NULL
);
830 CComBSTR(LPCOLESTR pSrc
)
835 m_str
= ::SysAllocString(pSrc
);
839 ::SysFreeString(m_str
);
844 class CComVariant
: public tagVARIANT
859 return ::VariantClear(this);
863 inline HRESULT __stdcall
AtlAdvise(IUnknown
*pUnkCP
, IUnknown
*pUnk
, const IID
&iid
, LPDWORD pdw
)
865 CComPtr
<IConnectionPointContainer
> container
;
866 CComPtr
<IConnectionPoint
> connectionPoint
;
871 hResult
= pUnkCP
->QueryInterface(IID_IConnectionPointContainer
, (void **)&container
);
874 hResult
= container
->FindConnectionPoint(iid
, &connectionPoint
);
877 return connectionPoint
->Advise(pUnk
, pdw
);
880 inline HRESULT __stdcall
AtlUnadvise(IUnknown
*pUnkCP
, const IID
&iid
, DWORD dw
)
882 CComPtr
<IConnectionPointContainer
> container
;
883 CComPtr
<IConnectionPoint
> connectionPoint
;
888 hResult
= pUnkCP
->QueryInterface(IID_IConnectionPointContainer
, (void **)&container
);
891 hResult
= container
->FindConnectionPoint(iid
, &connectionPoint
);
894 return connectionPoint
->Unadvise(dw
);
897 inline HRESULT __stdcall
AtlInternalQueryInterface(void *pThis
, const _ATL_INTMAP_ENTRY
*pEntries
, REFIID iid
, void **ppvObject
)
900 IUnknown
*resultInterface
;
903 ATLASSERT(pThis
!= NULL
&& pEntries
!= NULL
);
904 if (pThis
== NULL
|| pEntries
== NULL
)
906 ATLASSERT(ppvObject
!= NULL
);
907 if (ppvObject
== NULL
)
910 if (InlineIsEqualUnknown(iid
))
912 resultInterface
= reinterpret_cast<IUnknown
*>(reinterpret_cast<char *>(pThis
) + pEntries
[0].dw
);
913 *ppvObject
= resultInterface
;
914 resultInterface
->AddRef();
919 while (pEntries
[i
].pFunc
!= 0)
921 if (pEntries
[i
].piid
== NULL
|| InlineIsEqualGUID(iid
, *pEntries
[i
].piid
))
923 if (pEntries
[i
].pFunc
== reinterpret_cast<_ATL_CREATORARGFUNC
*>(1))
925 ATLASSERT(pEntries
[i
].piid
!= NULL
);
926 resultInterface
= reinterpret_cast<IUnknown
*>(reinterpret_cast<char *>(pThis
) + pEntries
[i
].dw
);
927 *ppvObject
= resultInterface
;
928 resultInterface
->AddRef();
933 hResult
= pEntries
[i
].pFunc(pThis
, iid
, ppvObject
, 0);
934 if (hResult
== S_OK
|| (FAILED(hResult
) && pEntries
[i
].piid
!= NULL
))
942 return E_NOINTERFACE
;
945 inline HRESULT __stdcall
AtlWinModuleInit(_ATL_WIN_MODULE
*pWinModule
)
947 if (pWinModule
== NULL
)
949 pWinModule
->m_pCreateWndList
= NULL
;
950 return pWinModule
->m_csWindowCreate
.Init();
953 inline HRESULT __stdcall
AtlWinModuleTerm(_ATL_WIN_MODULE
*pWinModule
, HINSTANCE hInst
)
955 if (pWinModule
== NULL
)
957 pWinModule
->m_csWindowCreate
.Term();
961 inline void __stdcall
AtlWinModuleAddCreateWndData(_ATL_WIN_MODULE
*pWinModule
, _AtlCreateWndData
*pData
, void *pObject
)
963 CComCritSecLock
<CComCriticalSection
> lock(pWinModule
->m_csWindowCreate
, true);
965 ATLASSERT(pWinModule
!= NULL
);
966 ATLASSERT(pObject
!= NULL
);
968 pData
->m_pThis
= pObject
;
969 pData
->m_dwThreadID
= ::GetCurrentThreadId();
970 pData
->m_pNext
= pWinModule
->m_pCreateWndList
;
971 pWinModule
->m_pCreateWndList
= pData
;
974 inline void *__stdcall
AtlWinModuleExtractCreateWndData(_ATL_WIN_MODULE
*pWinModule
)
976 CComCritSecLock
<CComCriticalSection
> lock(pWinModule
->m_csWindowCreate
, true);
978 _AtlCreateWndData
*currentEntry
;
979 _AtlCreateWndData
**previousLink
;
982 ATLASSERT(pWinModule
!= NULL
);
985 threadID
= GetCurrentThreadId();
986 currentEntry
= pWinModule
->m_pCreateWndList
;
987 previousLink
= &pWinModule
->m_pCreateWndList
;
988 while (currentEntry
!= NULL
)
990 if (currentEntry
->m_dwThreadID
== threadID
)
992 *previousLink
= currentEntry
->m_pNext
;
993 result
= currentEntry
->m_pThis
;
996 previousLink
= ¤tEntry
->m_pNext
;
997 currentEntry
= currentEntry
->m_pNext
;
1004 #ifndef _ATL_NO_AUTOMATIC_NAMESPACE
1005 using namespace ATL
;
1006 #endif //!_ATL_NO_AUTOMATIC_NAMESPACE
1008 #endif // _atlbase_h