C++ Memory Trainer (Copy and Paste Example)

10/16/2012 16:45 GinoGambino#1
This is simple C++ code to make an application that can manipulate memory in any program / game, works with all versions of windows.

Simply copy and paste into a new C++ main.c file, build your Form and your good to go.

Code:
#include <windows.h>
#include <tlhelp32.h>
#include "resource.h"



#pragma comment(linker,"/FILEALIGN:512 /MERGE:.rdata=.text /MERGE:.data=.text /SECTION:.text,EWR /IGNORE:4078")
BOOL GameRunning;

/** Start of Declarations here **/
BOOL GetProcessList( );

// Below is the about text that is shown when "About" button is clicked

char *about   =
"C++ Memory Changer /n"
"Vietcong Hud on/off"
"Thanks to Medic and Subsky";
/////////////////////////////////////////////////////////////////////

char *gameWindow = "vietcong.exe"; // exe name here
DWORD pid; HWND hwndWindow; DWORD bytes; HANDLE hand = NULL;



HANDLE pFile; //Used for logging address to file (not implimented in this build)

//below you will list the BOOLs for function toggles
BOOL Ismod-app1On,FirstTime1;
BOOL dlgReadSuccess = FALSE;


///////////////////////////////////////////////////////
////Global Variables
	
	
	Modapp on Code
	BYTE Hud[1] = {0x92}; 
	// add more below here


	Modapp off Code
	BYTE original_code[1] = {0x96};
	// Dont forget to add the original code to turn it off 
	
///////////////////////////////////////////////////////

	/** End of Declarations here **/

void aboutButton(HWND hwnd)
{
	MessageBox(hwnd,about,"About",MB_ICONINFORMATION);
}


	void Initialize(HWND hwnd,WPARAM wParam, LPARAM lParam) {
	GetProcessList();
	if(GameRunning==TRUE)
	{		 
         GetWindowThreadProcessId(hwndWindow, &pid);
		 hand = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
		 SetTimer(hwnd, 1, 200, NULL);	//Timer speed is 200ms, you can change it here
	} 
	else 
	{	//Error message for when game not found in process list
		MessageBox(NULL, "Vietcong not detected. Please run the game before running the trainer", "Error", MB_OK + MB_ICONWARNING);
	}




	FirstTime1=TRUE; //This is the true / false flag for "is this the first time the trainers read the game code

	Ismod-app1On=FALSE; 
	if(GameRunning==TRUE)
	{		 
         GetWindowThreadProcessId(hwndWindow, &pid);
		 hand = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
		 SetTimer(hwnd, 1, 200, NULL);	//Timer speed is 200ms, you can change it here
	} 
	else 
	{ //Error message for when game not found in process list
		MessageBox(NULL, "Vietcong not detected, please run the game before running the trainer", "Error", MB_OK + MB_ICONWARNING);
	}
}

void HookExe() //This function ensures we are attatched to the game at all times
{
	
	CloseHandle(hand);
    GetProcessList( );
    GetWindowThreadProcessId(hwndWindow, &pid);
	hand = OpenProcess(PROCESS_ALL_ACCESS,0,pid);

}

	/*----- Here comes the good stuff -----*/


void timerCall() //functions in here run according to timer above
{
	//char name = (); //this is our buffer to catch the current value
	//int bytes = 0; //used temporarily for Read/WriteProcessMemory functions
	//int prevAccessProtection = 0; //used temporarily for VirtualProtectEx function
		HookExe(); //Call to function above (game always attatched)


/////////////////////////////////////////////////////////////////////////
/////ReadProcMem arrays are used to read and store original code so we 
/////toggle the code on and off


	if(FirstTime1==TRUE) //checks to see if this is the first time its run, if it is continue
	{
		
		ReadProcessMemory(hand, (void*) 0xEB0F44 , &original_code, 1, &bytes); // reads the bytes at address 0xEB0F44 and stores them
	    FirstTime1=FALSE;
	}
	// What we are doing here is reading 3 bytes of the games code for VC Hud and storing them in a variable called "original_code"
	//	The number in sqaure brackets is the number of bytes, this has to match the number after our variable
	// "original_code" in the ReadProcessMemory line.

	// You can add more addresses in, just be sure to have unique varible names and specify the right number of bytes.


///////////////////////////////////////////////////////////////////////////
/////Start Hotkey Functions Below

		/* --Vietcong HUD on/off Example Function----------------------------------------- */



	if(GetAsyncKeyState(VK_NUMPAD1)) // User Pressed the NumPad1 to switch on HUD
	{			
		   
		if(Ismod-app1On==FALSE) //if this modapp is not on do this........
			{  
				WriteProcessMemory(hand, (void*)0xEB0F44, &Hud,1, &bytes);  //Change the memory to activate the Modapp
																			
																			
				Ismod-app1On=TRUE; //Sets our "Is On" flag to "on"
			}
			else // .... do this
			{
				
				WriteProcessMemory(hand, (void*)0xEB0F44, &original_code,1, &bytes); // Write the original code into memory

				Ismod-app1On=FALSE; //Sets our "Is On" flag to "off"
			}




			/// Copy and paste the above function and change the variables to add another modapp
	

		}


	

	//The function above will toggle between mod-app on and mod-app off status. For a list of virtual keys please visit:
	// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/WindowsUserInterface/UserInput/VirtualKeyCodes.asp
     		
	/* --Example Function --END------------------------------------ */



	/** End **/
}

// YOU DONT NEED TO EDIT BELOW THIS LINE 

BOOL GetProcessList( )
{
  HANDLE hProcessSnap;
  HANDLE hProcess;
  PROCESSENTRY32 pe32;
  DWORD dwPriorityClass;
  int PidTest;
  GameRunning=FALSE;
 
  
  // Take a snapshot of all processes in the system.
  hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  if( hProcessSnap == INVALID_HANDLE_VALUE ) return( FALSE );
  

  // Set the size of the structure before using it.
  pe32.dwSize = sizeof( PROCESSENTRY32 );

  // Retrieve information about the first process,
  // and exit if unsuccessful
  if( !Process32First( hProcessSnap, &pe32 ) )
  {
    CloseHandle( hProcessSnap );     // Must clean up the snapshot object!
    return( FALSE );
  }

  // Now walk the snapshot of processes, and
  // display information about each process in turn
  
  do
  {
    // Retrieve the priority class.
    dwPriorityClass = 0;
    hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
    if( hProcess != NULL )
    {
      dwPriorityClass = GetPriorityClass( hProcess );
      if( !dwPriorityClass )
        
      CloseHandle( hProcess );
    }

    PidTest=strcmp(gameWindow, pe32.szExeFile);
	if(PidTest==0){ pid=pe32.th32ProcessID; GameRunning=TRUE;}

  } while( Process32Next( hProcessSnap, &pe32 ) );

  // Don't forget to clean up the snapshot object!
  CloseHandle( hProcessSnap );
  return( TRUE );
}

BOOL CALLBACK DialogProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    switch (message)
	{
		case WM_INITDIALOG:
			Initialize(hwnd,wParam,lParam);
			return TRUE;

		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDC_ABOUT:
					aboutButton(hwnd);
					return TRUE;

				case IDC_EXIT:
					EndDialog (hwnd, 0);
					return TRUE;
			}
		return TRUE;

		case WM_DESTROY:
			CloseHandle(pFile);
			PostQuitMessage(0);
			return TRUE;

		case WM_CLOSE:
			PostQuitMessage(0);
			return TRUE;
		case WM_TIMER:
			timerCall();
			return TRUE;
    }
    return FALSE;
}


 
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{


	DialogBox(hInstance,MAKEINTRESOURCE(IDD_MAINDLG), NULL,DialogProc);
	return 0;
}
Enjoy..

Credits:
ht.t.p://tkc-community.n.e.t/forum/index.php?topic=10051.0
10/17/2012 09:36 MoepMeep#2
Worst code I saw this week, yay.
10/17/2012 10:11 GinoGambino#3
Its just an example.and not written by me.