Bug 844954 - Add LoadLibrary RAII helper and System32 LoadLirary helper for use anywhere. r=jimm

This commit is contained in:
Brian R. Bondy 2013-03-22 15:34:36 -04:00
parent 3ba4d66119
commit eb80afdd6b

View File

@ -14,13 +14,13 @@ class nsAutoRefTraits<HKEY>
{
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<SC_HANDLE>
{
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<HANDLE>
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<HMODULE>
{
public:
typedef HMODULE RawRef;
static RawRef Void()
{
return NULL;
}
static void Release(RawRef aFD)
{
if (aFD != Void()) {
FreeLibrary(aFD);
}
}
};
typedef nsAutoRef<HKEY> nsAutoRegKey;
typedef nsAutoRef<SC_HANDLE> nsAutoServiceHandle;
typedef nsAutoRef<HANDLE> nsAutoHandle;
typedef nsAutoRef<HMODULE> 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