×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How To Get The Base Address with c++?

How To Get The Base Address with c++?

How To Get The Base Address with c++?

(OP)
hello i want to get the base address of my application, so that i can write to it's memory, here is my code below:

CODE --> C++

DWORD dwGetModuleBaseAddress(TCHAR *lpszModuleName, DWORD ProcessID) {
	DWORD dwModuleBaseAddress = 0;
	MODULEENTRY32 ModuleEntry32 = {0};
	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessID);
	ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
	if ( Module32First(hSnapshot, &ModuleEntry32) )
	{
		do
		{
		  if ( _tcscmp(ModuleEntry32.szModule, lpszModuleName) == 0 ) {
			 dwModuleBaseAddress = (DWORD)ModuleEntry32.modBaseAddr;
             break;
		  }
		} while( Module32Next(hSnapshot, &ModuleEntry32) );
	}
	CloseHandle(hSnapshot);
    return dwModuleBaseAddress;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
	DWORD ProcessID;
	DWORD off1, off2, off3;
	DWORD BaseAddr;
	// Get Handles
	HWND HalfLife2 = FindWindowA(0, "AssaultCube");
	GetWindowThreadProcessId(HalfLife2, &ProcessID);
	HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessID);
	// Get Client Base Address
	DWORD ClientBaseAddress = dwGetModuleBaseAddress(L"ac_client.exe", ProcessID);
	ReadProcessMemory( hProcess, (LPCVOID)(ClientBaseAddress + 0x00109B74), &BaseAddr, sizeof(BaseAddr), 0 );
	CheatReturnStatus1->Caption = BaseAddr;
} 

The Problem is that every time i launch my application i see different address (always change) not pointing to the desired address that i found in cheat engine(which is static address), but i already got a base pointer to that address in cheat engine,
can you help me please?


RE: How To Get The Base Address with c++?

I'm not sure what the "cheat engine" is showing but when I load a program into memory, the location is rarely same. If always depends on what else is loaded and when.

James P. Cottingham
I'm number 1,229!
I'm number 1,229!

RE: How To Get The Base Address with c++?

(OP)
Thank you, but i already found the solution, the problem is i get the base address of my application module, but when i go ahead to display in label i forgot to convert the DWORD value to HEX.
Now i get the desired results.
Still, how to write to process memory with offsets, because i already have that code to do that, and it's not working somehow:

CODE --> C++

bool GetMoreAmmo(int AmmoAmount) {
	DWORD ProcessID;
	DWORD off1, off2, off3;
	DWORD BaseAddr;
	DWORD AmmoAddr;
        int CurrentAmmo;
	int NewAmmo = AmmoAmount;
	// Get Handles
	HWND HalfLife2 = FindWindowA(0, "HALF-LIFE 2");
	GetWindowThreadProcessId(HalfLife2, &ProcessID);
	HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessID);
	// Get Client Base Address
	DWORD ClientBaseAddress = dwGetModuleBaseAddress(L"client.dll", ProcessID);
	ReadProcessMemory( hProcess, (LPCVOID)(ClientBaseAddress + 0x0010A280), &BaseAddr, sizeof(BaseAddr), 0 );
	ReadProcessMemory(hProcess, (LPCVOID)(BaseAddr + 0x7B0), &off1, sizeof(off1), 0);
	ReadProcessMemory(hProcess, (LPCVOID)(off1 + 0x34), &off2, sizeof(off2), 0);
	ReadProcessMemory(hProcess, (LPCVOID)(off2 + 0xA4), &off3, sizeof(off3), 0);
	AmmoAddr = off3 + 0x4E0;
	ReadProcessMemory(hProcess, (LPCVOID)(AmmoAddr), &CurrentAmmo, sizeof(CurrentAmmo), 0);
	if ( WriteProcessMemory( hProcess, (LPVOID)(CurrentAmmo), &NewAmmo, sizeof(NewAmmo), 0 ) != 0 )
	{
		return true;
	} else
	{
                return false;
	}
} 

However, i don't get what i expects, any idea?

RE: How To Get The Base Address with c++?

The Operating system and your anti-virus should not allow you to write to your program's code segments after it is loaded into memory. Any attempt to do so will result in either a AV fault and program crash or the anti-virus quarantining your program. This a very specific viral behavior that is blocked, and for good reason.

RE: How To Get The Base Address with c++?

(OP)
OK, now i understand, thank you :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close