 |
|
 |
Goran Level: Moderator
 Registered: 16-05-2002 Posts: 1681
|
Re: Task Manager process info
Hi, Steve, the code to get memory usage is in the GetMemoryUsage function. But if it is called from form_load event, then it wont show the correct value, since the application is still loading, and will dispay less value than is. So, for demonstration purpose, I have used SetTimer and KillTimer API's to create a timer that will call this function every second.
' form code
Option Explicit
Private Sub Form_Load()
SetTimer Me.hwnd, 0, 1000, AddressOf TimerProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
KillTimer Me.hwnd, 0
End Sub |
' module code
Option Explicit
Const PROCESS_QUERY_INFORMATION = 1024
Const PROCESS_VM_READ = 16
Type PROCESS_MEMORY_COUNTERS
cb As Long
PageFaultCount As Long
PeakWorkingSetSize As Long
WorkingSetSize As Long
QuotaPeakPagedPoolUsage As Long
QuotaPagedPoolUsage As Long
QuotaPeakNonPagedPoolUsage As Long
QuotaNonPagedPoolUsage As Long
PagefileUsage As Long
PeakPagefileUsage As Long
End Type
Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Declare Function GetProcessMemoryInfo Lib "PSAPI.DLL" (ByVal hProcess As Long, ppsmemCounters As PROCESS_MEMORY_COUNTERS, ByVal cb As Long) As Long
Public Function GetMemoryUsage() As Long
Dim pID As Long
Dim pHandle As Long
Dim PMC As PROCESS_MEMORY_COUNTERS
pID = GetCurrentProcessId
pHandle = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, pID)
PMC.cb = LenB(PMC)
GetProcessMemoryInfo pHandle, PMC, PMC.cb
GetMemoryUsage = CLng(PMC.WorkingSetSize / 1024)
End Function
Public Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Form1.Caption = GetMemoryUsage
End Sub |
____________________________
If you find the answer helpful, please mark this topic as solved.
|
|
14-06-2005 at 08:03 PM |
|
|
steve_w Level: Moderator

 Registered: 18-04-2003 Posts: 1156
|
Re: Task Manager process info
This yours Goran that I changed.
Public Function GetMemoryUsage(ByVal pID As Long) As Long
Dim pHandle As Long
Dim PMC As PROCESS_MEMORY_COUNTERS
pHandle = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, pID)
PMC.cb = LenB(PMC)
GetProcessMemoryInfo pHandle, PMC, PMC.cb
GetMemoryUsage = CLng(PMC.WorkingSetSize / 1024)
End Function |
and this from bits I found and changed.
Private Const TH32CS_SNAPPROCESS = &H2
Private Const INVALID_HANDLE = &HFFFFFFFF
Private Const MAX_PATH As Long = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" _
(ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" _
(ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" _
(ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "kernel32" _
(ByVal hPass As Long)
Private Function GetProcessIDForExe(ByVal exename As String) As Long
Dim hSnapshot As Long
Dim pe As PROCESSENTRY32
'create a snapshot of the system
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapshot <> INVALID_HANDLE Then
'initialize the type
pe.dwSize = Len(pe)
If hSnapshot <> INVALID_HANDLE Then
Call Process32First(hSnapshot, pe)
'loop through processes
Do
If LCase(Left(pe.szExeFile, Len(exename))) = LCase(exename) Then
Exit Do
End If
Loop Until Process32Next(hSnapshot, pe) = 0
If pe.th32ProcessID <> 0 Then
GetProcessIDForExe = pe.th32ProcessID
Else
GetProcessIDForExe = 0
End If
End If
CloseHandle hSnapshot
End If
End Function |
Thanks Steve
|
|
15-06-2005 at 10:51 AM |
|
|
PullingMyHairOut Level: Protégé
 Registered: 08-08-2005 Posts: 5
|
Re: Task Manager process info
Is there a way to do this with the CPU Percentage for a specific process?
That would be well handy!
Cheers.
|
|
08-08-2005 at 08:00 PM |
|
|
PullingMyHairOut Level: Protégé
 Registered: 08-08-2005 Posts: 5
|
Re: Task Manager process info
Steve,
Do you think it may be impossible to do the same thing with CPU Usage?
I've searched the net for days and can find it wrote in C but not in VBA.
|
|
12-08-2005 at 11:51 AM |
|
|
Asim-GDI GURU Level: Sage
 Registered: 29-07-2005 Posts: 54
|
Re: Task Manager process info
Hey don't pull out your hairs and tell me the C code.I'll try to change it for you.
I'm waiting for the code...
Regards,
Asim Siddiqui.
|
|
13-08-2005 at 08:22 AM |
|
|
PullingMyHairOut Level: Protégé
 Registered: 08-08-2005 Posts: 5
|
Re: Task Manager process info
Thanks Asim, you're very kind.
This is the Link......
http://www.codeproject.com/system/cpuusage.asp
.......the code is on there. The designer has a method of retrieving the CPU usage of a specific thread. I cant post the code itself as threr are 8 files and I don't know which ones to post. I had to signup (for free) to download the source.
Thanks agin Asim.
|
|
13-08-2005 at 08:05 PM |
|
|
misterxed Level: VB Lord

 Registered: 12-06-2005 Posts: 151
|
Re: Task Manager process info
Good newz guyz!
MSDN comes to the rescue!
Check out the link below, u'll find the solution.. but its only for NT/2000 .... U can try d/loading PDH.dll and installing in other windows versions as well, but i'm not sure if it'll work 
http://support.microsoft.com/default.aspx?scid=kb;en-us;296526
Check this one out as well...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_helper.asp
and yeah, Asim, i bet u can't change it I've already tried changin it, it went WELL above ma head ... Anywayz, give it ur best shot, maybe i'm not that good at C as u r... So.. Go ahead, good luck (now i've sorta challenged u, u must do it )
P.S. Guyz, i tried a quick Copy-Paste-Run on the code on the above link, didn't work for me.. But i didnt give it another try.. Maybe when i'm interested in this stuff a bit more, i'd do some work on it... Its Microsoft code, it must run, it must have been my problem...
[Edited by misterxed on 14-08-2005 at 06:44 AM GMT]
____________________________
lOsT...
|
|
14-08-2005 at 01:38 AM |
|
|
saormart Level: Trainee
 Registered: 10-03-2006 Posts: 1
|
Re: Task Manager process info
Hi ... do you have Source Code of this project ?
|
|
10-03-2006 at 04:59 PM |
|
|
|
|
 |
 |