|
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/. */ |
|
4 |
|
5 #ifndef nsWindowsHelpers_h |
|
6 #define nsWindowsHelpers_h |
|
7 |
|
8 #include <windows.h> |
|
9 #include "nsAutoRef.h" |
|
10 #include "nscore.h" |
|
11 |
|
12 // ---------------------------------------------------------------------------- |
|
13 // Critical Section helper class |
|
14 // ---------------------------------------------------------------------------- |
|
15 |
|
16 class AutoCriticalSection |
|
17 { |
|
18 public: |
|
19 AutoCriticalSection(LPCRITICAL_SECTION section) |
|
20 : mSection(section) |
|
21 { |
|
22 ::EnterCriticalSection(mSection); |
|
23 } |
|
24 ~AutoCriticalSection() |
|
25 { |
|
26 ::LeaveCriticalSection(mSection); |
|
27 } |
|
28 private: |
|
29 LPCRITICAL_SECTION mSection; |
|
30 }; |
|
31 |
|
32 template<> |
|
33 class nsAutoRefTraits<HKEY> |
|
34 { |
|
35 public: |
|
36 typedef HKEY RawRef; |
|
37 static HKEY Void() |
|
38 { |
|
39 return nullptr; |
|
40 } |
|
41 |
|
42 static void Release(RawRef aFD) |
|
43 { |
|
44 if (aFD != Void()) { |
|
45 RegCloseKey(aFD); |
|
46 } |
|
47 } |
|
48 }; |
|
49 |
|
50 template<> |
|
51 class nsAutoRefTraits<SC_HANDLE> |
|
52 { |
|
53 public: |
|
54 typedef SC_HANDLE RawRef; |
|
55 static SC_HANDLE Void() |
|
56 { |
|
57 return nullptr; |
|
58 } |
|
59 |
|
60 static void Release(RawRef aFD) |
|
61 { |
|
62 if (aFD != Void()) { |
|
63 CloseServiceHandle(aFD); |
|
64 } |
|
65 } |
|
66 }; |
|
67 |
|
68 template<> |
|
69 class nsSimpleRef<HANDLE> |
|
70 { |
|
71 protected: |
|
72 typedef HANDLE RawRef; |
|
73 |
|
74 nsSimpleRef() : mRawRef(nullptr) |
|
75 { |
|
76 } |
|
77 |
|
78 nsSimpleRef(RawRef aRawRef) : mRawRef(aRawRef) |
|
79 { |
|
80 } |
|
81 |
|
82 bool HaveResource() const |
|
83 { |
|
84 return mRawRef != nullptr && mRawRef != INVALID_HANDLE_VALUE; |
|
85 } |
|
86 |
|
87 public: |
|
88 RawRef get() const |
|
89 { |
|
90 return mRawRef; |
|
91 } |
|
92 |
|
93 static void Release(RawRef aRawRef) |
|
94 { |
|
95 if (aRawRef != nullptr && aRawRef != INVALID_HANDLE_VALUE) { |
|
96 CloseHandle(aRawRef); |
|
97 } |
|
98 } |
|
99 RawRef mRawRef; |
|
100 }; |
|
101 |
|
102 |
|
103 template<> |
|
104 class nsAutoRefTraits<HMODULE> |
|
105 { |
|
106 public: |
|
107 typedef HMODULE RawRef; |
|
108 static RawRef Void() |
|
109 { |
|
110 return nullptr; |
|
111 } |
|
112 |
|
113 static void Release(RawRef aFD) |
|
114 { |
|
115 if (aFD != Void()) { |
|
116 FreeLibrary(aFD); |
|
117 } |
|
118 } |
|
119 }; |
|
120 |
|
121 typedef nsAutoRef<HKEY> nsAutoRegKey; |
|
122 typedef nsAutoRef<SC_HANDLE> nsAutoServiceHandle; |
|
123 typedef nsAutoRef<HANDLE> nsAutoHandle; |
|
124 typedef nsAutoRef<HMODULE> nsModuleHandle; |
|
125 |
|
126 namespace |
|
127 { |
|
128 bool |
|
129 IsRunningInWindowsMetro() |
|
130 { |
|
131 static bool alreadyChecked = false; |
|
132 static bool isMetro = false; |
|
133 if (alreadyChecked) { |
|
134 return isMetro; |
|
135 } |
|
136 |
|
137 HMODULE user32DLL = LoadLibraryW(L"user32.dll"); |
|
138 if (!user32DLL) { |
|
139 return false; |
|
140 } |
|
141 |
|
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; |
|
151 } |
|
152 |
|
153 isMetro = IsImmersiveProcessPtr(GetCurrentProcess()); |
|
154 alreadyChecked = true; |
|
155 return isMetro; |
|
156 } |
|
157 |
|
158 HMODULE |
|
159 LoadLibrarySystem32(LPCWSTR module) |
|
160 { |
|
161 WCHAR systemPath[MAX_PATH + 1] = { L'\0' }; |
|
162 |
|
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); |
|
167 |
|
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 |
|
173 } |
|
174 |
|
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'; |
|
182 } |
|
183 return LoadLibraryW(systemPath); |
|
184 } |
|
185 } |
|
186 |
|
187 #endif |