Can VB Monitor Memory Changes

Posted on 2006-06-02
Last Modified: 2012-06-21
I'm trying to write a trainer creator for games using vb. In this, I need to pick a running process and be able to monitor changes it makes to memory locations.

Is this something VB can do, and if so, where do I start?  Code examples would be of great help.

Thanks in advance

Question by:skipper68
    LVL 12

    Expert Comment

    Hi skipper68, has some good info on using the WIN API will allow you to work with memory locations.
    LVL 13

    Accepted Solution

    LVL 9

    Author Comment

    Thanks Bob, I'll check that out.

    What I have:
    Currently, I can list the running processes and select one.
    I found code online that will allow me to write to a specific memory address (i.e. &HC847BC) but I'm not at that point yet

    What I don't have yet:
    hwnd = FindWindow(vbNullString, WinName) doesn't return a value even for a valid open window

    From there I would like to search that processes memory space for a specific value, and store those values.
    Then search within those values for the new value.

    Once identified, I'd like to store that memory space into another list that I can modify in the future.  
    Also, I'd like it to save that information so i can come back to it another day.
    LVL 9

    Author Comment

    the one solution you provided is very close.  I just need to identify which memory location the specific process is using.  I don't want to have to crawl the entire memory.  

    How would I know where the memory starts and ends for a process (Start at address &x end at address &x)?
    LVL 19

    Assisted Solution

    I'll give you an example of a trainer I wrote in Visual Basic, for the Demo Version of Casino Empire.

    Public gbuff(3) As Byte
    Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Public Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
    Public Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

    Public Function CasinoEmpirePID(Optional FullVersion As Boolean) As Long
    Dim tID, pID, retCode As Long
    Dim winHandle As Long
    winHandle = FindWindow("Casino Empire", vbNullString)
    If winHandle <> 0 Then FullVersion = True
    If winHandle = 0 Then winHandle = FindWindow("Casino Empire Demo", vbNullString)
    retCode = GetWindowThreadProcessId(winHandle, pID)
    CasinoEmpirePID = pID
    End Function

    Public Sub SetMoney()
    On Error Resume Next
    Dim pID As Long
    Dim lpsz As Long
    Dim Version As Boolean
    pID = CasinoEmpirePID(Version)
    If pID = 0 Then Exit Sub
    If Version = True Then
        MsgBox "Full Version"
        lpsz = &H139E9FF 'Memory Location For Money
        MsgBox "Demo"
        lpsz = &HDD3380 'Memory Location For Money
    End If
    hProcess = OpenProcess(&H0, False, pID)
        gbuff(0) = 64
        gbuff(1) = 66
        gbuff(2) = 15
        t = WriteProcessMemory(hProcess, ByVal lpsz, gbuff(0), 3, lwritten)
    End Sub

    LVL 13

    Expert Comment

    The second url is a link to a vb program that enums processes and scans a process memory only not the entire memory looking for a value to replace. It lists the addresses all the found instances of the value relative to the process memory (with an offset to the main memory).

    To demonstrate whats happening open notepad and past the text: I AM A STRING then choose the notepad from the program list and press start search button. When it finds it remember the address

    Open another notepad instance and dont write anything in it. Select it from the program list after rerunning the program to refresh the windows list, enter an address before the previous found one and start search and you'll see that it doesn't find anything at the same address. Stop search and select the other notepad and do the same for the begining address and search, and you'll find the the text at the memory address.

    What we conclude is that the code is scanning the process address not the entire address using tha api ReadProcessMemory and the bellow is a description of this api and I doubt you'll find it in

    The ReadProcessMemory function reads memory in a specified process. The entire area to be read must be accessible, or the operation fails.

    BOOL ReadProcessMemory(

        HANDLE hProcess,      // handle of the process whose memory is read  
        LPCVOID lpBaseAddress,      // address to start reading
        LPVOID lpBuffer,      // address of buffer to place read data
        DWORD nSize,      // number of bytes to read
        LPDWORD lpNumberOfBytesRead       // address of number of bytes read



    Identifies an open handle of a process whose memory is read. The handle must have PROCESS_VM_READ access to the process.


    Points to the base address in the specified process to be read. Before any data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for read access. If this is the case, the function proceeds; otherwise, the function fails.


    Points to a buffer that receives the contents from the address space of the specified process.


    Specifies the requested number of bytes to read from the specified process.


    Points to the actual number of bytes transferred into the specified buffer. If lpNumberOfBytesRead is NULL, the parameter is ignored.


    Return Values

    If the function succeeds, the return value is nonzero.
    If the function fails, the return value is zero. To get extended error information, call GetLastError.
    The function fails if the requested read operation crosses into an area of the process that is inaccessible.


    ReadProcessMemory copies the data in the specified address range from the address space of the specified process into the specified buffer of the current process. Any process that has a handle with PROCESS_VM_READ access can call the function. The process whose address space is read is typically, but not necessarily, being debugged.
    The entire area to be read must be accessible. If it is not, the function fails as noted previously.
    LVL 19

    Expert Comment

    >>>the one solution you provided is very close.  I just need to identify which memory location the specific process is using.  I don't want to have to crawl the entire memory.  

    You have no choice by walk the memory and look for changes when your Money, Lives", etc change.
    LVL 9

    Author Comment


    How'd you find the memory location addresses?  Do you have a prog that will do that?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now