1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/base/nsWindowsHelpers.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,187 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef nsWindowsHelpers_h 1.9 +#define nsWindowsHelpers_h 1.10 + 1.11 +#include <windows.h> 1.12 +#include "nsAutoRef.h" 1.13 +#include "nscore.h" 1.14 + 1.15 +// ---------------------------------------------------------------------------- 1.16 +// Critical Section helper class 1.17 +// ---------------------------------------------------------------------------- 1.18 + 1.19 +class AutoCriticalSection 1.20 +{ 1.21 +public: 1.22 + AutoCriticalSection(LPCRITICAL_SECTION section) 1.23 + : mSection(section) 1.24 + { 1.25 + ::EnterCriticalSection(mSection); 1.26 + } 1.27 + ~AutoCriticalSection() 1.28 + { 1.29 + ::LeaveCriticalSection(mSection); 1.30 + } 1.31 +private: 1.32 + LPCRITICAL_SECTION mSection; 1.33 +}; 1.34 + 1.35 +template<> 1.36 +class nsAutoRefTraits<HKEY> 1.37 +{ 1.38 +public: 1.39 + typedef HKEY RawRef; 1.40 + static HKEY Void() 1.41 + { 1.42 + return nullptr; 1.43 + } 1.44 + 1.45 + static void Release(RawRef aFD) 1.46 + { 1.47 + if (aFD != Void()) { 1.48 + RegCloseKey(aFD); 1.49 + } 1.50 + } 1.51 +}; 1.52 + 1.53 +template<> 1.54 +class nsAutoRefTraits<SC_HANDLE> 1.55 +{ 1.56 +public: 1.57 + typedef SC_HANDLE RawRef; 1.58 + static SC_HANDLE Void() 1.59 + { 1.60 + return nullptr; 1.61 + } 1.62 + 1.63 + static void Release(RawRef aFD) 1.64 + { 1.65 + if (aFD != Void()) { 1.66 + CloseServiceHandle(aFD); 1.67 + } 1.68 + } 1.69 +}; 1.70 + 1.71 +template<> 1.72 +class nsSimpleRef<HANDLE> 1.73 +{ 1.74 +protected: 1.75 + typedef HANDLE RawRef; 1.76 + 1.77 + nsSimpleRef() : mRawRef(nullptr) 1.78 + { 1.79 + } 1.80 + 1.81 + nsSimpleRef(RawRef aRawRef) : mRawRef(aRawRef) 1.82 + { 1.83 + } 1.84 + 1.85 + bool HaveResource() const 1.86 + { 1.87 + return mRawRef != nullptr && mRawRef != INVALID_HANDLE_VALUE; 1.88 + } 1.89 + 1.90 +public: 1.91 + RawRef get() const 1.92 + { 1.93 + return mRawRef; 1.94 + } 1.95 + 1.96 + static void Release(RawRef aRawRef) 1.97 + { 1.98 + if (aRawRef != nullptr && aRawRef != INVALID_HANDLE_VALUE) { 1.99 + CloseHandle(aRawRef); 1.100 + } 1.101 + } 1.102 + RawRef mRawRef; 1.103 +}; 1.104 + 1.105 + 1.106 +template<> 1.107 +class nsAutoRefTraits<HMODULE> 1.108 +{ 1.109 +public: 1.110 + typedef HMODULE RawRef; 1.111 + static RawRef Void() 1.112 + { 1.113 + return nullptr; 1.114 + } 1.115 + 1.116 + static void Release(RawRef aFD) 1.117 + { 1.118 + if (aFD != Void()) { 1.119 + FreeLibrary(aFD); 1.120 + } 1.121 + } 1.122 +}; 1.123 + 1.124 +typedef nsAutoRef<HKEY> nsAutoRegKey; 1.125 +typedef nsAutoRef<SC_HANDLE> nsAutoServiceHandle; 1.126 +typedef nsAutoRef<HANDLE> nsAutoHandle; 1.127 +typedef nsAutoRef<HMODULE> nsModuleHandle; 1.128 + 1.129 +namespace 1.130 +{ 1.131 + bool 1.132 + IsRunningInWindowsMetro() 1.133 + { 1.134 + static bool alreadyChecked = false; 1.135 + static bool isMetro = false; 1.136 + if (alreadyChecked) { 1.137 + return isMetro; 1.138 + } 1.139 + 1.140 + HMODULE user32DLL = LoadLibraryW(L"user32.dll"); 1.141 + if (!user32DLL) { 1.142 + return false; 1.143 + } 1.144 + 1.145 + typedef BOOL (WINAPI* IsImmersiveProcessFunc)(HANDLE process); 1.146 + IsImmersiveProcessFunc IsImmersiveProcessPtr = 1.147 + (IsImmersiveProcessFunc)GetProcAddress(user32DLL, 1.148 + "IsImmersiveProcess"); 1.149 + FreeLibrary(user32DLL); 1.150 + if (!IsImmersiveProcessPtr) { 1.151 + // isMetro is already set to false. 1.152 + alreadyChecked = true; 1.153 + return false; 1.154 + } 1.155 + 1.156 + isMetro = IsImmersiveProcessPtr(GetCurrentProcess()); 1.157 + alreadyChecked = true; 1.158 + return isMetro; 1.159 + } 1.160 + 1.161 + HMODULE 1.162 + LoadLibrarySystem32(LPCWSTR module) 1.163 + { 1.164 + WCHAR systemPath[MAX_PATH + 1] = { L'\0' }; 1.165 + 1.166 + // If GetSystemPath fails we accept that we'll load the DLLs from the 1.167 + // normal search path. 1.168 + GetSystemDirectoryW(systemPath, MAX_PATH + 1); 1.169 + size_t systemDirLen = wcslen(systemPath); 1.170 + 1.171 + // Make the system directory path terminate with a slash 1.172 + if (systemDirLen && systemPath[systemDirLen - 1] != L'\\') { 1.173 + systemPath[systemDirLen] = L'\\'; 1.174 + ++systemDirLen; 1.175 + // No need to re-nullptr terminate 1.176 + } 1.177 + 1.178 + size_t fileLen = wcslen(module); 1.179 + wcsncpy(systemPath + systemDirLen, module, 1.180 + MAX_PATH - systemDirLen); 1.181 + if (systemDirLen + fileLen <= MAX_PATH) { 1.182 + systemPath[systemDirLen + fileLen] = L'\0'; 1.183 + } else { 1.184 + systemPath[MAX_PATH] = L'\0'; 1.185 + } 1.186 + return LoadLibraryW(systemPath); 1.187 + } 1.188 +} 1.189 + 1.190 +#endif