-
Jan 13th, 2007, 10:22 AM
#1
Thread Starter
Addicted Member
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
-
Jan 13th, 2007, 11:17 AM
#2
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)
Last edited by bushmobile; Feb 4th, 2007 at 05:41 AM.
Reason: added CloseHandle
-
Jan 13th, 2007, 11:49 AM
#3
Thread Starter
Addicted Member
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
Last edited by Vntalk; Jan 13th, 2007 at 12:00 PM.
-
Jan 13th, 2007, 12:09 PM
#4
Re: Check For Running Process
Originally Posted by Vntalk
wow , your code look scary. are you sure it's faster ?
yeah about 10 times faster
Originally Posted by Vntalk
i suspect that my simple queury is much better
-
May 18th, 2008, 05:12 AM
#5
New Member
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.
-
May 18th, 2008, 05:04 PM
#6
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
-
May 18th, 2008, 05:05 PM
#7
Re: Check For Running Process
Shoot, didn't realize this was a zombie thread. Sorry.
-
May 19th, 2008, 09:06 AM
#8
New Member
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.
-
Feb 26th, 2009, 04:35 AM
#9
Junior Member
Re: Check For Running Process
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.
-
Feb 26th, 2009, 04:38 AM
#10
Re: Check For Running Process
i would believe it means old /dead thread
like this one
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 8th, 2011, 01:38 PM
#11
New Member
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 ) ?
-
Mar 8th, 2011, 03:32 PM
#12
Re: Check For Running Process
change these lines, for a partial match
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 8th, 2011, 06:20 PM
#13
New Member
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
-
Mar 9th, 2011, 03:23 AM
#14
Re: Check For Running Process
I wasnt refering to that code, I was refering to the one Vntalk posted,
sorry, not mind reader
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 24th, 2011, 01:41 PM
#15
New Member
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?
-
Mar 24th, 2011, 03:45 PM
#16
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Jun 22nd, 2011, 08:53 PM
#17
Hyperactive Member
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...
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|