
Originally Posted by
Jason
You may need to add the modules base address. i.e
DWORD address = (DWORD)GetModuleHandle("modulename.extension") + 0x2000490C;
But I dunno as I can't see anything relevant.
Problem solved.
And if I had followed Jason's instructions step by step, I would have solved it much earlier.
I was messing around with the code to see where the problem could be. Turns out that the problem was the way I was handling the address.
I was getting the address like this:
Code:
DWORD* Address = (DWORD*) GetModuleHandle(TEXT("gamex86.dll")) + 0x490C; // this is basically summing up 0x20000000 with 0x490C, which is 0x2000490C -- the address I confirmed many times.
This is the way that worked, for my surprise:
Code:
DWORD Address = (DWORD) GetModuleHandle(TEXT("gamex86.dll")) + 0x490C; // note that now Address is no longer a pointer. When I changed according to Jason's suggestion, it worked properly.
Looking at some logs that I created myself, I saw that when I was passing the argument as (DWORD*)"0x2000490C", the final address had a random byte in front of it, which led to the wrong address all the time. I imagined it was related to the conversion between char constant and DWORD*.
Wrong ways that I tried (with double quotes):
Code:
NowSum((DWORD*)"0x2000490C");
NowSum((DWORD*)"2000490C");
Right way (no quotes):
Code:
NowSum((DWORD*)0x2000490C);
If I had tried the above version in the first day, I wouldn't have had trouble.
Anyway, thank you all for the help.
EDIT: I just noticed that Insane also suggested removing the quotes in the first reply. So. I'm blind I guess.
EDIT 2: I would like to give you the final code so that everybody can learn something with my mistake. That's the whole point of the forum.
Code:
#include "stdafx.h"
#include <string.h>
#include <fstream>
using namespace std;
void NowSum(DWORD* Address) {
DWORD OldProt;
char* CharPointer = (char*) Address;
VirtualProtect(Address, 1, PAGE_EXECUTE_READWRITE, &OldProt);
*CharPointer = '\x03';
VirtualProtect(Address, 1, OldProt, NULL);
// log to understand wtf was going on
ofstream myfile;
myfile.open ("log.txt");
myfile << "CharPointer: " << CharPointer << "\n";
myfile << "*CharPointer: " << *CharPointer << "\n";
myfile << "Address: " << Address << "\n";
myfile << "(char*) Address: " << (char*) Address << "\n";
myfile << "*(char*) Address: " << *(char*) Address << "\n";
myfile.close();
}
void MainThread() {
DWORD Address = (DWORD) GetModuleHandle(TEXT("gamex86.dll")) + 0x490C;
NowSum((DWORD*)Address); // I could use NowSum((DWORD*)0x2000490C), but as a template for future codes, I would use GetModuleHandle() anyway;
return;
}
BOOL APIENTRY DllMain( HANDLE hModule, DWORD fdwReason, LPVOID lpReserved ) { // main() function inside the dll
if( fdwReason == DLL_PROCESS_ATTACH){
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&MainThread, NULL, NULL, NULL);
return TRUE;
}
return TRUE;
}