Can VB Monitor Memory Changes

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

skipper68Application Development ManagerAsked:
Who is Participating?
Bob LambersonSoftware EngineerCommented:
Hi skipper68, has some good info on using the WIN API will allow you to work with memory locations.
skipper68Application Development ManagerAuthor Commented:
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.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

skipper68Application Development ManagerAuthor Commented:
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)?
BrianGEFF719Connect With a Mentor Commented:
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

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.
>>>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.
skipper68Application Development ManagerAuthor Commented:

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.