From a8da72d503e923add2d40a2ad7af117dbfc575de Mon Sep 17 00:00:00 2001 From: Dmitry Nagibin <19asdek91@gmail.com> Date: Mon, 11 Jan 2021 20:27:05 +0300 Subject: [PATCH] Added getting code base address API function to manage handles on other side --- MemoryModule.c | 9 +++++++-- MemoryModule.h | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/MemoryModule.c b/MemoryModule.c index 8df241d..1581693 100644 --- a/MemoryModule.c +++ b/MemoryModule.c @@ -603,7 +603,6 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size, PIMAGE_NT_HEADERS old_header; unsigned char *code, *headers; ptrdiff_t locationDelta; - SYSTEM_INFO sysInfo; PIMAGE_SECTION_HEADER section; DWORD i; size_t optionalSectionSize; @@ -732,7 +731,7 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *data, size_t size, result->getProcAddress = getProcAddress; result->freeLibrary = freeLibrary; result->userdata = userdata; - result->pageSize = sysInfo.dwPageSize; + result->pageSize = old_header->OptionalHeader.SectionAlignment; #ifdef _WIN64 result->blockedMemory = blockedMemory; #endif @@ -810,6 +809,12 @@ error: return NULL; } +LPVOID MemoryGetCodeAddress(HMEMORYMODULE mod) +{ + PMEMORYMODULE module = (PMEMORYMODULE)mod; + return module ? (LPVOID)module->codeBase : NULL; +} + static int _compare(const void *a, const void *b) { const struct ExportNameEntry *p1 = (const struct ExportNameEntry*) a; diff --git a/MemoryModule.h b/MemoryModule.h index a728f6b..28f8e11 100644 --- a/MemoryModule.h +++ b/MemoryModule.h @@ -73,6 +73,12 @@ HMEMORYMODULE MemoryLoadLibraryEx(const void *, size_t, */ FARPROC MemoryGetProcAddress(HMEMORYMODULE, LPCSTR); +/** + * Get the code base address of loading module to store it above. + * On load dynamic library it used as a handle of library instance. + */ +LPVOID MemoryGetCodeAddress(HMEMORYMODULE); + /** * Free previously loaded EXE/DLL. */