Bug 959405 - Please update the Buri Moz-central, 1.3, 1.2 with the latest blobs from...
[gecko.git] / xpcom / base / nsWindowsHelpers.h
blob411ab2106cabd422869429dbdae4221643b5aaa5
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
8 #include <windows.h>
9 #include "nsAutoRef.h"
10 #include "nscore.h"
12 // ----------------------------------------------------------------------------
13 // Critical Section helper class
14 // ----------------------------------------------------------------------------
16 class AutoCriticalSection
18 public:
19 AutoCriticalSection(LPCRITICAL_SECTION section)
20 : mSection(section)
22 ::EnterCriticalSection(mSection);
24 ~AutoCriticalSection()
26 ::LeaveCriticalSection(mSection);
28 private:
29 LPCRITICAL_SECTION mSection;
32 template<>
33 class nsAutoRefTraits<HKEY>
35 public:
36 typedef HKEY RawRef;
37 static HKEY Void()
39 return nullptr;
42 static void Release(RawRef aFD)
44 if (aFD != Void()) {
45 RegCloseKey(aFD);
50 template<>
51 class nsAutoRefTraits<SC_HANDLE>
53 public:
54 typedef SC_HANDLE RawRef;
55 static SC_HANDLE Void()
57 return nullptr;
60 static void Release(RawRef aFD)
62 if (aFD != Void()) {
63 CloseServiceHandle(aFD);
68 template<>
69 class nsSimpleRef<HANDLE>
71 protected:
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;
87 public:
88 RawRef get() const
90 return mRawRef;
93 static void Release(RawRef aRawRef)
95 if (aRawRef != nullptr && aRawRef != INVALID_HANDLE_VALUE) {
96 CloseHandle(aRawRef);
99 RawRef mRawRef;
103 template<>
104 class nsAutoRefTraits<HMODULE>
106 public:
107 typedef HMODULE RawRef;
108 static RawRef Void()
110 return nullptr;
113 static void Release(RawRef aFD)
115 if (aFD != Void()) {
116 FreeLibrary(aFD);
121 typedef nsAutoRef<HKEY> nsAutoRegKey;
122 typedef nsAutoRef<SC_HANDLE> nsAutoServiceHandle;
123 typedef nsAutoRef<HANDLE> nsAutoHandle;
124 typedef nsAutoRef<HMODULE> nsModuleHandle;
126 namespace
128 bool
129 IsRunningInWindowsMetro()
131 static bool alreadyChecked = false;
132 static bool isMetro = false;
133 if (alreadyChecked) {
134 return isMetro;
137 HMODULE user32DLL = LoadLibraryW(L"user32.dll");
138 if (!user32DLL) {
139 return false;
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;
150 return false;
153 isMetro = IsImmersiveProcessPtr(GetCurrentProcess());
154 alreadyChecked = true;
155 return isMetro;
158 HMODULE
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'\\';
171 ++systemDirLen;
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';
180 } else {
181 systemPath[MAX_PATH] = L'\0';
183 return LoadLibraryW(systemPath);
187 #endif