计算存储器地址c#

计算存储器地址c#,c#,memory,offset,base,readprocessmemory,C#,Memory,Offset,Base,Readprocessmemory,如何使用静态地址和偏移量在C#中找到新的内存地址 基准:0x1023469C 偏移量:1E8 我尝试将偏移量添加到readprocessmemory函数内的基址,但根本不起作用:( 我正在尝试从这个地址读取内存,因为我正在编程一个小工具,如果我在justcause 2中的健康状况变差,它将播放声音。 提前谢谢你的帮助:D 到目前为止,我得到的是: using System; using System.Collections.Generic; using System.ComponentModel

如何使用静态地址和偏移量在C#中找到新的内存地址

基准:0x1023469C

偏移量:1E8

我尝试将偏移量添加到
readprocessmemory
函数内的基址,但根本不起作用:( 我正在尝试从这个地址读取内存,因为我正在编程一个小工具,如果我在justcause 2中的健康状况变差,它将播放声音。 提前谢谢你的帮助:D

到目前为止,我得到的是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
    //variabeln JC2
    //Pointer
    const int Offset = 0x1E8; // offset
    const int Base = 0x1023469C; // base
    const string Game = "The Game you don't know"; //Name

   

    //permission to read process memory
    const int PROCESS_WM_READ = 0x0010; //needed for reading memory


    [DllImport("kernel32.dll")]
    public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool ReadProcessMemory(
    IntPtr hProcess,
    IntPtr lpBaseAddress,
    [Out] byte[] lpBuffer,
    int dwSize,
    out int lpNumberOfBytesRead);


    public Form1()
    {
        InitializeComponent();
    }

    private void BTcheck_Click(object sender, EventArgs e)
    {
        if (Process.GetProcessesByName(Game).Length > 0)
        {
            Process process = Process.GetProcessesByName(Game)[0];
            IntPtr procHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);

            IntPtr baseAddress = new IntPtr(Base); //whatever address you wish
            int offset = Offset; //whatever offset you wish
            baseAddress += offset;
            byte[] buffer = new byte[sizeof(int)]; //select a proper buffer size
            int read = -1;

            ReadProcessMemory(procHandle, baseAddress, buffer, buffer.Length, out read); 

                            if (read == buffer.Length)
            {
                int value = BitConverter.ToInt32(buffer, 0);
                //do something with it
                
                LBcurrent.Text = Convert.ToString(value); //display the value
            }
        }

        else
        { LBcurrent.Text = "Error!"; }
    }
}
}
以下是您的操作方法(测试):

对于函数导入:

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
[Out] byte[] lpBuffer,
int dwSize,
out int lpNumberOfBytesRead);
使用它:

IntPtr procHandle = Process.GetCurrentProcess().Handle;
IntPtr baseAddress = new IntPtr(0x027EF131); //whatever address you wish
int offset = 0x100; //whatever offset you wish
baseAddress += offset;
byte[] buffer = new byte[sizeof(int)];
int read = -1;

ReadProcessMemory(procHandle, baseAddress, buffer, buffer.Length, out read);

if (read == buffer.Length)
{
    int value = BitConverter.ToInt32(buffer, 0);
    //do something with it
}
编辑: 我假设您正在尝试从当前进程内存中读取,因此是
prochHandle=process.GetCurrentProcess().Handle;
部分。请随意将该句柄更改为您需要的任何进程句柄,并具有访问该句柄的权限

编辑:
我已编辑了读取32位整数值的答案。对于64位,请使用sizeof(long)作为缓冲区大小和位转换器。ToInt64。

您想做什么?确切地说,什么“根本不起作用”?你能详细说明一下吗?你让我们猜测一下,这里…我试图读取动态内存地址的值。在这里,我需要使用指针(静态地址和偏移量)计算地址。我尝试了:ReadProcessMemory((int)processHandle,address,buffer,buffer.Length,ref bytesRead);现在我不知道如何使用指针获取我的地址…请看,更好:)现在将其编辑到您的问题中,我们可以继续回答部分:)嗨,谢谢您的帮助。我上传了我的源代码,因为我不知道我做错了什么。我只是在尝试读取内存时才会看到奇怪的中文符号。你确定那里是Unicode文本吗?试试ASCII码。另外,你确定它是文本吗?4字节只是两个Unicode字符。。。或4个ASCII字符。可能是一个32位整数。我更新了我的代码,但仍然不起作用。。。我得到System.Byte[](不是[],而是某种类似[]的矩形)。可能是因为我试图从一个64位的进程中读取,所以它不起作用吗?在ReadProcessMemory调用之后,read的值是多少?