WindowsEnumProcessModules.h (2150B)
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/. */ 6 7 #ifndef mozilla_WindowsEnumProcessModules_h 8 #define mozilla_WindowsEnumProcessModules_h 9 10 #include <windows.h> 11 #include <psapi.h> 12 13 #include "mozilla/FunctionRef.h" 14 #include "mozilla/NativeNt.h" 15 #include "mozilla/UniquePtr.h" 16 #include "mozilla/WinHeaderOnlyUtils.h" 17 18 namespace mozilla { 19 20 // Why don't we use CreateToolhelp32Snapshot instead of EnumProcessModules? 21 // CreateToolhelp32Snapshot gets the ANSI versions of module path strings 22 // via ntdll!RtlQueryProcessDebugInformation and stores them into a snapshot. 23 // Module32FirstW/Module32NextW re-converts ANSI into Unicode, but it cannot 24 // restore lost information. This means we still need GetModuleFileNameEx 25 // even when we use CreateToolhelp32Snapshot, but EnumProcessModules is faster. 26 inline bool EnumerateProcessModules( 27 const FunctionRef<void(const wchar_t*, HMODULE)>& aCallback) { 28 DWORD modulesSize; 29 if (!::EnumProcessModules(nt::kCurrentProcess, nullptr, 0, &modulesSize)) { 30 return false; 31 } 32 33 DWORD modulesNum = modulesSize / sizeof(HMODULE); 34 UniquePtr<HMODULE[]> modules = MakeUnique<HMODULE[]>(modulesNum); 35 if (!::EnumProcessModules(nt::kCurrentProcess, modules.get(), 36 modulesNum * sizeof(HMODULE), &modulesSize)) { 37 return false; 38 } 39 40 // The list may have shrunk between calls 41 if (modulesSize / sizeof(HMODULE) < modulesNum) { 42 modulesNum = modulesSize / sizeof(HMODULE); 43 } 44 45 for (DWORD i = 0; i < modulesNum; ++i) { 46 UniquePtr<wchar_t[]> modulePath = GetFullModulePath(modules[i]); 47 if (!modulePath) { 48 continue; 49 } 50 51 // Please note that modules[i] could be invalid if the module 52 // was unloaded after GetFullModulePath succeeded. 53 aCallback(modulePath.get(), modules[i]); 54 } 55 56 return true; 57 } 58 59 } // namespace mozilla 60 61 #endif // mozilla_WindowsEnumProcessModules_h