I have created a memory read class for beginners

Posts 110 of 10 · Page 1 of 1
I have created a memory read class for beginners
This also has a text file read for pointers and such

Form your text files like this
[GetInfo]
Static=&H020304
P1=&H39
P2=&HAB

Code:
Public Class Memory
    Public Declare Function ReadMemoryBytes Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As IntPtr, <Out()> ByVal lpBuffer() As Byte, ByVal dwSize As Integer, ByRef lpNumberOfBytesRead As Integer) As Boolean
    Public Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Int32
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As ProcessAccessFlags, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Integer) As IntPtr
    Public Declare Function ReadMemory Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Int32, ByVal lpBaseAddress As Int32, ByRef lpBuffer As Int32, ByVal nSize As Int32, ByRef lpNumberOfBytesWritten As Int32) As Int32
    'Public Declare Function WriteProcessMemory Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer As Byte(), ByVal nSize As System.UInt32, <Out()> ByRef lpNumberOfBytesWritten As Int32) As Boolean
    Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer As Byte(), ByVal nSize As UInt32, ByRef lpNumberOfBytesWritten As UInt32) As Boolean

    Enum ProcessAccessFlags : uint
        All = &H1F0FFF
    End Enum

    Public Function GetProcess(ByVal ProcessTitle As String) As Long()
        Dim strReturn As Long
        Dim ReturnArray(100) As Long
        Dim idcount As Integer = 0
        Dim proc As Process = Process.GetCurrentProcess
        For Each proc In Process.GetProcesses

            If proc.MainWindowTitle = ProcessTitle Then
                ReturnArray(idcount) = proc.Id
                strReturn = proc.Id
                idcount = idcount + 1
            End If
        Next
        ReDim Preserve ReturnArray(idcount - 1)
        Return ReturnArray
    End Function


    Public Function ReadMemory(ByVal address() As Long, ByVal strIDVal As Long, ByVal dwSize As Integer) As Byte()
        Dim strReturn As Long
        If dwSize < 1 Then Return Nothing
        Dim buffer(dwSize - 1) As Byte
        Dim bytesRead As Integer = 0
        Dim winhandle As Long = OpenProcess(ProcessAccessFlags.All, True, strIDVal)

        For i = 0 To UBound(address) - 1
            ReadMemory(winhandle, address(i) + strReturn, strReturn, 4, 0)
        Next

        If ReadMemorybytes(winhandle, address(UBound(address)) + strReturn, buffer, dwSize, bytesRead) AndAlso bytesRead > 0 Then
            Return buffer
            CloseHandle(winhandle)
        Else
            Return Nothing

        End If
    End Function

    Public Sub WriteMemory(ByVal address() As Long, ByVal strIDVal As Long, ByVal value As Integer)
        Dim strreturn As Long
        Dim data() As Byte = BitConverter.GetBytes(value)
        Dim winhandle As IntPtr = OpenProcess(ProcessAccessFlags.All, True, strIDVal)
        For i = 0 To UBound(address)
            If i = UBound(address) Then
                WriteProcessMemory(winhandle, address(i) + strreturn, data, data.Length, 0&)
            End If
            ReadMemory(winhandle, address(i) + strreturn, strreturn, 4, 0)
        Next




    End Sub

    Public Function ReadMemoryArray(ByVal memRead As String) As Long()
        Dim Flags As String = "0"
        Dim count As Integer = 0
        Dim AppPath As String
        AppPath = Environment.CurrentDirectory()
        Dim sr As New System****.StreamReader("mytest.txt") 'Declare a streamreader
        Dim CurrentSection As String = "" 'Flag to track what section we are currently in
        Dim ThisLine As String = sr.ReadLine() 'Read in the first line
        Dim PArray(10) As Long
        Dim ArraySplit() As String
        Do Until Flags = "5"
            If InStr(LCase(ThisLine), LCase(memRead)) Then
                Flags = "1"
                GoTo NextLine
            End If

            Select Case Flags

                Case "1"
                    If ThisLine = "" Then
                        Flags = "5"
                        GoTo nextline
                    End If
                    ArraySplit = Split(ThisLine, "=")
                    PArray(count) = ArraySplit(1)
                    count = count + 1
            End Select

            If sr.Peek = -1 Then
                Flags = "5"
            End If
nextline:
            ThisLine = sr.ReadLine()
        Loop
        sr.Close() 'close the file
        ReDim Preserve PArray(count - 1)
        Return PArray
    End Function
End Class
good job!
)
thank you, i will be updating it more and more as i go!
I think you should make it easier to read.
I understand the code completely but it's overly complicated for such a simple task.

You're using OpenProcess way too often and not once did you even close the handle.
The only time i seen you even call Closehandle was after you return the buffer.
It doesn't work like that fella. If you're returning the buffer, closeHandle will never get called.

Besides only on the rare occasion do you even need OpenProcess.
Just use System.Diagnostic to obtain the handle. You won't even need to close the handle since it does it for you.
Quote Originally Posted by Pingo View Post
I think you should make it easier to read.
I understand the code completely but it's overly complicated for such a simple task.

You're using OpenProcess way too often and not once did you even close the handle.
The only time i seen you even call Closehandle was after you return the buffer.
It doesn't work like that fella. If you're returning the buffer, closeHandle will never get called.

Besides only on the rare occasion do you even need OpenProcess.
Just use System.Diagnostic to obtain the handle. You won't even need to close the handle since it does it for you.

thank you, i wrote all this from scratch and didnt realize my closehandle was after the buffer return *dizzy*... never tryed using system.diagnostic ill have too look into that!

---------- Post added at 07:03 AM ---------- Previous post was at 07:00 AM ----------

Quote Originally Posted by matthieu503 View Post
This method has been out for a pretty long time now. You can search it on MPGH search. C+P/Edit a few words much?
wow, talk about somone just trying to be rude. no actually i did not copy and paste at all.. i wrote this from scratch. funny you say "C+P/Edit a few words much" when there is only 1 routine to do it the way i did does not mean i C+P/Edit... now go troll some where else unless you got somthing to say to make the code better.
This method has been out for a pretty long time now. You can search it on MPGH search. C+P/Edit a few words much?
You were using System.Diagnostic
Dim proc As Process = Process.GetCurrentProcess (which you should remove since its never used)
Process.GetCurrentProcess will return the calling process (your app)

A simple function to get the process handle but i usually just store the handle.
Code:
    Function GetHandle(ByVal ProcessName As String) As IntPtr
        Dim P As Process() = Process.GetProcessesByName(ProcessName)
        Return If(P.Length <> 0, P(0).Handle, IntPtr.Zero)
    End Function
I have a super simple memory class if you want me to send it to you.
I'm not a pro by all means but you might find something useful in it.
Quote Originally Posted by Pingo View Post
You were using System.Diagnostic
Dim proc As Process = Process.GetCurrentProcess (which you should remove since its never used)
Process.GetCurrentProcess will return the calling process (your app)

A simple function to get the process handle but i usually just store the handle.
Code:
    Function GetHandle(ByVal ProcessName As String) As IntPtr
        Dim P As Process() = Process.GetProcessesByName(ProcessName)
        Return If(P.Length <> 0, P(0).Handle, IntPtr.Zero)
    End Function
I have a super simple memory class if you want me to send it to you.
I'm not a pro by all means but you might find something useful in it.
never thought of that ahahah. so ill just return proc.handle and skip the id bull crap *dizzy*
It's up to you how you want to do it.
By far the easiest way is to get it by process name.
You'l need the id to use OpenProcess but you can do away with that too.
In most cases, getting the handle the way i showed you will be enough.
People are hardly going to believe a guy who has 6 posts that he didn't copy paste it all. If you didn't, good job. If you did, well you know.
Posts 110 of 10 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?