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
25 #define WIN32_NO_STATUS
27 #define COM_NO_WINDOWS_H
38 #define ATLASSERT(expr) _ASSERTE(expr)
44 class CComCriticalSection
47 CRITICAL_SECTION m_sec
;
51 memset(&m_sec
, 0, sizeof(CRITICAL_SECTION
));
54 virtual ~CComCriticalSection()
60 EnterCriticalSection(&m_sec
);
66 LeaveCriticalSection(&m_sec
);
72 InitializeCriticalSection(&m_sec
);
78 DeleteCriticalSection(&m_sec
);
83 class CComFakeCriticalSection
107 class CComAutoCriticalSection
: public CComCriticalSection
110 CComAutoCriticalSection()
112 HRESULT hResult __MINGW_ATTRIB_UNUSED
;
114 hResult
= CComCriticalSection::Init();
115 ATLASSERT(SUCCEEDED(hResult
));
117 ~CComAutoCriticalSection()
119 CComCriticalSection::Term();
123 class CComSafeDeleteCriticalSection
: public CComCriticalSection
128 CComSafeDeleteCriticalSection()
130 m_bInitialized
= false;
133 ~CComSafeDeleteCriticalSection()
140 ATLASSERT(m_bInitialized
);
141 return CComCriticalSection::Lock();
148 ATLASSERT(!m_bInitialized
);
149 hResult
= CComCriticalSection::Init();
150 if (SUCCEEDED(hResult
))
151 m_bInitialized
= true;
159 m_bInitialized
= false;
160 return CComCriticalSection::Term();
164 class CComAutoDeleteCriticalSection
: public CComSafeDeleteCriticalSection
167 // CComAutoDeleteCriticalSection::Term should never be called
171 struct _ATL_BASE_MODULE70
175 HINSTANCE m_hInstResource
;
179 CRITICAL_SECTION m_csResource
;
181 CSimpleArray
<HINSTANCE
> m_rgResourceInstance
;
184 typedef _ATL_BASE_MODULE70 _ATL_BASE_MODULE
;
186 class CAtlBaseModule
: public _ATL_BASE_MODULE
189 static bool m_bInitFailed
;
193 cbSize
= sizeof(_ATL_BASE_MODULE
);
194 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (LPCWSTR
)this, &m_hInst
);
195 m_hInstResource
= m_hInst
;
198 HINSTANCE
GetModuleInstance()
203 HINSTANCE
GetResourceInstance()
205 return m_hInstResource
;
208 HINSTANCE
SetResourceInstance(HINSTANCE hInst
)
210 return static_cast< HINSTANCE
>(InterlockedExchangePointer((void**)&m_hInstResource
, hInst
));
213 HINSTANCE
GetHInstanceAt(int i
);
216 extern CAtlBaseModule _AtlBaseModule
;
220 // String Resource helper functions
223 #pragma warning(push)
224 #pragma warning(disable:4200)
226 struct ATLSTRINGRESOURCEIMAGE
235 inline const ATLSTRINGRESOURCEIMAGE
* _AtlGetStringResourceImage(
236 _In_ HINSTANCE hInstance
,
237 _In_ HRSRC hResource
,
240 const ATLSTRINGRESOURCEIMAGE
* pImage
;
241 const ATLSTRINGRESOURCEIMAGE
* pImageEnd
;
246 hGlobal
= ::LoadResource(hInstance
, hResource
);
247 if (hGlobal
== NULL
) return NULL
;
249 pImage
= (const ATLSTRINGRESOURCEIMAGE
*)::LockResource(hGlobal
);
250 if (pImage
== NULL
) return NULL
;
252 nResourceSize
= ::SizeofResource(hInstance
, hResource
);
253 pImageEnd
= (const ATLSTRINGRESOURCEIMAGE
*)(LPBYTE(pImage
) + nResourceSize
);
254 iIndex
= id
& 0x000f;
256 while ((iIndex
> 0) && (pImage
< pImageEnd
))
258 pImage
= (const ATLSTRINGRESOURCEIMAGE
*)(LPBYTE(pImage
) + (sizeof(ATLSTRINGRESOURCEIMAGE
) + (pImage
->nLength
* sizeof(WCHAR
))));
262 if (pImage
>= pImageEnd
) return NULL
;
263 if (pImage
->nLength
== 0) return NULL
;
268 inline const ATLSTRINGRESOURCEIMAGE
* AtlGetStringResourceImage(
269 _In_ HINSTANCE hInstance
,
270 _In_ UINT id
) throw()
273 hResource
= ::FindResourceW(hInstance
, MAKEINTRESOURCEW((((id
>> 4) + 1) & static_cast<WORD
>(~0))), (LPWSTR
)RT_STRING
);
274 if (hResource
== NULL
) return NULL
;
275 return _AtlGetStringResourceImage(hInstance
, hResource
, id
);
278 inline const ATLSTRINGRESOURCEIMAGE
* AtlGetStringResourceImage(
279 _In_ HINSTANCE hInstance
,
284 hResource
= ::FindResourceExW(hInstance
, (LPWSTR
)RT_STRING
, MAKEINTRESOURCEW((((id
>> 4) + 1) & static_cast<WORD
>(~0))), wLanguage
);
285 if (hResource
== NULL
) return NULL
;
286 return _AtlGetStringResourceImage(hInstance
, hResource
, id
);
289 inline HINSTANCE
AtlFindStringResourceInstance(
293 const ATLSTRINGRESOURCEIMAGE
* strRes
= NULL
;
294 HINSTANCE hInst
= _AtlBaseModule
.GetHInstanceAt(0);
296 for (int i
= 1; hInst
!= NULL
&& strRes
== NULL
; hInst
= _AtlBaseModule
.GetHInstanceAt(i
++))
298 strRes
= AtlGetStringResourceImage(hInst
, nID
, wLanguage
);
299 if (strRes
!= NULL
) return hInst
;