michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsWindowsHelpers_h michael@0: #define nsWindowsHelpers_h michael@0: michael@0: #include michael@0: #include "nsAutoRef.h" michael@0: #include "nscore.h" michael@0: michael@0: // ---------------------------------------------------------------------------- michael@0: // Critical Section helper class michael@0: // ---------------------------------------------------------------------------- michael@0: michael@0: class AutoCriticalSection michael@0: { michael@0: public: michael@0: AutoCriticalSection(LPCRITICAL_SECTION section) michael@0: : mSection(section) michael@0: { michael@0: ::EnterCriticalSection(mSection); michael@0: } michael@0: ~AutoCriticalSection() michael@0: { michael@0: ::LeaveCriticalSection(mSection); michael@0: } michael@0: private: michael@0: LPCRITICAL_SECTION mSection; michael@0: }; michael@0: michael@0: template<> michael@0: class nsAutoRefTraits michael@0: { michael@0: public: michael@0: typedef HKEY RawRef; michael@0: static HKEY Void() michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: static void Release(RawRef aFD) michael@0: { michael@0: if (aFD != Void()) { michael@0: RegCloseKey(aFD); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: template<> michael@0: class nsAutoRefTraits michael@0: { michael@0: public: michael@0: typedef SC_HANDLE RawRef; michael@0: static SC_HANDLE Void() michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: static void Release(RawRef aFD) michael@0: { michael@0: if (aFD != Void()) { michael@0: CloseServiceHandle(aFD); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: template<> michael@0: class nsSimpleRef michael@0: { michael@0: protected: michael@0: typedef HANDLE RawRef; michael@0: michael@0: nsSimpleRef() : mRawRef(nullptr) michael@0: { michael@0: } michael@0: michael@0: nsSimpleRef(RawRef aRawRef) : mRawRef(aRawRef) michael@0: { michael@0: } michael@0: michael@0: bool HaveResource() const michael@0: { michael@0: return mRawRef != nullptr && mRawRef != INVALID_HANDLE_VALUE; michael@0: } michael@0: michael@0: public: michael@0: RawRef get() const michael@0: { michael@0: return mRawRef; michael@0: } michael@0: michael@0: static void Release(RawRef aRawRef) michael@0: { michael@0: if (aRawRef != nullptr && aRawRef != INVALID_HANDLE_VALUE) { michael@0: CloseHandle(aRawRef); michael@0: } michael@0: } michael@0: RawRef mRawRef; michael@0: }; michael@0: michael@0: michael@0: template<> michael@0: class nsAutoRefTraits michael@0: { michael@0: public: michael@0: typedef HMODULE RawRef; michael@0: static RawRef Void() michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: static void Release(RawRef aFD) michael@0: { michael@0: if (aFD != Void()) { michael@0: FreeLibrary(aFD); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: typedef nsAutoRef nsAutoRegKey; michael@0: typedef nsAutoRef nsAutoServiceHandle; michael@0: typedef nsAutoRef nsAutoHandle; michael@0: typedef nsAutoRef nsModuleHandle; michael@0: michael@0: namespace michael@0: { michael@0: bool michael@0: IsRunningInWindowsMetro() michael@0: { michael@0: static bool alreadyChecked = false; michael@0: static bool isMetro = false; michael@0: if (alreadyChecked) { michael@0: return isMetro; michael@0: } michael@0: michael@0: HMODULE user32DLL = LoadLibraryW(L"user32.dll"); michael@0: if (!user32DLL) { michael@0: return false; michael@0: } michael@0: michael@0: typedef BOOL (WINAPI* IsImmersiveProcessFunc)(HANDLE process); michael@0: IsImmersiveProcessFunc IsImmersiveProcessPtr = michael@0: (IsImmersiveProcessFunc)GetProcAddress(user32DLL, michael@0: "IsImmersiveProcess"); michael@0: FreeLibrary(user32DLL); michael@0: if (!IsImmersiveProcessPtr) { michael@0: // isMetro is already set to false. michael@0: alreadyChecked = true; michael@0: return false; michael@0: } michael@0: michael@0: isMetro = IsImmersiveProcessPtr(GetCurrentProcess()); michael@0: alreadyChecked = true; michael@0: return isMetro; michael@0: } michael@0: michael@0: HMODULE michael@0: LoadLibrarySystem32(LPCWSTR module) michael@0: { michael@0: WCHAR systemPath[MAX_PATH + 1] = { L'\0' }; michael@0: michael@0: // If GetSystemPath fails we accept that we'll load the DLLs from the michael@0: // normal search path. michael@0: GetSystemDirectoryW(systemPath, MAX_PATH + 1); michael@0: size_t systemDirLen = wcslen(systemPath); michael@0: michael@0: // Make the system directory path terminate with a slash michael@0: if (systemDirLen && systemPath[systemDirLen - 1] != L'\\') { michael@0: systemPath[systemDirLen] = L'\\'; michael@0: ++systemDirLen; michael@0: // No need to re-nullptr terminate michael@0: } michael@0: michael@0: size_t fileLen = wcslen(module); michael@0: wcsncpy(systemPath + systemDirLen, module, michael@0: MAX_PATH - systemDirLen); michael@0: if (systemDirLen + fileLen <= MAX_PATH) { michael@0: systemPath[systemDirLen + fileLen] = L'\0'; michael@0: } else { michael@0: systemPath[MAX_PATH] = L'\0'; michael@0: } michael@0: return LoadLibraryW(systemPath); michael@0: } michael@0: } michael@0: michael@0: #endif