patterncppMinor
MemoryWriter Helper class
Viewed 0 times
helperclassmemorywriter
Problem
I have created this memory writer helper to ease the write into memory process and I want your suggestions regarding it. I am very new to C++ but have 6 years knowledge of .NET languages like VB and C#.
#include
class MemoryWriter {
private:
USHORT _currentIndex;
LPVOID _targetAddress;
template
static void editMemory(DWORD address, T data)
{
DWORD oldProtection;
VirtualProtect((LPVOID)address, sizeof(T), PAGE_EXECUTE_READWRITE, &oldProtection);
*((T*)address) = data;
VirtualProtect((LPVOID)address, sizeof(T), PAGE_EXECUTE_READWRITE, NULL);
}
public:
MemoryWriter(DWORD address) {
_currentIndex = 0;
_targetAddress = (LPVOID)address;
}
template
void write(T data)
{
editMemory(((DWORD)_targetAddress + _currentIndex), data);
_currentIndex += sizeof(T);
}
template
void write(LPVOID address, T data)
{
editMemory((DWORD)address, data);
_currentIndex = (DWORD)address;
}
void reset(LPVOID newAddress = NULL)
{
_currentIndex = 0;
if (newAddress)
_targetAddress = newAddress;
}
};Solution
A general physical layout guideline: Put all
I added white spaces around functions to make them more readable.
public members, functions as well as variables, first, followed by protected members, and private members at the end. You want users to see public members first, followed by protected members. Hopefully, they don't have to see private members at all.I added white spaces around functions to make them more readable.
class MemoryWriter {
public:
MemoryWriter(DWORD address) {
_currentIndex = 0;
_targetAddress = (LPVOID)address;
}
template
void write(T data)
{
editMemory(((DWORD)_targetAddress + _currentIndex), data);
_currentIndex += sizeof(T);
}
template
void write(LPVOID address, T data)
{
editMemory((DWORD)address, data);
_currentIndex = (DWORD)address;
}
void reset(LPVOID newAddress = NULL)
{
_currentIndex = 0;
if (newAddress)
_targetAddress = newAddress;
}
protected:
private:
USHORT _currentIndex;
LPVOID _targetAddress;
template
static void editMemory(DWORD address, T data)
{
DWORD oldProtection;
VirtualProtect((LPVOID)address, sizeof(T), PAGE_EXECUTE_READWRITE, &oldProtection);
*((T*)address) = data;
VirtualProtect((LPVOID)address, sizeof(T), PAGE_EXECUTE_READWRITE, NULL);
}
};Code Snippets
class MemoryWriter {
public:
MemoryWriter(DWORD address) {
_currentIndex = 0;
_targetAddress = (LPVOID)address;
}
template<typename T>
void write(T data)
{
editMemory<T>(((DWORD)_targetAddress + _currentIndex), data);
_currentIndex += sizeof(T);
}
template<typename T>
void write(LPVOID address, T data)
{
editMemory<T>((DWORD)address, data);
_currentIndex = (DWORD)address;
}
void reset(LPVOID newAddress = NULL)
{
_currentIndex = 0;
if (newAddress)
_targetAddress = newAddress;
}
protected:
private:
USHORT _currentIndex;
LPVOID _targetAddress;
template<typename T>
static void editMemory(DWORD address, T data)
{
DWORD oldProtection;
VirtualProtect((LPVOID)address, sizeof(T), PAGE_EXECUTE_READWRITE, &oldProtection);
*((T*)address) = data;
VirtualProtect((LPVOID)address, sizeof(T), PAGE_EXECUTE_READWRITE, NULL);
}
};Context
StackExchange Code Review Q#79840, answer score: 2
Revisions (0)
No revisions yet.