borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2017 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (How to rotate a drawing object) New Topic New Poll Post Reply
AndreaVB Forum : API : Task Manager process info
Poster Message
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1138

icon Task Manager process info

Hi

Can anyone help me get the memory usage for a process, its the value in the Windows Task Manager, Processes Tab.


Cheers Steve  

14-06-2005 at 11:30 AM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1652
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1138
icon Re: Task Manager process info

Thanks Goran, but sorry I did not make myself clear enough. I want to be able to have a program that monitors another process.

What I have is a dcom object running on a server which occasionally seems to hang. What I want to do is monitor the memory usage with another program and if the memory usage is not changing that it means it has stopped and not unloaded itself so I want to kill it, because it has to run as single instance it stops any subsequent users from using it.


Cheers Steve

Okay, your code will still work but now I need to find the processid for the other application from its exe name.

[Edited by steve_w on 15-06-2005 at 09:43 AM GMT]

Okay, think I got it, I'll post back when done. Thanks

[Edited by steve_w on 15-06-2005 at 10:21 AM GMT]

15-06-2005 at 08:44 AM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1138
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1652
icon Re: Task Manager process info

Ye, thats about it, altough you have duplicate

If hSnapshot <> INVALID_HANDLE Then

so you can safely remove one. I was on the fishing yesterday, so I couldnt send you the code, but you managed it prety good by yourself, as expected.  

____________________________
If you find the answer helpful, please mark this topic as solved.

16-06-2005 at 01:23 PM
View Profile Send Email to User Show All Posts | Quote Reply
PullingMyHairOut
Level: Protégé

Registered: 08-08-2005
Posts: 5
icon Re: Task Manager process info

Guys,

I Have registerd specilly to say thanks a million.  I have spent two days trawling thru the net looking for this type of thing that can tell when an application hangs.

Thanks to you both,  you're very clever

Best Regards

08-08-2005 at 03:16 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1138
icon Re: Task Manager process info

Glad to help  

Here's my finished program.

Steve

____________________________
Attached:
MemoryMonitor.zip 13 KB (Downloads: 92)

08-08-2005 at 03:53 PM
View Profile Send Email to User Show All Posts | Quote Reply
PullingMyHairOut
Level: Protégé

Registered: 08-08-2005
Posts: 5
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
misterxed
Level: Wizard


Registered: 12-06-2005
Posts: 151
icon Re: Task Manager process info

Hi there!
Steve, thats some REALLY great work there!
One thing, i couldnt get clearly....
What is CPU time exactly? Does that mean how  much  processor time a process is taking? If thats correct, then ur program doesnt do that it simply counts the no of seconds since the monitoring started... It doesn't work on the basis of the "CPU Time" u get from the Task Manager... Am i missing something here?


____________________________
lOsT...

09-08-2005 at 06:52 PM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1138
icon Re: Task Manager process info

Hi Misterxed

Thanks for your comments, I'll try and give you an explanation. I have a dcom object that runs an a server and occasionally it locks ups. So I wrote this app to look for when its running and monitor the memory usage, if the usage remains constant for above a set time then I will kill it.

The memory usage is read at the line
lblValue.Caption = GetMemoryUsage(pID)


then I compare it to the previous value
If lblValue.Caption <> lastvalue Then


if it is the same and above the time that I set then I kill the process
If chkKill.Value = vbChecked And Val(lblTime.Caption) > Val(txtSeconds.Text)


To be honest the code doesn't flow that well but I was mixing bits of other peoples code to get it to do what I wanted.
If I were to start again it would not look like that .


Steve

[Edited by steve_w on 10-08-2005 at 10:49 AM GMT]

10-08-2005 at 10:47 AM
View Profile Send Email to User Show All Posts | Quote Reply
PullingMyHairOut
Level: Protégé

Registered: 08-08-2005
Posts: 5
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1138
icon Re: Task Manager process info

Hi

Sorry didn't reply 'cos I didn't know the answer, still don't.  

Steve  

12-08-2005 at 12:12 PM
View Profile Send Email to User Show All Posts | Quote Reply
PullingMyHairOut
Level: Protégé

Registered: 08-08-2005
Posts: 5
icon Re: Task Manager process info

Thanks Steve,

I hate to say it but I don't think it can be done.  As I said,  I've found a C version using API calls but I'm not experienced enough to know how it's doing it.

It must be possible.  I need to trigger code when CPU usage falls back to Zero.  Winodws does it woth it's little Hourglass cursor!!

I'll keep looking  

Tel

[Edited by PullingMyHairOut on 12-08-2005 at 01:13 PM GMT]

12-08-2005 at 01:12 PM
View Profile Send Email to User Show All Posts | Quote Reply
Asim-GDI GURU
Level: Sage

Registered: 29-07-2005
Posts: 50
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
PullingMyHairOut
Level: Protégé

Registered: 08-08-2005
Posts: 5
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
misterxed
Level: Wizard


Registered: 12-06-2005
Posts: 151
icon 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
View Profile Send Email to User Show All Posts | Quote Reply
Asim-GDI GURU
Level: Sage

Registered: 29-07-2005
Posts: 50
icon Re: Task Manager process info

Hello folks,
First of all Mister Xed no need to give me such chalenges.Afterall I'm still your student .Now come to the point.The code is extremely difficult so I searched for a simple way of doing it and found something that might solve the problem.See if it can help you guys.And if its what you people want then tell me.I'll just ONLY try to change it to VB.

CPU Usage

Regards,
Asim Siddiqui.

14-08-2005 at 09:28 PM
View Profile Send Email to User Show All Posts | Quote Reply
saormart
Level: Trainee

Registered: 10-03-2006
Posts: 1
icon Re: Task Manager process info

Hi ... do you have Source Code of this project ?

10-03-2006 at 04:59 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
ianr
Level: Trainee

Registered: 27-01-2011
Posts: 1
icon Re: Task Manager process info

Excellent, Thanks

27-01-2011 at 09:20 AM
View Profile Send Email to User Show All Posts | Quote Reply
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1138
icon Re: Task Manager process info

Just found my own code from 12 years ago on Google  

22-02-2017 at 09:44 AM
View Profile Send Email to User Show All Posts | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 538
icon Re: Task Manager process info

Good to see you again! hope you're fine
  

____________________________
AndreaVB

22-02-2017 at 10:01 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
AndreaVB Forum : API : Task Manager process info
Next Topic (How to rotate a drawing object) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2017 Andrea Tincaniborder