Solved

How to monitor the applications running in the Task Manager

Posted on 2006-07-13
5
293 Views
Last Modified: 2013-12-26
I have a need to monitor the Task Manager for a program running in Windows.  The name of the program is XConfig.exe.  I need to be able to watch for it to be launched by another program at any time - so the program I need will run continuously and set a flag that says "Yes, XConfig.exe is running right now"...get the idea?

I have done some searching and found some reference to the following Windows API functions, but am not sure how to set this up.

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Seems like I need a program that runs a continuous loop, with some time management or something to keep it from taking system memory up unnecessarily...my experience is in database applications, so not sure how to do this kind of thing.

I am using VB 6.

Thanks for your assistance!

Mike
0
Comment
Question by:MikeORTEC
  • 3
  • 2
5 Comments
 

Author Comment

by:MikeORTEC
ID: 17104632
OK, I found something to get started with, but it needs some assistance:  The following is cut/pasted/modified to go through the list one time...it needs to go through the list say...once every 5 seconds or so...I used Notepad as the program that I am looking for in this example, so that anyone could run the code from vb6...Mike


Private Const TH32CS_SNAPPROCESS = &H2
Private Const MAX_PATH = 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 dwFlags As Long, ByVal PID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, pe32 As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, pe32 As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub Command1_Click()
 Cls
 Dim hSnapShot As Long: hSnapShot = 0
 Dim pe32 As PROCESSENTRY32

 hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
 If hSnapShot = -1 Then
 MsgBox "error"
 Exit Sub
 End If

 pe32.dwSize = Len(pe32)

 If Process32First(hSnapShot, pe32) Then
 Do
 Print pe32.szExeFile
 Loop While Process32Next(hSnapShot, pe32)
    If pe32.szExeFile = Notepad Then
        MsgBox "Notepad is running"
    End If
 Else
 MsgBox "process32first failed!"
 End If

 CloseHandle hSnapShot
End Sub
0
 

Author Comment

by:MikeORTEC
ID: 17105041
OK...I made some further progress...the problem that I am having is that the returned strings from the Print statement are appended with a bunch of nonsense...so I cant recognize that the program that I am looking for is there...how can I format the retruned values are just like "notepad.exe"?

Private Const TH32CS_SNAPPROCESS = &H2
Private Const MAX_PATH = 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 dwFlags As Long, ByVal PID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, pe32 As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, pe32 As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Sub Form_Load()

   'Can do this on the form itself at design time and leave this statement out
   Timer1.Interval = 60000   ' Set Timer interval to one minute,
End Sub

Private Sub Timer1_Timer()
   'Declaring it Static will perserve this variable through the calls
   Static minutesPassed As Integer

   minutesPassed = minutesPassed + 1
   If minutesPassed = 3 Then
      minutesPassed = 0
      Dim i As Integer
      For i = 1 To 50
     
         'Do your task here
         Cls
 Dim hSnapShot As Long: hSnapShot = 0
 Dim pe32 As PROCESSENTRY32

 hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
 If hSnapShot = -1 Then
 MsgBox "error"
 Exit Sub
 End If

 pe32.dwSize = Len(pe32)

 If Process32First(hSnapShot, pe32) Then
 Do
 Print pe32.szExeFile
 
 If pe32.szExeFile = "Notepad.exe" Then
        MsgBox "Notepad is running"
        Exit Sub
 End If
   
 Loop While Process32Next(hSnapShot, pe32)
   
 Else
 MsgBox "process32first failed!"
 End If

 CloseHandle hSnapShot

      Next
   End If


End Sub
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 17106718
This is a sample with clean strings :
========================

Needs:
1 Form


Private Const TH32CS_SNAPPROCESS = &H2
Private Const MAX_PATH = 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 dwFlags As Long, ByVal PID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, pe32 As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, pe32 As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub Form_Load()
Move 0, 0, 15 * 400, 15 * 300: AutoRedraw = -1
 Cls
 Dim hSnapShot As Long: hSnapShot = 0
 Dim pe32 As PROCESSENTRY32

 hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
 If hSnapShot = -1 Then
 MsgBox "error"
 Exit Sub
 End If

 pe32.dwSize = Len(pe32)

 If Process32First(hSnapShot, pe32) Then
 Do
 pg = InStr(1, pe32.szExeFile, Chr(0))
 Print Left(pe32.szExeFile, pg - 1)
 Loop While Process32Next(hSnapShot, pe32)
    If pe32.szExeFile = Notepad Then
        MsgBox "Notepad is running"
    End If
 Else
 MsgBox "process32first failed!"
 End If

 CloseHandle hSnapShot
End Sub
0
 
LVL 17

Accepted Solution

by:
vb_elmar earned 500 total points
ID: 17106733
This is how to detect Notepad :
==========================


Private Const TH32CS_SNAPPROCESS = &H2
Private Const MAX_PATH = 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 dwFlags As Long, ByVal PID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, pe32 As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, pe32 As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub Form_Load()
Dim Nir As Long
Move 0, 0, 15 * 400, 15 * 300: AutoRedraw = -1
 Cls
 Dim hSnapShot As Long: hSnapShot = 0
 Dim pe32 As PROCESSENTRY32

 hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
 If hSnapShot = -1 Then
 MsgBox "error"
 Exit Sub
 End If

 pe32.dwSize = Len(pe32)

 If Process32First(hSnapShot, pe32) Then
 Do
 pg = InStr(1, pe32.szExeFile, Chr(0))
 
 If InStr(1, Left(pe32.szExeFile, pg - 1), "notepad.exe", vbTextCompare) Then Nir = 1
 
 
 Loop While Process32Next(hSnapShot, pe32)
    If pe32.szExeFile = Notepad Then
        MsgBox "Notepad is running"
    End If
 Else
 MsgBox "process32first failed!"
 End If

 CloseHandle hSnapShot
 
     
If Nir = 1 Then
     MsgBox "Notepad is running"
 Else
    MsgBox "Notepad is not running"
 End If
 End
End Sub
0
 

Author Comment

by:MikeORTEC
ID: 17112780
Works like a champ!  Had to make some mods to make it work with the app...will post when it is complete.

Would like to get some of the api stuff commented better and will try to do so and post here...perhaps you can look after I do.

Thanks vb_Elmar!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

12 Experts available now in Live!

Get 1:1 Help Now