33//
44
55#include " memory.hpp"
6+ #include < intrin.h>
67
78namespace base ::win32::memory {
8- StatusOr<std::uintptr_t > GetModuleBaseAddress (std::uint32_t pid, const std::string& mod_name) {
9+ StatusOr<std::uintptr_t > GetModuleBaseAddress (const std::uint32_t pid, const std::string& mod_name) {
910 std::uint64_t mod_base_addr = 0 ;
10- HANDLE h_snap = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
11+ const HANDLE h_snap = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
1112 if (h_snap == INVALID_HANDLE_VALUE) {
1213 return MakeFailure<ResultCode::kINVALID_HANDLE >();
1314 }
@@ -17,7 +18,7 @@ namespace base::win32::memory {
1718 if (Module32First (h_snap, &mod_entry)) {
1819 do {
1920 if (!_stricmp (mod_entry.szModule , mod_name.c_str ())) {
20- mod_base_addr = ( std::uint64_t ) mod_entry.modBaseAddr ;
21+ mod_base_addr = reinterpret_cast < std::uint64_t >( mod_entry.modBaseAddr ) ;
2122 break ;
2223 }
2324 } while (Module32Next (h_snap, &mod_entry));
@@ -28,17 +29,17 @@ namespace base::win32::memory {
2829 return mod_base_addr;
2930 }
3031
31- StatusOr<MODULEENTRY32> GetModuleFromAddress (std::uint32_t pid, std::uintptr_t addr) {
32+ StatusOr<MODULEENTRY32> GetModuleFromAddress (const std::uint32_t pid, const std::uintptr_t addr) {
3233 MODULEENTRY32 mod_entry;
33- HANDLE h_snap = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
34+ const HANDLE h_snap = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid);
3435 if (h_snap == INVALID_HANDLE_VALUE) {
3536 return MakeFailure<ResultCode::kINVALID_HANDLE >();
3637 }
3738
3839 mod_entry.dwSize = sizeof (mod_entry);
3940 if (Module32First (h_snap, &mod_entry)) {
4041 do {
41- if (( std::uint64_t ) mod_entry.modBaseAddr <= addr && addr <= ( std::uint64_t ) mod_entry.modBaseAddr + mod_entry.modBaseSize ) {
42+ if (reinterpret_cast < std::uint64_t >( mod_entry.modBaseAddr ) <= addr && addr <= reinterpret_cast < std::uint64_t >( mod_entry.modBaseAddr ) + mod_entry.modBaseSize ) {
4243 break ;
4344 }
4445 } while (Module32Next (h_snap, &mod_entry));
@@ -49,26 +50,26 @@ namespace base::win32::memory {
4950 return mod_entry;
5051 }
5152
52- StatusOr<std::string> GetModuleNameFromAddress (std::uint32_t pid, std::uintptr_t addr) {
53+ StatusOr<std::string> GetModuleNameFromAddress (const std::uint32_t pid, const std::uintptr_t addr) {
5354 auto mod_addr = GetModuleFromAddress (pid, addr);
5455 if (!mod_addr)
5556 return mod_addr.error ().Forward ();
5657
5758 return mod_addr->szModule ;
5859 }
5960
60- StatusOr<uintptr_t > GetModuleOffsetFromAddress (std::uint32_t pid, std::uintptr_t addr) {
61+ StatusOr<uintptr_t > GetModuleOffsetFromAddress (const std::uint32_t pid, const std::uintptr_t addr) {
6162 auto mod_addr = GetModuleFromAddress (pid, addr);
6263 if (!mod_addr)
6364 return mod_addr.error ();
6465
6566 return addr - reinterpret_cast <std::uintptr_t >(mod_addr->modBaseAddr );
6667 }
6768
68- StatusOr<MODULEENTRY32> GetModuleFromHModule (HMODULE mod) {
69+ StatusOr<MODULEENTRY32> GetModuleFromHModule (const HMODULE mod) {
6970 MODULEENTRY32 mod_entry{};
7071 mod_entry.dwSize = sizeof (MODULEENTRY32);
71- HANDLE hSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, GetCurrentProcessId ());
72+ const HANDLE hSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPMODULE, GetCurrentProcessId ());
7273 if (hSnapshot == INVALID_HANDLE_VALUE) {
7374 return MakeFailure<ResultCode::kINVALID_HANDLE >();
7475 }
@@ -83,4 +84,26 @@ namespace base::win32::memory {
8384 CloseHandle (hSnapshot);
8485 return mod_entry;
8586 }
87+
88+ __declspec (noinline) bool IsAddressInCurrentModule (const std::uintptr_t addr) {
89+ const std::uint32_t pid = GetCurrentProcessId ();
90+
91+ // Get the module name of the caller (our DLL) using the return address
92+ auto caller_address = reinterpret_cast <std::uintptr_t >(_ReturnAddress ());
93+ auto our_module_name = GetModuleNameFromAddress (pid, caller_address);
94+
95+ if (!our_module_name.has_value ()) {
96+ return false ;
97+ }
98+
99+ // Get the module name of the input address
100+ auto addr_module_name = GetModuleNameFromAddress (pid, addr);
101+
102+ if (!addr_module_name.has_value ()) {
103+ return false ;
104+ }
105+
106+ // Compare the module names
107+ return _stricmp (our_module_name->c_str (), addr_module_name->c_str ()) == 0 ;
108+ }
86109}
0 commit comments