介绍
C++实现在其他进程内存分配,并且能够指定范围
Windows IDE:Visual Studio 2022
Windows
引用库
- <iostream>
- <string>
- <Windows.h>
- <vector>
分配内存
uintptr_t SimpleRemoteMemory(HANDLE process, SIZE_T size, uintptr_t address = NULL) // 分配内存 { LPVOID remoteMemory = VirtualAllocEx(process, (LPVOID)address, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); uintptr_t result; result = reinterpret_cast<uintptr_t>(remoteMemory); return result; }
释放内存
BOOL SimpleFreeMemory(HANDLE process, uintptr_t address) { // 释放分配内存 BOOL result = VirtualFreeEx(process, (LPVOID)address, 0, MEM_RELEASE); return result; }
在某个内存地址附近分配内存
uintptr_t FindAllowRemoteMemory(HANDLE process, uintptr_t address, SIZE_T size, SIZE_T range) { // 查找某个地址附近的可分配内存 bool continueFind = true; uintptr_t yetAddress = address; uintptr_t result = 0; if (address > range) { address = address - range; } else { address = 0; } std::vector<uint8_t> findData = ReadMemoryByteArray(process, address, size); while (continueFind) { if (findData.size() == 0) { if (SimpleRemoteMemory(process, size, address) != NULL) { continueFind = false; result = address; } } address = address + size; findData = ReadMemoryByteArray(process, address, size); if ((yetAddress + range - size) <= address) { continueFind = false; } } return result; }