Solved

How to monitor the applications running in the Task Manager

Posted on 2006-07-13
5
296 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

830 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