Solved

VB6 Search for string in process memory

Posted on 2010-08-18
7
1,441 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

895 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

13 Experts available now in Live!

Get 1:1 Help Now