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
11 #include "nsAutoRef.h"
13 // Critical Section helper class
15 class AutoCriticalSection
18 AutoCriticalSection(LPCRITICAL_SECTION aSection
)
21 ::EnterCriticalSection(mSection
);
23 ~AutoCriticalSection()
25 ::LeaveCriticalSection(mSection
);
28 LPCRITICAL_SECTION mSection
;
32 class nsAutoRefTraits
<HKEY
>
41 static void Release(RawRef aFD
)
50 class nsAutoRefTraits
<SC_HANDLE
>
53 typedef SC_HANDLE RawRef
;
54 static SC_HANDLE
Void()
59 static void Release(RawRef aFD
)
62 CloseServiceHandle(aFD
);
68 class nsSimpleRef
<HANDLE
>
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
;
92 static void Release(RawRef aRawRef
)
94 if (aRawRef
&& aRawRef
!= INVALID_HANDLE_VALUE
) {
103 class nsAutoRefTraits
<HMODULE
>
106 typedef HMODULE RawRef
;
112 static void Release(RawRef aFD
)
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
'\\';
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';
146 systemPath
[MAX_PATH
] = L
'\0';
148 return LoadLibraryW(systemPath
);