从外部地址读取浮点值

时间:2015-10-17 19:10:48

标签: c# readprocessmemory

我试图从外部地址获取浮动值。 为此,我使用此代码:

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

namespace Reader
{
    class Scanner
    {
        [DllImport("Kernel32.dll",SetLeastError=true)]
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, UInt32 nSize, ref UInt32 lpNumberOfBytesRead);

    unsafe public static string[] getCharValues()
    {
        try
        {
            Process[] pr = Process.GetProcessesByName("Prozess.exe");
            Process process = pr[0];
            uint baseAdress = (uint)process.MainModule.BaseAddress.ToInt32();
            uint o = 0;
            byte[] ret = new byte[10];
            if(!ReadProcessMemory(process.Handle, (IntPtr)(process.MainModule.BaseAddress+0x156CC38), ret, (UInt32)ret.Length, ref o))
                return new string[] {Marshal.GetLastWin32Error().TosTring()};
            return new string[] { BitConverter.ToSingle(ret, 0).ToString() };
        }
        catch(IndexOutOfRangeException)
        {
            MessageBox.Show("Prozess ist vermutlich nicht geöffnet");
            return new string[] {""};
        }
    }
}
}

返回的字符串数组写在文本框中,但它总是为0.它实际上应该是大约8000(这是作弊引擎的输出) 我做错了什么?

错误代码是299

1 个答案:

答案 0 :(得分:0)

我只是很快写了这个,但这是个主意:

public float ReadFloat(InPtr address, HANDLE pHandle)
{

    byte[] memory = new byte[4];

    if (ReadProcessMemory(pHandle, address, memory, (UIntPtr)4, IntPtr.Zero))
    {
        float value = BitConverter.ToSingle(memory, 0);
        return value;
    }
    return 0.0f;
}

如果是双精度,则使用8个字节作为数组大小,确保读取8个字节并使用Bitconverter.ToDouble(),然后将float更改为double