Reading integer from memory

01/03/2013 22:09 stranded123#1
i us VB and "ReadWritingMemory.VB"

WriteInteger("PurblePlace", &H5D0060, 23)
so i use this code to write an integer to an memory and in this case the value is "23"

but i cant get " ReadInteger("PurblePlace", &H5D0060, x)" to work
and in this case i wanna read the value from the address that in this case is
&H5D0060 and store the value in the integer "x"

but cant get it to work can someone help me ??

BUMP
01/06/2013 18:12 MrSm!th#2
#moved
01/07/2013 20:14 .NoThx#3
I dotn know so much about Vb's Read/Write Process memory, but usually u need just one argument to read from teh Memory..?
01/08/2013 03:30 »jD«#4
I never heard of any of those VB functions, but generally the way to go is importing Windows API DLLs and calling them.

EDIT: I would still use my below classes, but I think that 'x' in your ReadMemory call is an "out" parameter. Could be wrong tho...

If you have to read and write from/to a specific address that is known, then you could use something like what I generally use:

Code:
<Flags> _
Public Enum ProcessAccessFlags As UInteger
	All = &H1f0fff
	Terminate = &H1
	CreateThread = &H2
	VMOperation = &H8
	VMRead = &H10
	VMWrite = &H20
	DupHandle = &H40
	SetInformation = &H200
	QueryInformation = &H400
	Synchronize = &H100000
End Enum

<DllImport("kernel32.dll")> _
Private Shared Function OpenProcess(dwDesiredAccess As ProcessAccessFlags, <MarshalAs(UnmanagedType.Bool)> bInheritHandle As Boolean, dwProcessId As Integer) As IntPtr
End Function

<DllImport("kernel32.dll", SetLastError := True)> _
Private Shared Function WriteProcessMemory(hProcess As IntPtr, lpBaseAddress As IntPtr, lpBuffer As Byte(), nSize As UInteger, lpNumberOfBytesWritten As Integer) As Boolean
End Function

<DllImport("kernel32.dll", SetLastError := True)> _
Private Shared Function ReadProcessMemory(hProcess As IntPtr, lpBaseAddress As IntPtr, <Out> lpBuffer As Byte(), dwSize As Integer, lpNumberOfBytesRead As Integer) As Boolean
End Function

<DllImport("kernel32.dll")> _
Public Shared Function CloseHandle(hProcess As IntPtr) As Int32
End Function

Public Function ReadMemory(proc As Process, MemoryAddress As IntPtr, bytesToRead As UInteger, bytesRead As Integer) As Byte()
	Dim pointerProc As IntPtr = OpenProcess(ProcessAccessFlags.All, False, proc.Id)
	Dim buffer As Byte() = New Byte(bytesToRead - 1) {}

	Dim ptrBytesRead As IntPtr
	ReadProcessMemory(pointerProc, MemoryAddress, buffer, bytesToRead, ptrBytesRead)
	CloseHandle(pointerProc)

	bytesRead = ptrBytesRead.ToInt32()

	Return buffer
End Function

Public Sub WriteMemory(proc As Process, MemoryAddress As IntPtr, bytesToWrite As Byte(), bytesWritten As Integer)
	Dim pointerProc As IntPtr = OpenProcess(ProcessAccessFlags.All, False, proc.Id)

	Dim ptrBytesWritten As IntPtr
	WriteProcessMemory(pointerProc, MemoryAddress, bytesToWrite, CUInt(bytesToWrite.Length), ptrBytesWritten)
	CloseHandle(pointerProc)


	bytesWritten = ptrBytesWritten.ToInt32()
End Sub
You could then use it like this:

Code:
Dim proc As Process = Process.GetProcessesByName("SomeRandomProcess").FirstOrDefault()
Dim memaddr As Integer = &H1234567
Dim bytesRead As Integer
Dim value As Byte() = ReadMemory(process, address, 4, bytesRead)
Dim memoryValue As Integer = BitConverter.ToInt32(value, 0)
Hope it compiles, haven't written VB in years!!

-jD
01/17/2013 20:52 stranded123#5
Thx for showing intress »jD«
but did figur it ut by using a other code

[Only registered and activated users can see links. Click Here To Register...]

the just change "target" in Module1.VB to ur exe name with out the ".exe"

if u get error in Module1.VB it can be becuse he forgot the " _" on

<DllImport("kernel32.dll", SetLastError:=True)> " _ " <----- her
Private Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

and add to an buttom or any thing ells as an exampel

Code:
    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  
Dim myIntValue1 As Integer = ReadMemory(Of Integer)(&HF013F8C) ' Read memory &HF013F8C
    Label1.Text = myIntValue1
    End Sub

hope this post will help someone;)