MikeORTEC
asked on
How to monitor the applications running in the Task Manager
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
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
ASKER
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(T H32CS_SNAP PROCESS, 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
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(T
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
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(T H32CS_SNAP PROCESS, 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
========================
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(T
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!
ASKER
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(T
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