Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / onlineupdate / source / update / inc / nsWindowsHelpers.h
blob76a3aeb6585c5c9525b310c186b86368273672d7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsWindowsHelpers_h
8 #define nsWindowsHelpers_h
10 #include <windows.h>
11 #include "nsAutoRef.h"
13 // Critical Section helper class
15 class AutoCriticalSection
17 public:
18 AutoCriticalSection(LPCRITICAL_SECTION aSection)
19 : mSection(aSection)
21 ::EnterCriticalSection(mSection);
23 ~AutoCriticalSection()
25 ::LeaveCriticalSection(mSection);
27 private:
28 LPCRITICAL_SECTION mSection;
31 template<>
32 class nsAutoRefTraits<HKEY>
34 public:
35 typedef HKEY RawRef;
36 static HKEY Void()
38 return nullptr;
41 static void Release(RawRef aFD)
43 if (aFD != Void()) {
44 RegCloseKey(aFD);
49 template<>
50 class nsAutoRefTraits<SC_HANDLE>
52 public:
53 typedef SC_HANDLE RawRef;
54 static SC_HANDLE Void()
56 return nullptr;
59 static void Release(RawRef aFD)
61 if (aFD != Void()) {
62 CloseServiceHandle(aFD);
67 template<>
68 class nsSimpleRef<HANDLE>
70 protected:
71 typedef HANDLE RawRef;
73 nsSimpleRef() : mRawRef(nullptr)
77 nsSimpleRef(RawRef aRawRef) : mRawRef(aRawRef)
81 bool HaveResource() const
83 return mRawRef && mRawRef != INVALID_HANDLE_VALUE;
86 public:
87 RawRef get() const
89 return mRawRef;
92 static void Release(RawRef aRawRef)
94 if (aRawRef && aRawRef != INVALID_HANDLE_VALUE) {
95 CloseHandle(aRawRef);
98 RawRef mRawRef;
102 template<>
103 class nsAutoRefTraits<HMODULE>
105 public:
106 typedef HMODULE RawRef;
107 static RawRef Void()
109 return nullptr;
112 static void Release(RawRef aFD)
114 if (aFD != Void()) {
115 FreeLibrary(aFD);
121 namespace {
123 HMODULE inline
124 LoadLibrarySystem32(LPCWSTR aModule)
126 WCHAR systemPath[MAX_PATH + 1] = { L'\0' };
128 // If GetSystemPath fails we accept that we'll load the DLLs from the
129 // normal search path.
130 GetSystemDirectoryW(systemPath, MAX_PATH + 1);
131 size_t systemDirLen = wcslen(systemPath);
133 // Make the system directory path terminate with a slash
134 if (systemDirLen && systemPath[systemDirLen - 1] != L'\\') {
135 systemPath[systemDirLen] = L'\\';
136 ++systemDirLen;
137 // No need to re-nullptr terminate
140 size_t fileLen = wcslen(aModule);
141 wcsncpy(systemPath + systemDirLen, aModule,
142 MAX_PATH - systemDirLen);
143 if (systemDirLen + fileLen <= MAX_PATH) {
144 systemPath[systemDirLen + fileLen] = L'\0';
145 } else {
146 systemPath[MAX_PATH] = L'\0';
148 return LoadLibraryW(systemPath);
153 #endif