This is a book a friend recommended to me, and so far it has helped me a lot with my coding methods and styles.
This book is focused around teaching you to write clean, small code that can be easily understood by any programmer who reads it. If you're going to skim this book, don't bother. You'll only disagree with half of what you read and misunderstand everything else. However, if you actually want to better your programming and take the time to read it, you will probably learn a lot.
I'm currently starting on Chapter 9, been reading 1 a day.
Using teachings from this book, I've turned this (admittedly old, messy, pasted, and rushed) code
[code=c++]#include "StdAfx.h"
#include "Hook.h"
Hook::Hook(DWORD HookAddress, DWORD NewFunction)
{
HookAtAddress = HookAddress;
OldFuncAddress = HookAddress;
NewFuncAddress = NewFunction;
}
void Hook::Enable(){
DWORD dwOldProtect, dwNewProtect, dwNewCall;
//CALL opcode = 0xE8 <4 byte for distance>
BYTE callByte[5] = {0xE8, 0x00, 0x00, 0x00, 0x00};
//Calculate the distance
dwNewCall = NewFuncAddress - HookAtAddress - 5;
memcpy(&callByte[1], &dwNewCall, 4);
VirtualProtectEx(GetCurrentProcess(), (LPVOID)(HookAtAddress), 5, PAGE_READWRITE, &dwOldProtect); //Gain access to read/write
memcpy(&OldFuncAddress, (LPVOID)(HookAtAddress+1), 4); //Get the old function address for unhooking
memcpy((LPVOID)(HookAtAddress), &callByte, 5); //Hook the function
VirtualProtectEx(GetCurrentProcess(), (LPVOID)(HookAtAddress), 5, dwOldProtect, &dwNewProtect); //Restore access
}
void Hook:isable(){
DWORD dwOldProtect, dwNewProtect;
BYTE callByte[5] = {0xE8, 0x00, 0x00, 0x00, 0x00};
memcpy(&callByte[1], &OldFuncAddress, 4);
VirtualProtectEx(GetCurrentProcess(), (LPVOID)(HookAtAddress), 5, PAGE_READWRITE, &dwOldProtect);
memcpy((LPVOID)(HookAtAddress), &callByte, 5);
VirtualProtectEx(GetCurrentProcess(), (LPVOID)(HookAtAddress), 5, dwOldProtect, &dwNewProtect);
}
Hook::~Hook(void)
{
}[/code]
Into this:
[code=c++]#include "Hook.h"
#include "memoryHelper.h"
Hook::Hook(DWORD hookAddress, DWORD newFunction)
{
this->hookAtAddress = hookAddress + 1; //The first byte is the 0xE8 for the call, we want to write the 4 after it
this->newFuncAddress = newFunction - this->hookAtAddress - 5;
memoryHelper::allowOPCodeModification(this->hookAtAddress, 4);
this->oldFuncAddress = memoryHelper::getDWORD(this->hookAtAddress);
memoryHelper::disallowOPCodeModification(this->hookAtAddress, 4);
}
void Hook::enable()
{
this->setCall(this->newFuncAddress);
}
void Hook::disable()
{
this->setCall(this->oldFuncAddress);
}
void Hook::setCall(DWORD calladdress)
{
memoryHelper::allowOPCodeModification(this->hookAtAddress, 4);
memoryHelper::setDWORD(this->hookAtAddress, calladdress);
memoryHelper::disallowOPCodeModification(this->hookAtAddress, 4);
}[/code]
[code=c++]#include "memoryHelper.h"
DWORD memoryHelper::getDWORD(DWORD address)
{
return (DWORD)memoryHelper::getData(address, 4);
}
void* memoryHelper::getData(DWORD address, DWORD size)
{
void* copied;
memcpy(&copied, (LPVOID)(address), 4);
return copied;
}
void memoryHelper::setDWORD(DWORD address, DWORD value)
{
memoryHelper::setData(address, (LPVOID)value, 4);
}
void memoryHelper::setData(DWORD address, void* value, DWORD size)
{
memcpy((LPVOID)(address), value, size);
}
DWORD memoryHelper::allowOPCodeModification(DWORD address, DWORD size)
{
return memoryHelper::setRegionProtection(address, size, PAGE_READWRITE);
}
DWORD memoryHelper::disallowOPCodeModification(DWORD address, DWORD size)
{
return memoryHelper::setRegionProtection(address, size, PAGE_WRITECOPY);
}
DWORD memoryHelper::setRegionProtection(DWORD address, DWORD size, DWORD protection)
{
DWORD dwOldProtect;
VirtualProtect((LPVOID)(address), size, protection, &dwOldProtect);
return dwOldProtect;
}[/code]
Going to eventually apply what I've learned to all of my code. After seeing the changes and largely increased understandability in such a small piece of code, I think every programmer should live by this book.
DOWNLOAD
http://www.mediafire.com/?ney1ywi2yzz