| 2 | #include "Logger.hpp" |
| 3 | |
| 4 | bool 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 | } |
no test coverage detected