Solved

VB6 Search for string in process memory

Posted on 2010-08-18
7
1,450 Views
Last Modified: 2012-06-21
I have a routine in VB6 that will allow me to find the PID of a running application. What I need to do is search for a text string within the memory being used by that application.

How do I best go about this? It should be as a normal text string - not Unicode.

Thanks
0
Comment
Question by:Chris Millard
  • 4
  • 2
7 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33462443
>>What I need to do is search for a text string within the memory being used by that application.what do u mean the memory being used by the app?can u give an example?
0
 
LVL 17

Author Comment

by:Chris Millard
ID: 33462530
Well, take a look at Process Memory Dumper from https://www.evilfingers.com/tools/ProcessMemoryDumper.php

It will dump the memory from a running process to a file on disk. I need to search the process memory rather than dump it.
0
 
LVL 27

Expert Comment

by:Ark
ID: 33508253
Check out my sample
Read-Write.zip
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 17

Author Comment

by:Chris Millard
ID: 33514856
Hi Ark,

I've seen this sample before, and unfortunately I can't get it to work. Instead of launching calc.exe what I am trying to do is scan the memory of an already running application (I know the PID) for a particular text string (not in unicode). I know that the string exists within the memory being used by the application, but your sample does not find the string.
0
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
ID: 33517299
Using:
Dim ret As Long
ret=ScanProcessMemory(pid,"Hello")
If ret Then
   MsgBox "Found [Hello] at position " & ret
Else
   MsgBox "Search string not found"
End If

Public Function ScanProcessMemory(ByVal pid As Long, ByVal strSearch As String) As Long
    Dim hProcess As Long, lpMem As Long, lLenMBI As Long
    Dim ret As Long, lWritten As Long, lPos As Long
    Dim sBuffer As String
    Dim si As SYSTEM_INFO
    Dim mbi As MEMORY_BASIC_INFORMATION
    
    hProcess = OpenProcess(PROCESS_READ_WRITE_QUERY, False, pid)
    lLenMBI = Len(mbi)
    Call GetSystemInfo(si)
    lpMem = si.lpMinimumApplicationAddress
    Do While lpMem < si.lpMaximumApplicationAddress
        mbi.RegionSize = 0
        ret = VirtualQueryEx(hProcess, ByVal lpMem, mbi, lLenMBI)
        If ret = lLenMBI Then
            If ((mbi.lType = MEM_PRIVATE) And (mbi.State = MEM_COMMIT)) Then ' this block is In use by this process
                If mbi.RegionSize > 0 Then
                   sBuffer = String(mbi.RegionSize, 0)
                   ReadProcessMemory hProcess, ByVal mbi.BaseAddress, ByVal sBuffer, mbi.RegionSize, lWritten
                   lPos = InStr(1, sBuffer, strSearch, vbTextCompare)
                   If lPos > 0 Then Exit Do
                End If
            End If
            On Error GoTo Finished
            lpMem = mbi.BaseAddress + mbi.RegionSize
            On Error GoTo 0
        Else
            Exit Do
        End If
    Loop
Finished:
   CloseHandle hProcess
   ScanProcessMemory = lPos
End Function

Open in new window

0
 
LVL 17

Author Comment

by:Chris Millard
ID: 33602420
Hi again Ark,

Try as I might, I still cannot get this to work - your example above does not find the string that I am searching for - yet I know it's there, because the ProcessMemoryDumper application I mentioned above, does dump the process memory to a file which I can then open and search using a hex editor...

Thanks

Chris
0
 
LVL 17

Author Closing Comment

by:Chris Millard
ID: 34358319
Thanks Ark, with a bit of tweaking, I got this to work.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

813 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

11 Experts available now in Live!

Get 1:1 Help Now