C # Action Address read write data from memory (beginner)

Source: Internet
Author: User

This example uses Plants vs. Zombies as an example to allow the sun to refresh to 9999 per 1 seconds. The game version used for this example is [Plant vs Zombies 2010 edition], and the tool used to view the memory address is CE.

Because every time the game is started, the Sun address in the game is changed, the only unchanged base 1, we have to use the CE tool to find the address of base 1, you can calculate the address of the sun.

Address of base 2 = value in base 1 + offset 1;

The address of the Sun = value in base 2 + offset 2;

The following is a simple example: A window interface with a button and a timer

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;usingSystem.Diagnostics;namespacezhiwudazhanjiangshi{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        #regionApi//reads the byte set data from the specified memory[DllImportAttribute ("Kernel32.dll", EntryPoint ="readprocessmemory")]         Public Static extern BOOLReadProcessMemory (IntPtr hprocess,intptr lpbaseaddress,intptr lpbuffer,intnsize,intptr Lpnumberofbytesread); //writes the byte set data from the specified memory[DllImportAttribute ("Kernel32.dll", EntryPoint ="WriteProcessMemory")]         Public Static extern BOOLWriteProcessMemory (IntPtr hprocess,intptr lpbaseaddress,int[] lpbuffer,intnSize, IntPtr lpnumberofbyteswritten); //opens a Process object that already exists and returns a handle to the process[DllImportAttribute ("Kernel32.dll", EntryPoint ="openprocess")]         Public Static externIntPtr OpenProcess (intdwDesiredAccess,BOOLbInheritHandle,intdwprocessid); //closes a kernel object. These include files, file mappings, processes, threads, security, and synchronization objects. [DllImport ("Kernel32.dll")]        Private Static extern voidCloseHandle (IntPtr hobject); #endregion        #regionHow to use//get PID based on process name         Public Static intGetpidbyprocessname (stringprocessName) {process[] arrayprocess=Process.getprocessesbyname (processName); foreach(Process Pincharrayprocess) {                returnp.id; }            return 0; }        //read in-memory values         Public Static intReadmemoryvalue (intBaseAddress,stringprocessName) {            Try            {                byte[] buffer =New byte[4]; //Get buffer AddressIntPtr byteaddress = marshal.unsafeaddrofpinnedarrayelement (buffer,0); //open a Process object that already exists 0x1f0fff highest privilegeIntPtr hprocess = OpenProcess (0x1f0fff,false, Getpidbyprocessname (processName)); //reads in-memory values into buffersReadProcessMemory (hprocess, (INTPTR) baseaddress, byteaddress,4, IntPtr.Zero); //Close OperationCloseHandle (hprocess); //reads a 32-bit signed integer from unmanaged memory.                 returnMarshal.readint32 (byteaddress); }            Catch            {                return 0; }        }        //writes a value to the specified memory address         Public Static voidWritememoryvalue (intBaseAddress,stringProcessName,intvalue) {            Try            {                //open a Process object that already exists 0x1f0fff highest privilegeIntPtr hprocess = OpenProcess (0x1f0fff,false, Getpidbyprocessname (processName)); //writes the byte set data from the specified memoryWriteProcessMemory (hprocess, (INTPTR) baseaddress,New int[] {value},4, IntPtr.Zero); //Close OperationCloseHandle (hprocess); }            Catch { }        }        #endregion        //Game Memory Base        Private intBaseAddress =0x0015e944; //Game Process Name        Private stringProcessName ="plantsvszombies"; //toggle button on/off function        Private voidButton1_Click (Objectsender, EventArgs e) {            if(Getpidbyprocessname (processName) = =0) {MessageBox.Show ("The game is not running!"); return; }            if(button1. Text = ="Open") {button1. Text="Close"; Timer1. Enabled=true; }            Else{button1. Text="Open"; Timer1. Enabled=false; }        }        //Timer        Private voidTimer1_Tick (Objectsender, EventArgs e) {            if(Getpidbyprocessname (processName) = =0) {timer1. Enabled=false; }            //baseaddress: Game Memory Base processName: Game Process name//read the value stored in base 1            intAddress =Readmemoryvalue (baseaddress, processName); //calculate address of base 2 = value in base 1 + offset 1Address = address +0x868; //read the value stored in base 2Address =Readmemoryvalue (address, processName); //address of the calculated Sun = value in base 2 + offset 2Address = address +0x5578; //write values to the Sun address, 0x378:888Writememoryvalue (address, ProcessName,0x378); }     }}
View Code

C # Action Address read write data from memory (beginner)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.