diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /mozglue/misc/WindowsEnumProcessModules.h | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'mozglue/misc/WindowsEnumProcessModules.h')
-rw-r--r-- | mozglue/misc/WindowsEnumProcessModules.h | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/mozglue/misc/WindowsEnumProcessModules.h b/mozglue/misc/WindowsEnumProcessModules.h new file mode 100644 index 0000000000..573b0dbdfa --- /dev/null +++ b/mozglue/misc/WindowsEnumProcessModules.h @@ -0,0 +1,61 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/. */ + +#ifndef mozilla_WindowsEnumProcessModules_h +#define mozilla_WindowsEnumProcessModules_h + +#include <windows.h> +#include <psapi.h> + +#include "mozilla/FunctionRef.h" +#include "mozilla/NativeNt.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/WinHeaderOnlyUtils.h" + +namespace mozilla { + +// Why don't we use CreateToolhelp32Snapshot instead of EnumProcessModules? +// CreateToolhelp32Snapshot gets the ANSI versions of module path strings +// via ntdll!RtlQueryProcessDebugInformation and stores them into a snapshot. +// Module32FirstW/Module32NextW re-converts ANSI into Unicode, but it cannot +// restore lost information. This means we still need GetModuleFileNameEx +// even when we use CreateToolhelp32Snapshot, but EnumProcessModules is faster. +inline bool EnumerateProcessModules( + const FunctionRef<void(const wchar_t*, HMODULE)>& aCallback) { + DWORD modulesSize; + if (!::EnumProcessModules(nt::kCurrentProcess, nullptr, 0, &modulesSize)) { + return false; + } + + DWORD modulesNum = modulesSize / sizeof(HMODULE); + UniquePtr<HMODULE[]> modules = MakeUnique<HMODULE[]>(modulesNum); + if (!::EnumProcessModules(nt::kCurrentProcess, modules.get(), + modulesNum * sizeof(HMODULE), &modulesSize)) { + return false; + } + + // The list may have shrunk between calls + if (modulesSize / sizeof(HMODULE) < modulesNum) { + modulesNum = modulesSize / sizeof(HMODULE); + } + + for (DWORD i = 0; i < modulesNum; ++i) { + UniquePtr<wchar_t[]> modulePath = GetFullModulePath(modules[i]); + if (!modulePath) { + continue; + } + + // Please note that modules[i] could be invalid if the module + // was unloaded after GetFullModulePath succeeded. + aCallback(modulePath.get(), modules[i]); + } + + return true; +} + +} // namespace mozilla + +#endif // mozilla_WindowsEnumProcessModules_h |