2014-05-05 21:30:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-01-05 08:19:14 +04:00
|
|
|
|
|
|
|
#ifndef nsWindowsHelpers_h
|
|
|
|
#define nsWindowsHelpers_h
|
|
|
|
|
2012-07-25 18:23:26 +04:00
|
|
|
#include <windows.h>
|
2012-01-05 08:19:14 +04:00
|
|
|
#include "nsAutoRef.h"
|
|
|
|
#include "nscore.h"
|
|
|
|
|
2013-10-18 22:24:51 +04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Critical Section helper class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class AutoCriticalSection
|
|
|
|
{
|
|
|
|
public:
|
2014-05-13 21:41:38 +04:00
|
|
|
AutoCriticalSection(LPCRITICAL_SECTION aSection)
|
|
|
|
: mSection(aSection)
|
2013-10-18 22:24:51 +04:00
|
|
|
{
|
|
|
|
::EnterCriticalSection(mSection);
|
|
|
|
}
|
|
|
|
~AutoCriticalSection()
|
|
|
|
{
|
|
|
|
::LeaveCriticalSection(mSection);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
LPCRITICAL_SECTION mSection;
|
|
|
|
};
|
|
|
|
|
2012-01-05 08:19:14 +04:00
|
|
|
template<>
|
|
|
|
class nsAutoRefTraits<HKEY>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef HKEY RawRef;
|
2013-03-22 23:34:36 +04:00
|
|
|
static HKEY Void()
|
|
|
|
{
|
2013-10-11 00:41:00 +04:00
|
|
|
return nullptr;
|
2012-01-05 08:19:14 +04:00
|
|
|
}
|
|
|
|
|
2013-03-22 23:34:36 +04:00
|
|
|
static void Release(RawRef aFD)
|
|
|
|
{
|
2012-01-05 08:19:14 +04:00
|
|
|
if (aFD != Void()) {
|
|
|
|
RegCloseKey(aFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-01 21:48:26 +03:00
|
|
|
template<>
|
|
|
|
class nsAutoRefTraits<HDC>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef HDC RawRef;
|
|
|
|
static HDC Void()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Release(RawRef aFD)
|
|
|
|
{
|
|
|
|
if (aFD != Void()) {
|
|
|
|
::DeleteDC(aFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class nsAutoRefTraits<HBRUSH>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef HBRUSH RawRef;
|
|
|
|
static HBRUSH Void()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Release(RawRef aFD)
|
|
|
|
{
|
|
|
|
if (aFD != Void()) {
|
|
|
|
::DeleteObject(aFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class nsAutoRefTraits<HRGN>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef HRGN RawRef;
|
|
|
|
static HRGN Void()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Release(RawRef aFD)
|
|
|
|
{
|
|
|
|
if (aFD != Void()) {
|
|
|
|
::DeleteObject(aFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class nsAutoRefTraits<HBITMAP>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef HBITMAP RawRef;
|
|
|
|
static HBITMAP Void()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Release(RawRef aFD)
|
|
|
|
{
|
|
|
|
if (aFD != Void()) {
|
|
|
|
::DeleteObject(aFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-05 08:19:14 +04:00
|
|
|
template<>
|
|
|
|
class nsAutoRefTraits<SC_HANDLE>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef SC_HANDLE RawRef;
|
2013-03-22 23:34:36 +04:00
|
|
|
static SC_HANDLE Void()
|
|
|
|
{
|
2013-10-11 00:41:00 +04:00
|
|
|
return nullptr;
|
2012-01-05 08:19:14 +04:00
|
|
|
}
|
|
|
|
|
2013-03-22 23:34:36 +04:00
|
|
|
static void Release(RawRef aFD)
|
|
|
|
{
|
2012-01-05 08:19:14 +04:00
|
|
|
if (aFD != Void()) {
|
|
|
|
CloseServiceHandle(aFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
class nsSimpleRef<HANDLE>
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
typedef HANDLE RawRef;
|
|
|
|
|
2013-10-11 00:41:00 +04:00
|
|
|
nsSimpleRef() : mRawRef(nullptr)
|
2012-01-05 08:19:14 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-22 23:34:36 +04:00
|
|
|
nsSimpleRef(RawRef aRawRef) : mRawRef(aRawRef)
|
2012-01-05 08:19:14 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-03-22 23:34:36 +04:00
|
|
|
bool HaveResource() const
|
2012-01-05 08:19:14 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
return mRawRef && mRawRef != INVALID_HANDLE_VALUE;
|
2012-01-05 08:19:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2013-03-22 23:34:36 +04:00
|
|
|
RawRef get() const
|
2012-01-05 08:19:14 +04:00
|
|
|
{
|
|
|
|
return mRawRef;
|
|
|
|
}
|
|
|
|
|
2013-03-22 23:34:36 +04:00
|
|
|
static void Release(RawRef aRawRef)
|
2012-01-05 08:19:14 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (aRawRef && aRawRef != INVALID_HANDLE_VALUE) {
|
2012-01-05 08:19:14 +04:00
|
|
|
CloseHandle(aRawRef);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RawRef mRawRef;
|
|
|
|
};
|
|
|
|
|
2013-03-22 23:34:36 +04:00
|
|
|
|
|
|
|
template<>
|
|
|
|
class nsAutoRefTraits<HMODULE>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef HMODULE RawRef;
|
|
|
|
static RawRef Void()
|
|
|
|
{
|
2013-10-11 00:41:00 +04:00
|
|
|
return nullptr;
|
2013-03-22 23:34:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Release(RawRef aFD)
|
|
|
|
{
|
|
|
|
if (aFD != Void()) {
|
|
|
|
FreeLibrary(aFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-07 00:36:54 +03:00
|
|
|
|
|
|
|
template<>
|
|
|
|
class nsAutoRefTraits<DEVMODEW*>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef DEVMODEW* RawRef;
|
|
|
|
static RawRef Void()
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Release(RawRef aDevMode)
|
|
|
|
{
|
|
|
|
if (aDevMode != Void()) {
|
|
|
|
::HeapFree(::GetProcessHeap(), 0, aDevMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-05 08:19:14 +04:00
|
|
|
typedef nsAutoRef<HKEY> nsAutoRegKey;
|
2016-03-01 21:48:26 +03:00
|
|
|
typedef nsAutoRef<HDC> nsAutoHDC;
|
|
|
|
typedef nsAutoRef<HBRUSH> nsAutoBrush;
|
|
|
|
typedef nsAutoRef<HRGN> nsAutoRegion;
|
|
|
|
typedef nsAutoRef<HBITMAP> nsAutoBitmap;
|
2012-01-05 08:19:14 +04:00
|
|
|
typedef nsAutoRef<SC_HANDLE> nsAutoServiceHandle;
|
|
|
|
typedef nsAutoRef<HANDLE> nsAutoHandle;
|
2013-03-22 23:34:36 +04:00
|
|
|
typedef nsAutoRef<HMODULE> nsModuleHandle;
|
2015-08-07 00:36:54 +03:00
|
|
|
typedef nsAutoRef<DEVMODEW*> nsAutoDevMode;
|
2012-01-05 08:19:14 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
namespace {
|
|
|
|
|
2014-11-24 13:19:36 +03:00
|
|
|
HMODULE inline
|
2014-05-13 21:41:38 +04:00
|
|
|
LoadLibrarySystem32(LPCWSTR aModule)
|
2014-05-05 21:30:39 +04:00
|
|
|
{
|
|
|
|
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-nullptr terminate
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
size_t fileLen = wcslen(aModule);
|
|
|
|
wcsncpy(systemPath + systemDirLen, aModule,
|
2014-05-05 21:30:39 +04:00
|
|
|
MAX_PATH - systemDirLen);
|
|
|
|
if (systemDirLen + fileLen <= MAX_PATH) {
|
|
|
|
systemPath[systemDirLen + fileLen] = L'\0';
|
|
|
|
} else {
|
|
|
|
systemPath[MAX_PATH] = L'\0';
|
2013-03-22 23:34:36 +04:00
|
|
|
}
|
2014-05-05 21:30:39 +04:00
|
|
|
return LoadLibraryW(systemPath);
|
|
|
|
}
|
2014-05-13 21:41:38 +04:00
|
|
|
|
2012-07-25 18:23:26 +04:00
|
|
|
}
|
|
|
|
|
2012-01-05 08:19:14 +04:00
|
|
|
#endif
|