简体   繁体   中英

Reading a float value from an external adress

i am trying to get a float value from an external adress. To do this, i am using this code:

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[] {""};
        }
    }
}
}

The returned string array is written down in a text box, but it is always 0. it should actually be about 8000 (that's the output of cheat engine) What am i doing wrong?

Error code is 299

I just wrote this quickly but this is the idea:

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;
}

If it's double precision, use 8 bytes for the array size, make sure you read 8 bytes and use Bitconverter.ToDouble(), and change float to double

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM