Zmiana kodu czytającego wartości 4-bajtowe na czytający 8-bajtowe

0

Witam. Mam problem z kodem, gdyż czyta on tylko wartości 4 bajtowe. Chciałbym go zmienić na to, by czytał wartości double (8 bajtowe). Posiada ktoś wiedzę jak to zrobić? Co jest źle?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;


namespace ConsoleApp1
{
    class Program
    {
        [DllImport("kernel32.dll")]
        public static extern Int64 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);

        public static byte[] ReadBytes(IntPtr Handle, Int64 Address, uint BytesToRead)
        {
            IntPtr bytesRead;
            byte[] buffer = new byte[BytesToRead];
            ReadProcessMemory(Handle, new IntPtr(Address), buffer, BytesToRead, out bytesRead);
            return buffer;
        }

        public static int ReadInt32(Int64 Address, IntPtr Handle)
        {
            return BitConverter.ToInt32(ReadBytes(Handle, Address, 8), 0);
        }

        public static string ReadString(long Address, IntPtr Handle, uint length = 64)
        {
            return ASCIIEncoding.Default.GetString(ReadBytes(Handle, Address, length)).Split('\0')[0];
        }

        static void Main(string[] args)
        {
            Process File = Process.GetProcessesByName("TestFile")[0];
            IntPtr Handle = File.Handle;
            UInt32 HPAdr = 0xA036C68;
            Console.WriteLine("Sprawdzenie   : " + Convert.ToString(ReadInt32(HPAdr, Handle)));
            Console.ReadLine();
        }
    }
}

Ja się nad tym męczę i mi nie idzie to.

1

Spójrz na metodę ReadInt32. Tam zamiast 8 powinno być oczywiście 4.
Musisz napisać sobie nową metodę na tej samej zasadzie. Coś w stylu (piszę z głowy, więc coś może być nie tak):

public static double ReadDouble(Int64 Address, IntPtr Handle)
{
    return BitConverter.ToDouble(ReadBytes(Handle, Address, 8), 0);
}
0

Myślałem żeby to tak zrobić i mam taką funkcję:

 public static double ReadDouble(IntPtr handle, long address)
        {
            return BitConverter.ToDouble(ReadBytes(handle, address, 8), 0);
        }

Jednak wtedy mi tutaj wykrywa błąd:

 static void Main(string[] args)
        {
            Process File = Process.GetProcessesByName("TestFile")[0];
            IntPtr Handle = File.Handle;
            UInt32 XDAdr = 0xA036C68;
           Console.WriteLine("Sprawdzenie   : " + Convert.ToString(ReadDouble(`**<u>XDAdr, Handle</u>**`)));
            Console.ReadLine();
        }
0

Udało mi się to zrobić. Temat do zamknięcia. Dzięki :)

0

Kurde jednak nadal potrzebuję pomocy.
Ten kod działa w konsoli:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;


namespace ConsoleApp1
{
    class Program
    {
        [DllImport("kernel32.dll")]
        public static extern Int64 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);



        public static byte[] ReadBytes(IntPtr Handle, Int64 Address, uint BytesToRead)
        {
            IntPtr bytesRead;
            byte[] buffer = new byte[BytesToRead];
            ReadProcessMemory(Handle, new IntPtr(Address), buffer, BytesToRead, out bytesRead);
            return buffer;
        }


        //  public int GetInt32(IntPtr pHandle, Int32 address) {
        //      return BitConverter.ToInt32(ReadBytes(pHandle, new IntPtr(address), 4), 0); }



        public static double ReadDouble(Int64 Address, IntPtr Handle)
        {
            return BitConverter.ToDouble(ReadBytes(Handle, Address, 8), 0);

        }


        static void Main(string[] args)
        {
            Process File = Process.GetProcessesByName("testfile")[0];
            IntPtr Handle = File.Handle;
            UInt32 XDAdr = 0x9C79418;
            Console.WriteLine("Sprawdzenie   : " + Convert.ToString(ReadDouble(XDAdr, Handle)));
            Console.ReadLine();
        }
    }
}

Chcę by on działał w programie exe, lecz cokolwiek nie zrobię to nie chce on odpalić:

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

namespace MPC_FORUM_BOT
{
    public class MPC_MEMORY_READER
    {
        [DllImport("kernel32.dll")]
        
        private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);



        private byte[] ReadBytes(IntPtr Handle, Int64 Address, int bytesToRead)
        {
            int bytesRead = 0;
            byte[] buffer = new byte[bytesToRead];
            ReadProcessMemory(Handle, Address, buffer, bytesToRead, ref bytesRead);
            return buffer;
        }


        

        public int ReadDouble(Int64 Address, IntPtr Handle)
        {
            return BitConverter.ToDouble(ReadBytes(Handle, new IntPtr(Address), 8), 0);
        }



        UInt32 xDAdr = 0x9C79418;

         public int GetHealth(Int64 Address, IntPtr Handle)
        {
        return (ReadDouble(xDAdr, Handle));
        }
        
        
    }
}

Np. tutaj mi podkreśla:

ReadProcessMemory(Handle, Address, buffer, bytesToRead, ref bytesRead);                           <--Address
return BitConverter.ToDouble(ReadBytes(Handle, new IntPtr(Address), 8), 0);                           <--new IntPtr(Address)
0

A czym wg Ciebie różni się konsola od programu exe?

1 użytkowników online, w tym zalogowanych: 0, gości: 1