Page 1 of 1

Process memory usage

Posted: Sun Apr 25, 2004 11:47 am
by Kris_a
Hi,

How would I go about getting the amount of memory a process is using? I want to dabble with my own memory hacking tool, but I have no idea what range of addresses to actually search (0 - 4bn is a bit wide)

Thanks,

Kris

PS. I think the task manager uses the method I'm looking for.

Image

PPS. Sorry if this is in the wrong forum :roll:

Posted: Sun Apr 25, 2004 7:47 pm
by Henrik
Hi Kris
I duno if it's any help to you, i stumble over this in a VB forum, -what ever i was doing there, my Vb is very bad- :oops:
It's about mem usage for win98
http://www.visualbasicforum.com/t85703.html

as i cant convert it to pb for you, i just post the "Maybe solution" here
if you or another can convert i would like to see the pb version :
All the rest is from the forum:
Best Regards
Henrik
-------------
What I'm doing here is to calc the total of bytes of all the process' heaps.
Not sure if it's the same as showed by Win2K Task Manager.
Im on Win98, but MSDN says the functions works too in NT5 or greater.
Only pass the Process ID returned by Shell(),
and don't forget to divide the result by 1024....

Code: Select all

Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Declare Function Heap32ListFirst Lib "kernel32" (ByVal hSnapshot As Long, lphl As HEAPLIST32) As Long
Public Declare Function Heap32ListNext Lib "kernel32" (ByVal hSnapshot As Long, lphl As HEAPLIST32) As Long
Public Declare Function Heap32First Lib "kernel32" (lphe As HEAPENTRY32, ByVal th32ProcessID As Long, ByVal th32HeapID As Long) As Long
Public Declare Function Heap32Next Lib "kernel32" (lphe As HEAPENTRY32) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal Handle As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long

Public Const TH32CS_SNAPHEAPLIST = &H1&

Public Type HEAPLIST32
    dwSize        As Long
    th32ProcessID As Long
    th32HeapID    As Long
    dwFlags       As Long
End Type

Public Type HEAPENTRY32
    dwSize        As Long
    hHandle       As Long
    dwAddress     As Long
    dwBlockSize   As Long
    dwFlags       As Long
    dwLockCount   As Long
    dwResvd       As Long
    th32ProcessID As Long
    th32HeapID    As Long
End Type

Public Function GetProcessMemory(ByVal lngProcID As Long) As Long
Dim lngGPM As Long, _
    lngSnapHeap As Long, _
    hl32Heap As HEAPLIST32, _
    he32Heap As HEAPENTRY32
    lngGPM = 0
    lngSnapHeap = CreateToolhelp32Snapshot(TH32CS_SNAPHEAPLIST, lngProcID)
    If (lngSnapHeap > 0) Then
        hl32Heap.dwSize = Len(hl32Heap)
        If (Heap32ListFirst(lngSnapHeap, hl32Heap)) Then
            Do
                With hl32Heap
                    he32Heap.dwSize = Len(he32Heap)
                    If (Heap32First(he32Heap, .th32ProcessID, .th32HeapID)) Then
                        Do
                            lngGPM = lngGPM + he32Heap.dwBlockSize
                        Loop While (Heap32Next(he32Heap))
                    End If
                End With
            Loop While (Heap32ListNext(lngSnapHeap, hl32Heap))
        End If
        CloseHandle lngSnapHeap
    End If
    GetProcessMemory = lngGPM
End Function

Posted: Sun Apr 25, 2004 9:03 pm
by Kris_a
ah, thanks very much, I'll have a look at that now :D