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 mozilla_WindowsEnumProcessModules_h
8 #define mozilla_WindowsEnumProcessModules_h
13 #include "mozilla/FunctionRef.h"
14 #include "mozilla/NativeNt.h"
15 #include "mozilla/UniquePtr.h"
16 #include "mozilla/WinHeaderOnlyUtils.h"
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
) {
29 if (!::EnumProcessModules(nt::kCurrentProcess
, nullptr, 0, &modulesSize
)) {
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
)) {
40 // The list may have shrunk between calls
41 if (modulesSize
/ sizeof(HMODULE
) < modulesNum
) {
42 modulesNum
= modulesSize
/ sizeof(HMODULE
);
45 for (DWORD i
= 0; i
< modulesNum
; ++i
) {
46 UniquePtr
<wchar_t[]> modulePath
= GetFullModulePath(modules
[i
]);
51 // Please note that modules[i] could be invalid if the module
52 // was unloaded after GetFullModulePath succeeded.
53 aCallback(modulePath
.get(), modules
[i
]);
59 } // namespace mozilla
61 #endif // mozilla_WindowsEnumProcessModules_h