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 https://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_WinHeaderOnlyUtils_h
8 #define mozilla_WinHeaderOnlyUtils_h
20 #include "mozilla/Assertions.h"
21 #include "mozilla/Attributes.h"
22 #include "mozilla/DynamicallyLinkedFunctionPtr.h"
23 #include "mozilla/Maybe.h"
24 #include "mozilla/ResultVariant.h"
25 #include "mozilla/UniquePtr.h"
26 #include "nsWindowsHelpers.h"
28 #if defined(MOZILLA_INTERNAL_API)
30 # include "nsString.h"
31 #endif // defined(MOZILLA_INTERNAL_API)
34 * This header is intended for self-contained, header-only, utility code for
35 * Win32. It may be used outside of xul.dll, in places such as firefox.exe or
36 * mozglue.dll. If your code creates dependencies on Mozilla libraries, you
37 * should put it elsewhere.
40 #if !defined(STATUS_SUCCESS)
41 # define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
42 #endif // !defined(STATUS_SUCCESS)
44 // Our data indicates a few users of Win7 x86 hit failure to load urlmon.dll
45 // for unknown reasons. Since we don't always require urlmon.dll on Win7,
46 // we delay-load it, which causes a crash if loading urlmon.dll fails. This
47 // macro is to safely load and call urlmon's API graciously without crash.
49 # define SAFECALL_URLMON_FUNC(FuncName, ...) \
51 static const mozilla::StaticDynamicallyLinkedFunctionPtr< \
52 decltype(&::FuncName)> \
53 func(L"urlmon.dll", #FuncName); \
55 func ? func(__VA_ARGS__) : HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND); \
58 # define SAFECALL_URLMON_FUNC(FuncName, ...) hr = ::FuncName(__VA_ARGS__)
63 class WindowsError final
{
65 // HRESULT and NTSTATUS are both typedefs of LONG, so we cannot use
66 // overloading to properly differentiate between the two. Instead we'll use
67 // static functions to convert the various error types to HRESULTs before
69 explicit constexpr WindowsError(HRESULT aHResult
) : mHResult(aHResult
) {}
72 using UniqueString
= UniquePtr
<WCHAR
[], LocalFreeDeleter
>;
74 static constexpr WindowsError
FromNtStatus(NTSTATUS aNtStatus
) {
75 if (aNtStatus
== STATUS_SUCCESS
) {
76 // Special case: we don't want to set FACILITY_NT_BIT
77 // (HRESULT_FROM_NT does not handle this case, unlike HRESULT_FROM_WIN32)
78 return WindowsError(S_OK
);
81 return WindowsError(HRESULT_FROM_NT(aNtStatus
));
84 static constexpr WindowsError
FromHResult(HRESULT aHResult
) {
85 return WindowsError(aHResult
);
88 static constexpr WindowsError
FromWin32Error(DWORD aWin32Err
) {
89 return WindowsError(HRESULT_FROM_WIN32(aWin32Err
));
92 static WindowsError
FromLastError() {
93 return FromWin32Error(::GetLastError());
96 static WindowsError
CreateSuccess() { return WindowsError(S_OK
); }
98 static WindowsError
CreateGeneric() {
99 return FromWin32Error(ERROR_UNIDENTIFIED_ERROR
);
102 bool IsSuccess() const { return SUCCEEDED(mHResult
); }
104 bool IsFailure() const { return FAILED(mHResult
); }
106 bool IsAvailableAsWin32Error() const {
107 return IsAvailableAsNtStatus() ||
108 HRESULT_FACILITY(mHResult
) == FACILITY_WIN32
;
111 bool IsAvailableAsNtStatus() const {
112 return mHResult
== S_OK
|| (mHResult
& FACILITY_NT_BIT
);
115 bool IsAvailableAsHResult() const { return true; }
117 UniqueString
AsString() const {
118 LPWSTR rawMsgBuf
= nullptr;
119 constexpr DWORD flags
= FORMAT_MESSAGE_ALLOCATE_BUFFER
|
120 FORMAT_MESSAGE_FROM_SYSTEM
|
121 FORMAT_MESSAGE_IGNORE_INSERTS
;
123 ::FormatMessageW(flags
, nullptr, mHResult
, 0,
124 reinterpret_cast<LPWSTR
>(&rawMsgBuf
), 0, nullptr);
129 return UniqueString(rawMsgBuf
);
132 HRESULT
AsHResult() const { return mHResult
; }
134 // Not all HRESULTs are convertible to Win32 Errors, so we use Maybe
135 Maybe
<DWORD
> AsWin32Error() const {
136 if (mHResult
== S_OK
) {
137 return Some(static_cast<DWORD
>(ERROR_SUCCESS
));
140 if (HRESULT_FACILITY(mHResult
) == FACILITY_WIN32
) {
141 // This is the inverse of HRESULT_FROM_WIN32
142 return Some(static_cast<DWORD
>(HRESULT_CODE(mHResult
)));
145 // The NTSTATUS facility is a special case and thus does not utilize the
146 // HRESULT_FACILITY and HRESULT_CODE macros.
147 if (mHResult
& FACILITY_NT_BIT
) {
148 return Some(NtStatusToWin32Error(
149 static_cast<NTSTATUS
>(mHResult
& ~FACILITY_NT_BIT
)));
155 // Not all HRESULTs are convertible to NTSTATUS, so we use Maybe
156 Maybe
<NTSTATUS
> AsNtStatus() const {
157 if (mHResult
== S_OK
) {
158 return Some(STATUS_SUCCESS
);
161 // The NTSTATUS facility is a special case and thus does not utilize the
162 // HRESULT_FACILITY and HRESULT_CODE macros.
163 if (mHResult
& FACILITY_NT_BIT
) {
164 return Some(static_cast<NTSTATUS
>(mHResult
& ~FACILITY_NT_BIT
));
170 constexpr bool operator==(const WindowsError
& aOther
) const {
171 return mHResult
== aOther
.mHResult
;
174 constexpr bool operator!=(const WindowsError
& aOther
) const {
175 return mHResult
!= aOther
.mHResult
;
178 static DWORD
NtStatusToWin32Error(NTSTATUS aNtStatus
) {
179 static const StaticDynamicallyLinkedFunctionPtr
<
180 decltype(&RtlNtStatusToDosError
)>
181 pRtlNtStatusToDosError(L
"ntdll.dll", "RtlNtStatusToDosError");
183 MOZ_ASSERT(!!pRtlNtStatusToDosError
);
184 if (!pRtlNtStatusToDosError
) {
185 return ERROR_UNIDENTIFIED_ERROR
;
188 return pRtlNtStatusToDosError(aNtStatus
);
192 // We store the error code as an HRESULT because they can encode both Win32
193 // error codes and NTSTATUS codes.
199 struct UnusedZero
<WindowsError
> {
200 using StorageType
= WindowsError
;
202 static constexpr bool value
= true;
203 static constexpr StorageType nullValue
= WindowsError::FromHResult(S_OK
);
205 static constexpr void AssertValid(StorageType aValue
) {}
206 static constexpr const WindowsError
& Inspect(const StorageType
& aValue
) {
209 static constexpr WindowsError
Unwrap(StorageType aValue
) { return aValue
; }
210 static constexpr StorageType
Store(WindowsError aValue
) { return aValue
; }
212 } // namespace detail
214 enum DetourResultCode
: uint32_t {
216 INTERCEPTOR_MOD_NULL
,
217 INTERCEPTOR_MOD_INACCESSIBLE
,
218 INTERCEPTOR_PROC_NULL
,
219 INTERCEPTOR_PROC_INACCESSIBLE
,
220 DETOUR_PATCHER_RESERVE_FOR_MODULE_PE_ERROR
,
221 DETOUR_PATCHER_RESERVE_FOR_MODULE_TEXT_ERROR
,
222 DETOUR_PATCHER_RESERVE_FOR_MODULE_RESERVE_ERROR
,
223 DETOUR_PATCHER_DO_RESERVE_ERROR
,
224 DETOUR_PATCHER_NEXT_TRAMPOLINE_ERROR
,
225 DETOUR_PATCHER_INVALID_TRAMPOLINE
,
226 DETOUR_PATCHER_WRITE_POINTER_ERROR
,
227 DETOUR_PATCHER_CREATE_TRAMPOLINE_ERROR
,
228 FUNCHOOKCROSSPROCESS_COPYSTUB_ERROR
,
229 MMPOLICY_RESERVE_INVALIDARG
,
230 MMPOLICY_RESERVE_ZERO_RESERVATIONSIZE
,
231 MMPOLICY_RESERVE_CREATEFILEMAPPING
,
232 MMPOLICY_RESERVE_MAPVIEWOFFILE
,
233 MMPOLICY_RESERVE_NOBOUND_RESERVE_ERROR
,
234 MMPOLICY_RESERVE_FINDREGION_INVALIDLEN
,
235 MMPOLICY_RESERVE_FINDREGION_INVALIDRANGE
,
236 MMPOLICY_RESERVE_FINDREGION_VIRTUALQUERY_ERROR
,
237 MMPOLICY_RESERVE_FINDREGION_NO_FREE_REGION
,
238 MMPOLICY_RESERVE_FINAL_RESERVE_ERROR
,
241 #if defined(NIGHTLY_BUILD)
243 // We have a 16-bytes buffer, but only minimum bytes to detour per
244 // architecture are copied. See CreateTrampoline in PatcherDetour.h.
245 DetourResultCode mErrorCode
;
246 uint8_t mOrigBytes
[16];
247 explicit DetourError(DetourResultCode aError
)
248 : mErrorCode(aError
), mOrigBytes
{} {}
249 DetourError(DetourResultCode aError
, DWORD aWin32Error
)
250 : mErrorCode(aError
), mOrigBytes
{} {
251 static_assert(sizeof(mOrigBytes
) >= sizeof(aWin32Error
),
252 "Can't fit a DWORD in mOrigBytes");
253 *reinterpret_cast<DWORD
*>(mOrigBytes
) = aWin32Error
;
255 operator WindowsError() const {
256 return WindowsError::FromHResult(mErrorCode
);
259 #endif // defined(NIGHTLY_BUILD)
261 template <typename T
>
262 using WindowsErrorResult
= Result
<T
, WindowsError
>;
264 struct LauncherError
{
265 LauncherError(const char* aFile
, int aLine
, WindowsError aWin32Error
)
266 : mFile(aFile
), mLine(aLine
), mError(aWin32Error
) {}
268 #if defined(NIGHTLY_BUILD)
269 LauncherError(const char* aFile
, int aLine
,
270 const Maybe
<DetourError
>& aDetourError
)
273 mError(aDetourError
.isSome() ? aDetourError
.value()
274 : WindowsError::CreateGeneric()),
275 mDetourError(aDetourError
) {}
276 #endif // defined(NIGHTLY_BUILD)
281 #if defined(NIGHTLY_BUILD)
282 Maybe
<DetourError
> mDetourError
;
283 #endif // defined(NIGHTLY_BUILD)
285 bool operator==(const LauncherError
& aOther
) const {
286 return mError
== aOther
.mError
;
289 bool operator!=(const LauncherError
& aOther
) const {
290 return mError
!= aOther
.mError
;
293 bool operator==(const WindowsError
& aOther
) const { return mError
== aOther
; }
295 bool operator!=(const WindowsError
& aOther
) const { return mError
!= aOther
; }
298 #if defined(MOZ_USE_LAUNCHER_ERROR)
300 template <typename T
>
301 using LauncherResult
= Result
<T
, LauncherError
>;
303 template <typename T
>
304 using LauncherResultWithLineInfo
= LauncherResult
<T
>;
306 using WindowsErrorType
= LauncherError
;
310 template <typename T
>
311 using LauncherResult
= WindowsErrorResult
<T
>;
313 template <typename T
>
314 using LauncherResultWithLineInfo
= Result
<T
, LauncherError
>;
316 using WindowsErrorType
= WindowsError
;
318 #endif // defined(MOZ_USE_LAUNCHER_ERROR)
320 using LauncherVoidResult
= LauncherResult
<Ok
>;
322 using LauncherVoidResultWithLineInfo
= LauncherResultWithLineInfo
<Ok
>;
324 #if defined(MOZ_USE_LAUNCHER_ERROR)
326 # define LAUNCHER_ERROR_GENERIC() \
327 ::mozilla::Err(::mozilla::LauncherError( \
328 __FILE__, __LINE__, ::mozilla::WindowsError::CreateGeneric()))
330 # if defined(NIGHTLY_BUILD)
331 # define LAUNCHER_ERROR_FROM_DETOUR_ERROR(err) \
332 ::mozilla::Err(::mozilla::LauncherError(__FILE__, __LINE__, err))
334 # define LAUNCHER_ERROR_FROM_DETOUR_ERROR(err) LAUNCHER_ERROR_GENERIC()
335 # endif // defined(NIGHTLY_BUILD)
337 # define LAUNCHER_ERROR_FROM_WIN32(err) \
338 ::mozilla::Err(::mozilla::LauncherError( \
339 __FILE__, __LINE__, ::mozilla::WindowsError::FromWin32Error(err)))
341 # define LAUNCHER_ERROR_FROM_LAST() \
342 ::mozilla::Err(::mozilla::LauncherError( \
343 __FILE__, __LINE__, ::mozilla::WindowsError::FromLastError()))
345 # define LAUNCHER_ERROR_FROM_NTSTATUS(ntstatus) \
346 ::mozilla::Err(::mozilla::LauncherError( \
347 __FILE__, __LINE__, ::mozilla::WindowsError::FromNtStatus(ntstatus)))
349 # define LAUNCHER_ERROR_FROM_HRESULT(hresult) \
350 ::mozilla::Err(::mozilla::LauncherError( \
351 __FILE__, __LINE__, ::mozilla::WindowsError::FromHResult(hresult)))
353 // This macro wraps the supplied WindowsError with a LauncherError
354 # define LAUNCHER_ERROR_FROM_MOZ_WINDOWS_ERROR(err) \
355 ::mozilla::Err(::mozilla::LauncherError(__FILE__, __LINE__, err))
359 # define LAUNCHER_ERROR_GENERIC() \
360 ::mozilla::Err(::mozilla::WindowsError::CreateGeneric())
362 # define LAUNCHER_ERROR_FROM_DETOUR_ERROR(err) LAUNCHER_ERROR_GENERIC()
364 # define LAUNCHER_ERROR_FROM_WIN32(err) \
365 ::mozilla::Err(::mozilla::WindowsError::FromWin32Error(err))
367 # define LAUNCHER_ERROR_FROM_LAST() \
368 ::mozilla::Err(::mozilla::WindowsError::FromLastError())
370 # define LAUNCHER_ERROR_FROM_NTSTATUS(ntstatus) \
371 ::mozilla::Err(::mozilla::WindowsError::FromNtStatus(ntstatus))
373 # define LAUNCHER_ERROR_FROM_HRESULT(hresult) \
374 ::mozilla::Err(::mozilla::WindowsError::FromHResult(hresult))
376 # define LAUNCHER_ERROR_FROM_MOZ_WINDOWS_ERROR(err) ::mozilla::Err(err)
378 #endif // defined(MOZ_USE_LAUNCHER_ERROR)
380 // How long to wait for a created process to become available for input,
381 // to prevent that process's windows being forced to the background.
382 // This is used across update, restart, and the launcher.
383 const DWORD kWaitForInputIdleTimeoutMS
= 10 * 1000;
386 * Wait for a child GUI process to become "idle." Idle means that the process
387 * has created its message queue and has begun waiting for user input.
389 * Note that this must only be used when the child process is going to display
390 * GUI! Otherwise you're going to be waiting for a very long time ;-)
392 * @return true if we successfully waited for input idle;
393 * false if we timed out or failed to wait.
395 inline bool WaitForInputIdle(HANDLE aProcess
,
396 DWORD aTimeoutMs
= kWaitForInputIdleTimeoutMS
) {
397 const DWORD kSleepTimeMs
= 10;
398 const DWORD waitStart
= aTimeoutMs
== INFINITE
? 0 : ::GetTickCount();
402 if (aTimeoutMs
!= INFINITE
) {
403 elapsed
= ::GetTickCount() - waitStart
;
406 if (elapsed
>= aTimeoutMs
) {
410 // ::WaitForInputIdle() doesn't always set the last-error code on failure
411 ::SetLastError(ERROR_SUCCESS
);
413 DWORD waitResult
= ::WaitForInputIdle(aProcess
, aTimeoutMs
- elapsed
);
418 if (waitResult
== WAIT_FAILED
&&
419 ::GetLastError() == ERROR_NOT_GUI_PROCESS
) {
420 ::Sleep(kSleepTimeMs
);
428 enum class PathType
{
433 class FileUniqueId final
{
435 explicit FileUniqueId(const wchar_t* aPath
, PathType aPathType
)
436 : mId(FILE_ID_INFO()) {
438 mId
= LAUNCHER_ERROR_FROM_HRESULT(E_INVALIDARG
);
446 mId
= LAUNCHER_ERROR_FROM_HRESULT(E_INVALIDARG
);
447 MOZ_ASSERT_UNREACHABLE("Unhandled PathType");
450 case PathType::eNtPath
: {
451 UNICODE_STRING unicodeString
;
452 ::RtlInitUnicodeString(&unicodeString
, aPath
);
453 OBJECT_ATTRIBUTES objectAttributes
;
454 InitializeObjectAttributes(&objectAttributes
, &unicodeString
,
455 OBJ_CASE_INSENSITIVE
, nullptr, nullptr);
456 IO_STATUS_BLOCK ioStatus
= {};
458 NTSTATUS status
= ::NtOpenFile(
459 &ntHandle
, SYNCHRONIZE
| FILE_READ_ATTRIBUTES
, &objectAttributes
,
460 &ioStatus
, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
461 FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_FOR_BACKUP_INTENT
);
462 // We don't need to check |ntHandle| for INVALID_HANDLE_VALUE here,
463 // as that value is set by the Win32 layer.
464 if (!NT_SUCCESS(status
)) {
465 mId
= LAUNCHER_ERROR_FROM_NTSTATUS(status
);
473 case PathType::eDosPath
: {
474 file
.own(::CreateFileW(
475 aPath
, 0, FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
,
476 nullptr, OPEN_EXISTING
, FILE_FLAG_BACKUP_SEMANTICS
, nullptr));
477 if (file
== INVALID_HANDLE_VALUE
) {
478 mId
= LAUNCHER_ERROR_FROM_LAST();
489 explicit FileUniqueId(const nsAutoHandle
& aFile
) : mId(FILE_ID_INFO()) {
493 ~FileUniqueId() = default;
495 bool IsError() const { return mId
.isErr(); }
497 const WindowsErrorType
& GetError() const { return mId
.inspectErr(); }
499 FileUniqueId(FileUniqueId
&& aOther
) = default;
500 FileUniqueId
& operator=(FileUniqueId
&& aOther
) = delete;
502 bool operator==(const FileUniqueId
& aOther
) const {
503 return mId
.isOk() && aOther
.mId
.isOk() &&
504 !memcmp(&mId
.inspect(), &aOther
.mId
.inspect(), sizeof(FILE_ID_INFO
));
507 bool operator!=(const FileUniqueId
& aOther
) const {
508 return !((*this) == aOther
);
512 void GetId(const nsAutoHandle
& aFile
) {
513 FILE_ID_INFO fileIdInfo
= {};
514 if (::GetFileInformationByHandleEx(aFile
.get(), FileIdInfo
, &fileIdInfo
,
515 sizeof(fileIdInfo
))) {
519 // Only NTFS and ReFS support FileIdInfo. So we have to fallback if
520 // GetFileInformationByHandleEx failed.
522 BY_HANDLE_FILE_INFORMATION info
= {};
523 if (!::GetFileInformationByHandle(aFile
.get(), &info
)) {
524 mId
= LAUNCHER_ERROR_FROM_LAST();
528 fileIdInfo
.VolumeSerialNumber
= info
.dwVolumeSerialNumber
;
529 memcpy(&fileIdInfo
.FileId
.Identifier
[0], &info
.nFileIndexLow
,
531 memcpy(&fileIdInfo
.FileId
.Identifier
[sizeof(DWORD
)], &info
.nFileIndexHigh
,
537 LauncherResult
<FILE_ID_INFO
> mId
;
540 class MOZ_RAII AutoVirtualProtect final
{
542 AutoVirtualProtect(void* aAddress
, size_t aLength
, DWORD aProtFlags
,
543 HANDLE aTargetProcess
= ::GetCurrentProcess())
544 : mAddress(aAddress
),
546 mTargetProcess(aTargetProcess
),
548 mError(WindowsError::CreateSuccess()) {
549 if (!::VirtualProtectEx(aTargetProcess
, aAddress
, aLength
, aProtFlags
,
551 mError
= WindowsError::FromLastError();
555 ~AutoVirtualProtect() {
556 if (mError
.IsFailure()) {
560 ::VirtualProtectEx(mTargetProcess
, mAddress
, mLength
, mPrevProt
,
564 explicit operator bool() const { return mError
.IsSuccess(); }
566 WindowsError
GetError() const { return mError
; }
568 DWORD
PrevProt() const { return mPrevProt
; }
570 AutoVirtualProtect(const AutoVirtualProtect
&) = delete;
571 AutoVirtualProtect(AutoVirtualProtect
&&) = delete;
572 AutoVirtualProtect
& operator=(const AutoVirtualProtect
&) = delete;
573 AutoVirtualProtect
& operator=(AutoVirtualProtect
&&) = delete;
578 HANDLE mTargetProcess
;
583 inline UniquePtr
<wchar_t[]> GetFullModulePath(HMODULE aModule
) {
584 DWORD bufLen
= MAX_PATH
;
585 mozilla::UniquePtr
<wchar_t[]> buf
;
589 buf
= mozilla::MakeUnique
<wchar_t[]>(bufLen
);
590 retLen
= ::GetModuleFileNameW(aModule
, buf
.get(), bufLen
);
595 if (retLen
== bufLen
&& ::GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
603 // Upon success, retLen *excludes* the null character
606 // Since we're likely to have a bunch of unused space in buf, let's
607 // reallocate a string to the actual size of the file name.
608 auto result
= mozilla::MakeUnique
<wchar_t[]>(retLen
);
609 if (wcscpy_s(result
.get(), retLen
, buf
.get())) {
616 inline UniquePtr
<wchar_t[]> GetFullBinaryPath() {
617 return GetFullModulePath(nullptr);
620 // Generates the install directory without a trailing path separator.
621 inline bool GetInstallDirectory(UniquePtr
<wchar_t[]>& installPath
) {
622 installPath
= GetFullBinaryPath();
623 // It's not safe to use PathRemoveFileSpecW with strings longer than MAX_PATH
624 // (including null terminator).
625 if (wcslen(installPath
.get()) >= MAX_PATH
) {
628 ::PathRemoveFileSpecW(installPath
.get());
632 class ModuleVersion final
{
634 constexpr ModuleVersion() : mVersion(0ULL) {}
636 explicit ModuleVersion(const VS_FIXEDFILEINFO
& aFixedInfo
)
637 : mVersion((static_cast<uint64_t>(aFixedInfo
.dwFileVersionMS
) << 32) |
638 static_cast<uint64_t>(aFixedInfo
.dwFileVersionLS
)) {}
640 explicit ModuleVersion(const uint64_t aVersion
) : mVersion(aVersion
) {}
642 ModuleVersion(const ModuleVersion
& aOther
) : mVersion(aOther
.mVersion
) {}
644 uint64_t AsInteger() const { return mVersion
; }
646 operator uint64_t() const { return AsInteger(); }
648 std::tuple
<uint16_t, uint16_t, uint16_t, uint16_t> AsTuple() const {
649 uint16_t major
= static_cast<uint16_t>((mVersion
>> 48) & 0xFFFFU
);
650 uint16_t minor
= static_cast<uint16_t>((mVersion
>> 32) & 0xFFFFU
);
651 uint16_t patch
= static_cast<uint16_t>((mVersion
>> 16) & 0xFFFFU
);
652 uint16_t build
= static_cast<uint16_t>(mVersion
& 0xFFFFU
);
654 return {major
, minor
, patch
, build
};
657 explicit operator bool() const { return !!mVersion
; }
659 bool operator<(const ModuleVersion
& aOther
) const {
660 return mVersion
< aOther
.mVersion
;
663 bool operator<(const uint64_t& aOther
) const { return mVersion
< aOther
; }
665 ModuleVersion
& operator=(const uint64_t aIntVersion
) {
666 mVersion
= aIntVersion
;
674 inline LauncherResult
<ModuleVersion
> GetModuleVersion(
675 const wchar_t* aModuleFullPath
) {
676 DWORD verInfoLen
= ::GetFileVersionInfoSizeW(aModuleFullPath
, nullptr);
678 return LAUNCHER_ERROR_FROM_LAST();
681 auto verInfoBuf
= MakeUnique
<BYTE
[]>(verInfoLen
);
682 if (!::GetFileVersionInfoW(aModuleFullPath
, 0, verInfoLen
,
684 return LAUNCHER_ERROR_FROM_LAST();
688 VS_FIXEDFILEINFO
* fixedInfo
= nullptr;
689 if (!::VerQueryValueW(verInfoBuf
.get(), L
"\\",
690 reinterpret_cast<LPVOID
*>(&fixedInfo
), &fixedInfoLen
)) {
691 // VerQueryValue may fail if the resource does not exist. This is not an
692 // error; we'll return 0 in this case.
693 return ModuleVersion(0ULL);
696 return ModuleVersion(*fixedInfo
);
699 inline LauncherResult
<ModuleVersion
> GetModuleVersion(HMODULE aModule
) {
700 UniquePtr
<wchar_t[]> fullPath(GetFullModulePath(aModule
));
702 return LAUNCHER_ERROR_GENERIC();
705 return GetModuleVersion(fullPath
.get());
708 #if defined(MOZILLA_INTERNAL_API)
709 inline LauncherResult
<ModuleVersion
> GetModuleVersion(nsIFile
* aFile
) {
711 return LAUNCHER_ERROR_FROM_HRESULT(E_INVALIDARG
);
714 nsAutoString fullPath
;
715 nsresult rv
= aFile
->GetPath(fullPath
);
717 return LAUNCHER_ERROR_GENERIC();
720 return GetModuleVersion(fullPath
.get());
722 #endif // defined(MOZILLA_INTERNAL_API)
724 struct CoTaskMemFreeDeleter
{
725 void operator()(void* aPtr
) { ::CoTaskMemFree(aPtr
); }
728 inline LauncherResult
<TOKEN_ELEVATION_TYPE
> GetElevationType(
729 const nsAutoHandle
& aToken
) {
731 TOKEN_ELEVATION_TYPE elevationType
;
732 if (!::GetTokenInformation(aToken
.get(), TokenElevationType
, &elevationType
,
733 sizeof(elevationType
), &retLen
)) {
734 return LAUNCHER_ERROR_FROM_LAST();
737 return elevationType
;
740 inline bool HasPackageIdentity() {
741 HMODULE kernel32Dll
= ::GetModuleHandleW(L
"kernel32");
746 typedef LONG(WINAPI
* GetCurrentPackageIdProc
)(UINT32
*, BYTE
*);
747 GetCurrentPackageIdProc pGetCurrentPackageId
=
748 (GetCurrentPackageIdProc
)::GetProcAddress(kernel32Dll
,
749 "GetCurrentPackageId");
751 // If there was any package identity to retrieve, we get
752 // ERROR_INSUFFICIENT_BUFFER. If there had been no package identity it
753 // would instead return APPMODEL_ERROR_NO_PACKAGE.
754 UINT32 packageNameSize
= 0;
755 return pGetCurrentPackageId
&&
756 (pGetCurrentPackageId(&packageNameSize
, nullptr) ==
757 ERROR_INSUFFICIENT_BUFFER
);
760 inline UniquePtr
<wchar_t[]> GetPackageFamilyName() {
761 HMODULE kernel32Dll
= ::GetModuleHandleW(L
"kernel32");
766 typedef LONG(WINAPI
* GetCurrentPackageFamilyNameProc
)(UINT32
*, PWSTR
);
767 GetCurrentPackageFamilyNameProc pGetCurrentPackageFamilyName
=
768 (GetCurrentPackageFamilyNameProc
)::GetProcAddress(
769 kernel32Dll
, "GetCurrentPackageFamilyName");
770 if (!pGetCurrentPackageFamilyName
) {
774 UINT32 packageNameSize
= 0;
775 if (pGetCurrentPackageFamilyName(&packageNameSize
, nullptr) !=
776 ERROR_INSUFFICIENT_BUFFER
) {
780 UniquePtr
<wchar_t[]> packageIdentity
= MakeUnique
<wchar_t[]>(packageNameSize
);
781 if (pGetCurrentPackageFamilyName(&packageNameSize
, packageIdentity
.get()) !=
786 return packageIdentity
;
789 // This implementation is equivalent to PathGetDriveNumber[AW].
790 // We define our own version because using PathGetDriveNumber
791 // delay-loads shlwapi.dll, which may fail when the process is
793 template <typename T
>
794 int MozPathGetDriveNumber(const T
* aPath
) {
795 const auto ToDriveNumber
= [](const T
* aPath
) -> int {
796 if (*aPath
== '\0' || *(aPath
+ 1) != ':') {
801 return (c
>= 'A' && c
<= 'Z') ? c
- 'A'
802 : (c
>= 'a' && c
<= 'z') ? c
- 'a'
810 if (*aPath
== '\\' && *(aPath
+ 1) == '\\' && *(aPath
+ 2) == '?' &&
811 *(aPath
+ 3) == '\\') {
812 return ToDriveNumber(aPath
+ 4);
815 return ToDriveNumber(aPath
);
818 } // namespace mozilla
820 #endif // mozilla_WinHeaderOnlyUtils_h