1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsWindowsHelpers_h
6 #define nsWindowsHelpers_h
12 // ----------------------------------------------------------------------------
13 // Critical Section helper class
14 // ----------------------------------------------------------------------------
16 class AutoCriticalSection
19 AutoCriticalSection(LPCRITICAL_SECTION section
)
22 ::EnterCriticalSection(mSection
);
24 ~AutoCriticalSection()
26 ::LeaveCriticalSection(mSection
);
29 LPCRITICAL_SECTION mSection
;
33 class nsAutoRefTraits
<HKEY
>
42 static void Release(RawRef aFD
)
51 class nsAutoRefTraits
<SC_HANDLE
>
54 typedef SC_HANDLE RawRef
;
55 static SC_HANDLE
Void()
60 static void Release(RawRef aFD
)
63 CloseServiceHandle(aFD
);
69 class nsSimpleRef
<HANDLE
>
72 typedef HANDLE RawRef
;
74 nsSimpleRef() : mRawRef(nullptr)
78 nsSimpleRef(RawRef aRawRef
) : mRawRef(aRawRef
)
82 bool HaveResource() const
84 return mRawRef
!= nullptr && mRawRef
!= INVALID_HANDLE_VALUE
;
93 static void Release(RawRef aRawRef
)
95 if (aRawRef
!= nullptr && aRawRef
!= INVALID_HANDLE_VALUE
) {
104 class nsAutoRefTraits
<HMODULE
>
107 typedef HMODULE RawRef
;
113 static void Release(RawRef aFD
)
121 typedef nsAutoRef
<HKEY
> nsAutoRegKey
;
122 typedef nsAutoRef
<SC_HANDLE
> nsAutoServiceHandle
;
123 typedef nsAutoRef
<HANDLE
> nsAutoHandle
;
124 typedef nsAutoRef
<HMODULE
> nsModuleHandle
;
129 IsRunningInWindowsMetro()
131 static bool alreadyChecked
= false;
132 static bool isMetro
= false;
133 if (alreadyChecked
) {
137 HMODULE user32DLL
= LoadLibraryW(L
"user32.dll");
142 typedef BOOL (WINAPI
* IsImmersiveProcessFunc
)(HANDLE process
);
143 IsImmersiveProcessFunc IsImmersiveProcessPtr
=
144 (IsImmersiveProcessFunc
)GetProcAddress(user32DLL
,
145 "IsImmersiveProcess");
146 FreeLibrary(user32DLL
);
147 if (!IsImmersiveProcessPtr
) {
148 // isMetro is already set to false.
149 alreadyChecked
= true;
153 isMetro
= IsImmersiveProcessPtr(GetCurrentProcess());
154 alreadyChecked
= true;
159 LoadLibrarySystem32(LPCWSTR module
)
161 WCHAR systemPath
[MAX_PATH
+ 1] = { L
'\0' };
163 // If GetSystemPath fails we accept that we'll load the DLLs from the
164 // normal search path.
165 GetSystemDirectoryW(systemPath
, MAX_PATH
+ 1);
166 size_t systemDirLen
= wcslen(systemPath
);
168 // Make the system directory path terminate with a slash
169 if (systemDirLen
&& systemPath
[systemDirLen
- 1] != L
'\\') {
170 systemPath
[systemDirLen
] = L
'\\';
172 // No need to re-nullptr terminate
175 size_t fileLen
= wcslen(module
);
176 wcsncpy(systemPath
+ systemDirLen
, module
,
177 MAX_PATH
- systemDirLen
);
178 if (systemDirLen
+ fileLen
<= MAX_PATH
) {
179 systemPath
[systemDirLen
+ fileLen
] = L
'\0';
181 systemPath
[MAX_PATH
] = L
'\0';
183 return LoadLibraryW(systemPath
);