Can VB Monitor Memory Changes

Posted on 2006-06-02
Medium Priority
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
  • 3
  • 2
  • 2
  • +1
LVL 12

Expert Comment

by:Bob Lamberson
ID: 16821796
Hi skipper68,

http://www.allapi.net/ has some good info on using the WIN API will allow you to work with memory locations.
LVL 13

Accepted Solution

iHadi earned 700 total points
ID: 16821830

Author Comment

ID: 16821891
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 16821952
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

BrianGEFF719 earned 300 total points
ID: 16822566
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

ID: 16822574
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 allapi.net:

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

ID: 16822575
>>>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.

Author Comment

ID: 16825372

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


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 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