@@ -44,53 +44,55 @@ static DWORD GetRemotePeb(HANDLE process, PPEB* ppeb)
4444
4545using InternalEnumerateRemoteModulesCallback = std::function<void (EnumerateRemoteModuleData&)>;
4646
47- static bool EnumerateRemoteModulesNative (HANDLE process, const InternalEnumerateRemoteModulesCallback& callback)
47+ bool EnumerateRemoteModulesNative (const RC_Pointer process, const InternalEnumerateRemoteModulesCallback& callback)
4848{
4949 PPEB ppeb;
50- const auto error = GetRemotePeb (process, &ppeb);
51- if (error != ERROR_SUCCESS)
50+ if ( GetRemotePeb (process, &ppeb) != ERROR_SUCCESS)
51+ {
5252 return false ;
53+ }
5354
5455 PPEB_LDR_DATA ldr;
55- auto success = ReadRemoteMemory (process, &ppeb->Ldr , &ldr, 0 , sizeof (ldr));
56- if (!success)
56+ if (! ReadRemoteMemory (process, &ppeb->Ldr , &ldr, 0 , sizeof (PPEB_LDR_DATA)))
57+ {
5758 return false ;
59+ }
5860
59- const auto list_head = &ldr->InMemoryOrderModuleList ; // remote address
60- PLIST_ENTRY list_current; // remote address
61- success = ReadRemoteMemory (process, &list_head ->Flink , &list_current , 0 , sizeof (list_current));
62- if (!success)
61+ const auto head = &ldr->InMemoryOrderModuleList ;
62+ PLIST_ENTRY current;
63+ if (! ReadRemoteMemory (process, &head ->Flink , ¤t , 0 , sizeof (PLIST_ENTRY)))
64+ {
6365 return false ;
66+ }
6467
65- while (list_current != list_head )
68+ while (current != head )
6669 {
67- // TODO: error handling - what do we do if module list changed? We can't un-call the callback
68-
69- LDR_DATA_TABLE_ENTRY mod;
70- success = ReadRemoteMemory (process, CONTAINING_RECORD (list_current, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks), &mod, 0 , sizeof (mod));
71- if (!success)
70+ LDR_DATA_TABLE_ENTRY entry;
71+ if (!ReadRemoteMemory (process, CONTAINING_RECORD (current, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks), &entry, 0 , sizeof (entry)))
72+ {
7273 break ;
74+ }
7375
7476 EnumerateRemoteModuleData data = {};
75- data.BaseAddress = mod.DllBase ;
76- data.Size = *(ULONG*)&mod.Reserved2 [1 ]; // instead of undocced member could read ImageSize from headers
77- const auto path_len = std::min (sizeof (RC_UnicodeChar) * (PATH_MAXIMUM_LENGTH - 1 ), size_t (mod.FullDllName .Length ));
78- success = ReadRemoteMemory (process, mod.FullDllName .Buffer , data.Path , 0 , int (path_len));
79- if (!success)
77+ data.BaseAddress = entry.DllBase ;
78+ data.Size = *reinterpret_cast <ULONG*>(&entry.Reserved2 [1 ]); // instead of undocced member could read ImageSize from headers
79+
80+ const auto length = std::min<int >(sizeof (RC_UnicodeChar) * (PATH_MAXIMUM_LENGTH - 1 ), entry.FullDllName .Length );
81+ if (!ReadRemoteMemory (process, entry.FullDllName .Buffer , data.Path , 0 , length))
82+ {
8083 break ;
81-
82- // UNICODE_STRING is not guaranteed to be null terminated
83- data.Path [path_len / 2 ] = 0 ;
84+ }
85+ data.Path [length / 2 ] = 0 ;
8486
8587 callback (data);
8688
87- list_current = mod .InMemoryOrderLinks .Flink ;
89+ current = entry .InMemoryOrderLinks .Flink ;
8890 }
8991
9092 return true ;
9193}
9294
93- bool EnumerateRemoteModulesWinapi (HANDLE process, const InternalEnumerateRemoteModulesCallback& callback)
95+ bool EnumerateRemoteModulesWinapi (const RC_Pointer process, const InternalEnumerateRemoteModulesCallback& callback)
9496{
9597 const auto handle = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, GetProcessId (process));
9698 if (handle == INVALID_HANDLE_VALUE)
0 commit comments