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
26 #define WIN32_NO_STATUS
28 #define COM_NO_WINDOWS_H
42 #define ATLASSERT(expr) _ASSERTE(expr)
48 class CComCriticalSection
51 CRITICAL_SECTION m_sec
;
55 memset(&m_sec
, 0, sizeof(CRITICAL_SECTION
));
58 virtual ~CComCriticalSection()
64 EnterCriticalSection(&m_sec
);
70 LeaveCriticalSection(&m_sec
);
76 InitializeCriticalSection(&m_sec
);
82 DeleteCriticalSection(&m_sec
);
87 class CComFakeCriticalSection
111 class CComAutoCriticalSection
: public CComCriticalSection
114 CComAutoCriticalSection()
116 HRESULT hResult __MINGW_ATTRIB_UNUSED
;
118 hResult
= CComCriticalSection::Init();
119 ATLASSERT(SUCCEEDED(hResult
));
121 ~CComAutoCriticalSection()
123 CComCriticalSection::Term();
127 class CComSafeDeleteCriticalSection
: public CComCriticalSection
132 CComSafeDeleteCriticalSection()
134 m_bInitialized
= false;
137 ~CComSafeDeleteCriticalSection()
144 ATLASSERT(m_bInitialized
);
145 return CComCriticalSection::Lock();
152 ATLASSERT(!m_bInitialized
);
153 hResult
= CComCriticalSection::Init();
154 if (SUCCEEDED(hResult
))
155 m_bInitialized
= true;
163 m_bInitialized
= false;
164 return CComCriticalSection::Term();
168 class CComAutoDeleteCriticalSection
: public CComSafeDeleteCriticalSection
171 // CComAutoDeleteCriticalSection::Term should never be called
175 struct _ATL_BASE_MODULE70
179 HINSTANCE m_hInstResource
;
183 CRITICAL_SECTION m_csResource
;
185 CSimpleArray
<HINSTANCE
> m_rgResourceInstance
;
188 typedef _ATL_BASE_MODULE70 _ATL_BASE_MODULE
;
190 class CAtlBaseModule
: public _ATL_BASE_MODULE
193 static bool m_bInitFailed
;
197 cbSize
= sizeof(_ATL_BASE_MODULE
);
198 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (LPCWSTR
)this, &m_hInst
);
199 m_hInstResource
= m_hInst
;
202 HINSTANCE
GetModuleInstance()
207 HINSTANCE
GetResourceInstance()
209 return m_hInstResource
;
212 HINSTANCE
SetResourceInstance(HINSTANCE hInst
)
214 return static_cast< HINSTANCE
>(InterlockedExchangePointer((void**)&m_hInstResource
, hInst
));
217 HINSTANCE
GetHInstanceAt(int i
);
220 __declspec(selectany
) CAtlBaseModule _AtlBaseModule
;
221 __declspec(selectany
) bool CAtlBaseModule::m_bInitFailed
= false;
225 // String Resource helper functions
228 #pragma warning(push)
229 #pragma warning(disable:4200)
231 struct ATLSTRINGRESOURCEIMAGE
240 inline const ATLSTRINGRESOURCEIMAGE
* _AtlGetStringResourceImage(
241 _In_ HINSTANCE hInstance
,
242 _In_ HRSRC hResource
,
245 const ATLSTRINGRESOURCEIMAGE
* pImage
;
246 const ATLSTRINGRESOURCEIMAGE
* pImageEnd
;
251 hGlobal
= ::LoadResource(hInstance
, hResource
);
252 if (hGlobal
== NULL
) return NULL
;
254 pImage
= (const ATLSTRINGRESOURCEIMAGE
*)::LockResource(hGlobal
);
255 if (pImage
== NULL
) return NULL
;
257 nResourceSize
= ::SizeofResource(hInstance
, hResource
);
258 pImageEnd
= (const ATLSTRINGRESOURCEIMAGE
*)(LPBYTE(pImage
) + nResourceSize
);
259 iIndex
= id
& 0x000f;
261 while ((iIndex
> 0) && (pImage
< pImageEnd
))
263 pImage
= (const ATLSTRINGRESOURCEIMAGE
*)(LPBYTE(pImage
) + (sizeof(ATLSTRINGRESOURCEIMAGE
) + (pImage
->nLength
* sizeof(WCHAR
))));
267 if (pImage
>= pImageEnd
) return NULL
;
268 if (pImage
->nLength
== 0) return NULL
;
273 inline const ATLSTRINGRESOURCEIMAGE
* AtlGetStringResourceImage(
274 _In_ HINSTANCE hInstance
,
275 _In_ UINT id
) throw()
278 hResource
= ::FindResourceW(hInstance
, MAKEINTRESOURCEW((((id
>> 4) + 1) & static_cast<WORD
>(~0))), (LPWSTR
)RT_STRING
);
279 if (hResource
== NULL
) return NULL
;
280 return _AtlGetStringResourceImage(hInstance
, hResource
, id
);
283 inline const ATLSTRINGRESOURCEIMAGE
* AtlGetStringResourceImage(
284 _In_ HINSTANCE hInstance
,
289 hResource
= ::FindResourceExW(hInstance
, (LPWSTR
)RT_STRING
, MAKEINTRESOURCEW((((id
>> 4) + 1) & static_cast<WORD
>(~0))), wLanguage
);
290 if (hResource
== NULL
) return NULL
;
291 return _AtlGetStringResourceImage(hInstance
, hResource
, id
);
294 inline HINSTANCE
AtlFindStringResourceInstance(
298 const ATLSTRINGRESOURCEIMAGE
* strRes
= NULL
;
299 HINSTANCE hInst
= _AtlBaseModule
.GetHInstanceAt(0);
301 for (int i
= 1; hInst
!= NULL
&& strRes
== NULL
; hInst
= _AtlBaseModule
.GetHInstanceAt(i
++))
303 strRes
= AtlGetStringResourceImage(hInst
, nID
, wLanguage
);
304 if (strRes
!= NULL
) return hInst
;