From 02b79d0d640c718358cf953f9a1d15ea35c83429 Mon Sep 17 00:00:00 2001 From: "Brian R. Bondy" Date: Fri, 22 Mar 2013 15:34:36 -0400 Subject: [PATCH] Bug 844954 - Add LoadLibrary RAII helper and System32 LoadLirary helper for use anywhere. r=jimm --- xpcom/base/nsWindowsHelpers.h | 80 ++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 16 deletions(-) diff --git a/xpcom/base/nsWindowsHelpers.h b/xpcom/base/nsWindowsHelpers.h index 4a75ea9e5e7e..331a58477b97 100644 --- a/xpcom/base/nsWindowsHelpers.h +++ b/xpcom/base/nsWindowsHelpers.h @@ -14,13 +14,13 @@ class nsAutoRefTraits { public: typedef HKEY RawRef; - static HKEY Void() - { - return NULL; + static HKEY Void() + { + return NULL; } - static void Release(RawRef aFD) - { + static void Release(RawRef aFD) + { if (aFD != Void()) { RegCloseKey(aFD); } @@ -32,13 +32,13 @@ class nsAutoRefTraits { public: typedef SC_HANDLE RawRef; - static SC_HANDLE Void() - { - return NULL; + static SC_HANDLE Void() + { + return NULL; } - static void Release(RawRef aFD) - { + static void Release(RawRef aFD) + { if (aFD != Void()) { CloseServiceHandle(aFD); } @@ -51,26 +51,26 @@ class nsSimpleRef protected: typedef HANDLE RawRef; - nsSimpleRef() : mRawRef(NULL) + nsSimpleRef() : mRawRef(NULL) { } - nsSimpleRef(RawRef aRawRef) : mRawRef(aRawRef) + nsSimpleRef(RawRef aRawRef) : mRawRef(aRawRef) { } - bool HaveResource() const + bool HaveResource() const { return mRawRef != NULL && mRawRef != INVALID_HANDLE_VALUE; } public: - RawRef get() const + RawRef get() const { return mRawRef; } - static void Release(RawRef aRawRef) + static void Release(RawRef aRawRef) { if (aRawRef != NULL && aRawRef != INVALID_HANDLE_VALUE) { CloseHandle(aRawRef); @@ -79,9 +79,29 @@ public: RawRef mRawRef; }; + +template<> +class nsAutoRefTraits +{ +public: + typedef HMODULE RawRef; + static RawRef Void() + { + return NULL; + } + + static void Release(RawRef aFD) + { + if (aFD != Void()) { + FreeLibrary(aFD); + } + } +}; + typedef nsAutoRef nsAutoRegKey; typedef nsAutoRef nsAutoServiceHandle; typedef nsAutoRef nsAutoHandle; +typedef nsAutoRef nsModuleHandle; namespace { @@ -110,7 +130,7 @@ namespace } typedef BOOL (WINAPI* IsImmersiveProcessFunc)(HANDLE process); - IsImmersiveProcessFunc IsImmersiveProcessPtr = + IsImmersiveProcessFunc IsImmersiveProcessPtr = (IsImmersiveProcessFunc)GetProcAddress(user32DLL, "IsImmersiveProcess"); FreeLibrary(user32DLL); @@ -124,6 +144,34 @@ namespace alreadyChecked = true; return isMetro; } + + HMODULE + LoadLibrarySystem32(LPCWSTR module) + { + WCHAR systemPath[MAX_PATH + 1] = { L'\0' }; + + // If GetSystemPath fails we accept that we'll load the DLLs from the + // normal search path. + GetSystemDirectoryW(systemPath, MAX_PATH + 1); + size_t systemDirLen = wcslen(systemPath); + + // Make the system directory path terminate with a slash + if (systemDirLen && systemPath[systemDirLen - 1] != L'\\') { + systemPath[systemDirLen] = L'\\'; + ++systemDirLen; + // No need to re-NULL terminate + } + + size_t fileLen = wcslen(module); + wcsncpy(systemPath + systemDirLen, module, + MAX_PATH - systemDirLen); + if (systemDirLen + fileLen <= MAX_PATH) { + systemPath[systemDirLen + fileLen] = L'\0'; + } else { + systemPath[MAX_PATH] = L'\0'; + } + return LoadLibraryW(systemPath); + } } #endif