Jump to content
Sign in to follow this  
Windows Dragon

Ler strings guardadas em endereços de memoria de um processo

Recommended Posts

Windows Dragon

Boas pessoal... eu tenho um programa que lê o score do mw2 e o codigo que utilizo para ler é este

int bytesreaded;
byte[] b_arrray;
GammaJul.LgLcd.Samples.Wpf.MemoryReader test = new GammaJul.LgLcd.Samples.Wpf.MemoryReader("iw4mp.dat");
b_arrray = test.ReadAdress((IntPtr)0x007F4BA4, 1, out bytesreaded);
foreach (byte b in b_arrray)
           {
               string curr_mag_ammo = Convert.ToString(b);
           }
try{
           test.CloseHandle();
           }
           catch (Exception ex)
           {
           }

E tenho esta classe chamada MemoryReader

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

namespace GammaJul.LgLcd.Samples.Wpf
{
   class MemoryReader
   {
       private enum ProcessAccessFlags : uint
       {
           All = 0x001F0FFF,
           Terminate = 0x00000001,
           CreateThread = 0x00000002,
           VMOperation = 0x00000008,
           VMRead = 0x00000010,
           VMWrite = 0x00000020,
           DupHandle = 0x00000040,
           SetInformation = 0x00000200,
           QueryInformation = 0x00000400,
           Synchronize = 0x00100000
       }

       [DllImport("kernel32.dll")]
       static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
          UInt32 dwProcessId);

       [DllImport("kernel32.dll")]
       public static extern bool WriteProcessMemory(
           IntPtr hProcess,
           IntPtr lpBaseAddress,
           string lpBuffer,
           UIntPtr nSize,
           out IntPtr lpNumberOfBytesWritten
       );

       [DllImport("kernel32.dll")]
       public static extern Int32 CloseHandle(
       IntPtr hObject
       );

       [DllImport("kernel32.dll")]
       public static extern Int32 ReadProcessMemory(
           IntPtr hProcess,
           IntPtr lpBaseAddress,
           [in, Out] byte[] buffer,
           UInt32 size,
           out IntPtr lpNumberOfBytesRead
           );

       private IntPtr processHandle;

       public MemoryReader(string process_name)
       {
           OpenProcess(process_name);
       }

       private void OpenProcess(string process_name)
       {
           try
           {

               Process[] processes = Process.GetProcessesByName(process_name);
               if (processes.Length != 0)
               {
                   processHandle = OpenProcess(ProcessAccessFlags.All, false, (UInt32)processes[0].Id);
                   if (processHandle == null)
                   {

                   }
               }
               else
               {

               }
           }
           catch
           {

           }
       }

       public byte[] ReadAdress(IntPtr MemoryAddress, uint bytesToRead,
                                   out int bytesReaded)
       {

           byte[] buffer = new byte[bytesToRead];
           IntPtr ptrBytesReaded;

           ReadProcessMemory(processHandle, MemoryAddress, buffer, bytesToRead, out ptrBytesReaded);

           bytesReaded = ptrBytesReaded.ToInt32();

           return buffer;

       }

       public void CloseHandle()
       {
           int RetValue;
           RetValue = CloseHandle(processHandle);
           if (RetValue == 0)
               throw new Exception("CloseHandle failed");

       }
   }
}

O problema é que quero ler uma string e não estou a conseguir ler o valor... dá-me algo tipo 49 so enquanto o valor é 188.80.40.151

Alguma ajuda?

Share this post


Link to post
Share on other sites
Windows Dragon

Pronto já não preciso de ajuda.... basta converter o byte array para string...

aqui fica a solução para quem tiver o mesmo problema

string str;
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
str = enc.GetString(b_arrray);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.