33#include < tlhelp32.h>
44#include < vector>
55#include < algorithm>
6+ #include < functional>
67
78#include " NativeCore.hpp"
89
@@ -41,24 +42,25 @@ static DWORD GetRemotePeb(HANDLE process, PPEB* ppeb)
4142 return ERROR_SUCCESS;
4243}
4344
44- template <typename Proc>
45- static DWORD EnumerateRemoteModulesNative (HANDLE process, Proc proc)
45+ using InternalEnumerateRemoteModulesCallback = std::function<void (EnumerateRemoteModuleData&)>;
46+
47+ static bool EnumerateRemoteModulesNative (HANDLE process, const InternalEnumerateRemoteModulesCallback& callback)
4648{
4749 PPEB ppeb;
4850 const auto error = GetRemotePeb (process, &ppeb);
4951 if (error != ERROR_SUCCESS)
50- return error ;
52+ return false ;
5153
5254 PPEB_LDR_DATA ldr;
5355 auto success = ReadRemoteMemory (process, &ppeb->Ldr , &ldr, 0 , sizeof (ldr));
5456 if (!success)
55- return ERROR_READ_FAULT; // we seem to swallow the error anyways, might aswell give a distinctive one back
57+ return false ;
5658
5759 const auto list_head = &ldr->InMemoryOrderModuleList ; // remote address
5860 PLIST_ENTRY list_current; // remote address
5961 success = ReadRemoteMemory (process, &list_head->Flink , &list_current, 0 , sizeof (list_current));
6062 if (!success)
61- return ERROR_READ_FAULT ;
63+ return false ;
6264
6365 while (list_current != list_head)
6466 {
@@ -67,33 +69,34 @@ static DWORD EnumerateRemoteModulesNative(HANDLE process, Proc proc)
6769 LDR_DATA_TABLE_ENTRY mod;
6870 success = ReadRemoteMemory (process, CONTAINING_RECORD (list_current, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks), &mod, 0 , sizeof (mod));
6971 if (!success)
70- return ERROR_SUCCESS; // return success here to prevent running the other one
72+ break ;
7173
7274 EnumerateRemoteModuleData data = {};
7375 data.BaseAddress = mod.DllBase ;
7476 data.Size = *(ULONG*)&mod.Reserved2 [1 ]; // instead of undocced member could read ImageSize from headers
7577 const auto path_len = std::min (sizeof (RC_UnicodeChar) * (PATH_MAXIMUM_LENGTH - 1 ), size_t (mod.FullDllName .Length ));
7678 success = ReadRemoteMemory (process, mod.FullDllName .Buffer , data.Path , 0 , int (path_len));
7779 if (!success)
78- return ERROR_SUCCESS; // return success here to prevent running the other one
80+ break ;
7981
8082 // UNICODE_STRING is not guaranteed to be null terminated
8183 data.Path [path_len / 2 ] = 0 ;
8284
83- proc (& data);
85+ callback ( data);
8486
8587 list_current = mod.InMemoryOrderLinks .Flink ;
8688 }
8789
88- return ERROR_SUCCESS ;
90+ return true ;
8991}
9092
91- template <typename Proc>
92- static DWORD EnumerateRemoteModulesWinapi (HANDLE process, Proc proc)
93+ bool EnumerateRemoteModulesWinapi (HANDLE process, const InternalEnumerateRemoteModulesCallback& callback)
9394{
9495 const auto handle = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, GetProcessId (process));
9596 if (handle == INVALID_HANDLE_VALUE)
96- return GetLastError ();
97+ {
98+ return false ;
99+ }
97100
98101 MODULEENTRY32W me32 = {};
99102 me32.dwSize = sizeof (MODULEENTRY32W);
@@ -106,13 +109,13 @@ static DWORD EnumerateRemoteModulesWinapi(HANDLE process, Proc proc)
106109 data.Size = me32.modBaseSize ;
107110 std::memcpy (data.Path , me32.szExePath , std::min (MAX_PATH, PATH_MAXIMUM_LENGTH));
108111
109- proc (& data);
112+ callback ( data);
110113 } while (Module32NextW (handle, &me32));
111114 }
112115
113116 CloseHandle (handle);
114117
115- return ERROR_SUCCESS ;
118+ return true ;
116119}
117120
118121void RC_CallConv EnumerateRemoteSectionsAndModules (RC_Pointer process, EnumerateRemoteSectionsCallback callbackSection, EnumerateRemoteModulesCallback callbackModule)
@@ -165,42 +168,42 @@ void RC_CallConv EnumerateRemoteSectionsAndModules(RC_Pointer process, Enumerate
165168 address = reinterpret_cast <size_t >(memInfo.BaseAddress ) + memInfo.RegionSize ;
166169 }
167170
168- const auto moduleEnumerator = [&](EnumerateRemoteModuleData* data)
171+ const auto moduleEnumerator = [&](EnumerateRemoteModuleData& data)
169172 {
170173 if (callbackModule != nullptr )
171174 {
172- callbackModule (data);
175+ callbackModule (& data);
173176 }
174177
175178 if (callbackSection != nullptr )
176179 {
177- auto it = std::lower_bound (std::begin (sections), std::end (sections), static_cast <LPVOID>(data-> BaseAddress ), [§ions](const auto & lhs, const LPVOID& rhs)
180+ auto it = std::lower_bound (std::begin (sections), std::end (sections), static_cast <LPVOID>(data. BaseAddress ), [§ions](const auto & lhs, const LPVOID& rhs)
178181 {
179182 return lhs.BaseAddress < rhs;
180183 });
181184
182185 IMAGE_DOS_HEADER imageDosHeader = {};
183186 IMAGE_NT_HEADERS imageNtHeaders = {};
184187
185- if (!ReadRemoteMemory (process, data-> BaseAddress , &imageDosHeader, 0 , sizeof (IMAGE_DOS_HEADER))
186- || !ReadRemoteMemory (process, PUCHAR (data-> BaseAddress ) + imageDosHeader.e_lfanew , &imageNtHeaders, 0 , sizeof (IMAGE_NT_HEADERS)))
188+ if (!ReadRemoteMemory (process, data. BaseAddress , &imageDosHeader, 0 , sizeof (IMAGE_DOS_HEADER))
189+ || !ReadRemoteMemory (process, PUCHAR (data. BaseAddress ) + imageDosHeader.e_lfanew , &imageNtHeaders, 0 , sizeof (IMAGE_NT_HEADERS)))
187190 {
188191 return ;
189192 }
190193
191194 std::vector<IMAGE_SECTION_HEADER> sectionHeaders (imageNtHeaders.FileHeader .NumberOfSections );
192- ReadRemoteMemory (process, PUCHAR (data-> BaseAddress ) + imageDosHeader.e_lfanew + sizeof (IMAGE_NT_HEADERS), sectionHeaders.data (), 0 , imageNtHeaders.FileHeader .NumberOfSections * sizeof (IMAGE_SECTION_HEADER));
195+ ReadRemoteMemory (process, PUCHAR (data. BaseAddress ) + imageDosHeader.e_lfanew + sizeof (IMAGE_NT_HEADERS), sectionHeaders.data (), 0 , imageNtHeaders.FileHeader .NumberOfSections * sizeof (IMAGE_SECTION_HEADER));
193196 for (auto && sectionHeader : sectionHeaders)
194197 {
195- const auto sectionAddress = reinterpret_cast <size_t >(data-> BaseAddress ) + sectionHeader.VirtualAddress ;
198+ const auto sectionAddress = reinterpret_cast <size_t >(data. BaseAddress ) + sectionHeader.VirtualAddress ;
196199
197200 for (; it != std::end (sections); ++it)
198201 {
199202 auto && section = *it;
200203
201204 if (sectionAddress >= reinterpret_cast <size_t >(section.BaseAddress )
202205 && sectionAddress < reinterpret_cast <size_t >(section.BaseAddress ) + static_cast <size_t >(section.Size )
203- && sectionHeader.VirtualAddress + sectionHeader.Misc .VirtualSize <= data-> Size )
206+ && sectionHeader.VirtualAddress + sectionHeader.Misc .VirtualSize <= data. Size )
204207 {
205208 if ((sectionHeader.Characteristics & IMAGE_SCN_CNT_CODE) == IMAGE_SCN_CNT_CODE)
206209 {
@@ -222,7 +225,7 @@ void RC_CallConv EnumerateRemoteSectionsAndModules(RC_Pointer process, Enumerate
222225 {
223226 std::memset (section.Name , 0 , sizeof (section.Name ));
224227 }
225- std::memcpy (section.ModulePath , data-> Path , std::min (MAX_PATH, PATH_MAXIMUM_LENGTH));
228+ std::memcpy (section.ModulePath , data. Path , std::min (MAX_PATH, PATH_MAXIMUM_LENGTH));
226229
227230 break ;
228231 }
@@ -231,7 +234,7 @@ void RC_CallConv EnumerateRemoteSectionsAndModules(RC_Pointer process, Enumerate
231234 }
232235 };
233236
234- if (EnumerateRemoteModulesNative (process, moduleEnumerator) != ERROR_SUCCESS )
237+ if (! EnumerateRemoteModulesNative (process, moduleEnumerator))
235238 {
236239 EnumerateRemoteModulesWinapi (process, moduleEnumerator);
237240 }
0 commit comments