Check For Running Process
Hi guy, i use this code to check if an aplication is still running
VB Code:
Public Function isRunning(ByVal Process As String) As Boolean
Dim objWMIService, colProcesses
Set objWMIService = GetObject("winmgmts:")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process where name='" & Process & "'")
If colProcesses.Count Then
isRunning = True
Else
isRunning = False
End If
End Function
but it's kindda slow to open the winmgmts and use queury to execute to see if the process is still there. i jst want to know if you guys have a better way to check for the process
Re: Check For Running Process
try this:
VB Code:
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" ( _
ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Declare Function EnumProcesses Lib "PSAPI.DLL" ( _
lpidProcess As Long, ByVal cb As Long, cbNeeded As Long) As Long
Private Declare Function EnumProcessModules Lib "PSAPI.DLL" ( _
ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, lpcbNeeded As Long) As Long
Private Declare Function GetModuleBaseName Lib "PSAPI.DLL" Alias "GetModuleBaseNameA" ( _
ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Const PROCESS_VM_READ = &H10
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Function IsProcessRunning(ByVal sProcess As String) As Boolean
Const MAX_PATH As Long = 260
Dim lProcesses() As Long, lModules() As Long, N As Long, lRet As Long, hProcess As Long
Dim sName As String
sProcess = UCase$(sProcess)
ReDim lProcesses(1023) As Long
If EnumProcesses(lProcesses(0), 1024 * 4, lRet) Then
For N = 0 To (lRet \ 4) - 1
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcesses(N))
If hProcess Then
ReDim lModules(1023)
If EnumProcessModules(hProcess, lModules(0), 1024 * 4, lRet) Then
sName = String$(MAX_PATH, vbNullChar)
GetModuleBaseName hProcess, lModules(0), sName, MAX_PATH
sName = Left$(sName, InStr(sName, vbNullChar) - 1)
If Len(sName) = Len(sProcess) Then
If sProcess = UCase$(sName) Then IsProcessRunning = True: Exit Function
End If
End If
End If
CloseHandle hProcess
Next N
End If
End Function
Private Sub Command1_Click()
Debug.Print IsProcessRunning("vb6.exe")
End Sub
(adapted from code at vbAccelerator)
Re: Check For Running Process
wow , your code look scary. are you sure it's faster ?
i suspect that my simple queury is much better :)
Re: Check For Running Process
Quote:
Originally Posted by Vntalk
wow , your code look scary. are you sure it's faster ?
yeah about 10 times faster
Quote:
Originally Posted by Vntalk
i suspect that my simple queury is much better :)
;)
Re: Check For Running Process
It is possible to get the code to read the processess started by other users?
EnumProcesses seems to return the expected amount of processes, but OpenProcess returns 0 for some of them so I can't get the name of the process.
Re: Check For Running Process
I use FindWindow() to identify whether processes are running. It's...much shorter code.
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Function WindowIsOpen(pstrWindow As String) As Boolean
WindowIsOpen = (FindWindow(vbNullString, pstrWindow) <> 0)
End Function
I use this in my maintenance program, which updates virus definitions, reboots, waits for the automatic background Ad-Aware startup scan finishes, then defrags the disk and runs a virus scan before shutting down the computer.
The Ad-Aware startup scan doesn't show up in the taskbar, so I looked up its process name in task manager and plopped it into the code. Works like a charm using the above helper function:
Code:
If WindowIsOpen("AAWTrayTitle") Then
StatusbarText "Waiting for Ad-Aware scan to finish...", True
Else
Re: Check For Running Process
Shoot, didn't realize this was a zombie thread. Sorry.
Re: Check For Running Process
I solved it in the end, used WTSEnumerateProcesses to do the same thing, but you get those processes running under other user sessions.
Re: Check For Running Process
Quote:
Originally Posted by Ellis Dee
Shoot, didn't realize this was a zombie thread. Sorry.
This is so off topic but what is a Zombie Thread?
I found a heap of references but no answers.
Re: Check For Running Process
i would believe it means old /dead thread
like this one
Re: Check For Running Process
Can someone tell me how to detect a process with only part of it's name ?
Ex : Let's say there is a hack program...but it as lots of versions, so it's name says hackprogramv2.1.3.exe for exemple for the version 2.1.3 and so on.
How can I only kill the process by finding only the "hackprogram" ( the only part that I need to find it ) ?
Re: Check For Running Process
change these lines, for a partial match
Quote:
If Len(sName) = Len(sProcess) Then
If sProcess = UCase$(sName)
maybe like
vb Code:
if left(sname, len(sprocess) = ucase(sprocess) then
remove exit function so it will continue to find the next match
i did not test this
note you could find some unwanted processes if your passed process name is too short
Re: Check For Running Process
I wasnt refering to that code, I was refering to the one Vntalk posted, but anyways, I changed some of the code and managed to make what I wanted possible ^^
Thanks for the help though :)
Re: Check For Running Process
Quote:
I wasnt refering to that code, I was refering to the one Vntalk posted,
sorry, not mind reader
Re: Check For Running Process
The code here provides a way to locate a specific active process ... but is there a vba command whereby I can kill a process if I find what I am seeking?
Re: Check For Running Process
search in codebank (vb6) for unlocker, there is a sample project there (by chris) that shows how to kill process
Re: Check For Running Process
I have check on WIN7 64
It can show only 32 bit apps that runs in taskmenager.
I have try with "CCC.exe" catalyst control centre sw, show is taskmenager that run, but not using this code...
Quote:
Originally Posted by
bushmobile
try this:
VB Code:
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" ( _
ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Declare Function EnumProcesses Lib "PSAPI.DLL" ( _
lpidProcess As Long, ByVal cb As Long, cbNeeded As Long) As Long
Private Declare Function EnumProcessModules Lib "PSAPI.DLL" ( _
ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, lpcbNeeded As Long) As Long
Private Declare Function GetModuleBaseName Lib "PSAPI.DLL" Alias "GetModuleBaseNameA" ( _
ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Const PROCESS_VM_READ = &H10
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Function IsProcessRunning(ByVal sProcess As String) As Boolean
Const MAX_PATH As Long = 260
Dim lProcesses() As Long, lModules() As Long, N As Long, lRet As Long, hProcess As Long
Dim sName As String
sProcess = UCase$(sProcess)
ReDim lProcesses(1023) As Long
If EnumProcesses(lProcesses(0), 1024 * 4, lRet) Then
For N = 0 To (lRet \ 4) - 1
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcesses(N))
If hProcess Then
ReDim lModules(1023)
If EnumProcessModules(hProcess, lModules(0), 1024 * 4, lRet) Then
sName = String$(MAX_PATH, vbNullChar)
GetModuleBaseName hProcess, lModules(0), sName, MAX_PATH
sName = Left$(sName, InStr(sName, vbNullChar) - 1)
If Len(sName) = Len(sProcess) Then
If sProcess = UCase$(sName) Then IsProcessRunning = True: Exit Function
End If
End If
End If
CloseHandle hProcess
Next N
End If
End Function
Private Sub Command1_Click()
Debug.Print IsProcessRunning("vb6.exe")
End Sub
(adapted from code at
vbAccelerator)