MCPcopy Create free account
hub / github.com/Atarity/Lightpack / WriteToProtectedMem

Function WriteToProtectedMem

Software/hooks/hooksutils.cpp:4–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2#include "Logger.hpp"
3
4bool WriteToProtectedMem(void * mem, void * newVal, void * savedVal, size_t size) {
5 DWORD protection = PAGE_READWRITE;
6 DWORD oldProtection;
7 Logger *logger = Logger::getInstance();
8
9 // align protected/unprotected memory by 8 bytes
10 size_t protectedSize = ((size + 7)/8)*8;
11
12 if (VirtualProtect(mem, protectedSize, protection, &oldProtection)) {
13 if (savedVal) {
14 logger->reportLogDebug(L"Writing to memory. Saving old content. dest 0x%x, src 0x%x, size %u", savedVal, mem, size);
15 memcpy(savedVal, mem, size);
16 logger->reportLogDebug(L"Writing to memory. Saving old content. finished");
17 }
18 logger->reportLogDebug(L"Writing to memory. dest 0x%x, src 0x%x, size %u", mem, newVal, size);
19 memcpy(mem, newVal, size);
20 logger->reportLogDebug(L"Writing to memory. finished");
21
22 if (VirtualProtect(mem, protectedSize, oldProtection, &oldProtection))
23 return true;
24 }
25 return false;
26}

Callers 4

installHookMethod · 0.85
removeHookMethod · 0.85
installHookMethod · 0.85
removeHookMethod · 0.85

Calls 1

reportLogDebugMethod · 0.80

Tested by

no test coverage detected