PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Check For Running Process-VBForums
Results 1 to 17 of 17

Thread: Check For Running Process

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    Check For Running Process

    Hi guy, i use this code to check if an aplication is still running

    VB Code:
    1. Public Function isRunning(ByVal Process As String) As Boolean
    2.     Dim objWMIService, colProcesses
    3.     Set objWMIService = GetObject("winmgmts:")
    4.     Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process where name='" & Process & "'")
    5.     If colProcesses.Count Then
    6.     isRunning = True
    7.     Else
    8.     isRunning = False
    9.     End If
    10. 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

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Check For Running Process

    try this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function OpenProcess Lib "kernel32" ( _
    4.     ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    5.    
    6. Private Declare Function CloseHandle Lib "kernel32" ( _
    7.     ByVal hObject As Long) As Long
    8.  
    9. Private Declare Function EnumProcesses Lib "PSAPI.DLL" ( _
    10.    lpidProcess As Long, ByVal cb As Long, cbNeeded As Long) As Long
    11.  
    12. Private Declare Function EnumProcessModules Lib "PSAPI.DLL" ( _
    13.     ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, lpcbNeeded As Long) As Long
    14.  
    15. Private Declare Function GetModuleBaseName Lib "PSAPI.DLL" Alias "GetModuleBaseNameA" ( _
    16.     ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
    17.  
    18. Private Const PROCESS_VM_READ = &H10
    19. Private Const PROCESS_QUERY_INFORMATION = &H400
    20.  
    21. Private Function IsProcessRunning(ByVal sProcess As String) As Boolean
    22.     Const MAX_PATH As Long = 260
    23.     Dim lProcesses() As Long, lModules() As Long, N As Long, lRet As Long, hProcess As Long
    24.     Dim sName As String
    25.    
    26.     sProcess = UCase$(sProcess)
    27.    
    28.     ReDim lProcesses(1023) As Long
    29.     If EnumProcesses(lProcesses(0), 1024 * 4, lRet) Then
    30.         For N = 0 To (lRet \ 4) - 1
    31.             hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcesses(N))
    32.             If hProcess Then
    33.                 ReDim lModules(1023)
    34.                 If EnumProcessModules(hProcess, lModules(0), 1024 * 4, lRet) Then
    35.                     sName = String$(MAX_PATH, vbNullChar)
    36.                     GetModuleBaseName hProcess, lModules(0), sName, MAX_PATH
    37.                     sName = Left$(sName, InStr(sName, vbNullChar) - 1)
    38.                     If Len(sName) = Len(sProcess) Then
    39.                         If sProcess = UCase$(sName) Then IsProcessRunning = True: Exit Function
    40.                     End If
    41.                 End If
    42.             End If
    43.             CloseHandle hProcess
    44.         Next N
    45.     End If
    46. End Function
    47.  
    48. Private Sub Command1_Click()
    49.     Debug.Print IsProcessRunning("vb6.exe")
    50. End Sub
    (adapted from code at vbAccelerator)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    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.

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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

  5. #5
    New Member
    Join Date
    May 2008
    Posts
    2

    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.

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,527

    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

  7. #7
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,527

    Re: Check For Running Process

    Shoot, didn't realize this was a zombie thread. Sorry.

  8. #8
    New Member
    Join Date
    May 2008
    Posts
    2

    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.

  9. #9
    Junior Member
    Join Date
    Feb 2009
    Posts
    27

    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.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,303

    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

  11. #11
    New Member
    Join Date
    Mar 2011
    Posts
    6

    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 ) ?

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,303

    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:
    1. 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

  13. #13
    New Member
    Join Date
    Mar 2011
    Posts
    6

    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

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,303

    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

  15. #15
    New Member
    Join Date
    Mar 2011
    Posts
    1

    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?

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,303

    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

  17. #17
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    282

    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 View Post
    try this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function OpenProcess Lib "kernel32" ( _
    4.     ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    5.    
    6. Private Declare Function CloseHandle Lib "kernel32" ( _
    7.     ByVal hObject As Long) As Long
    8.  
    9. Private Declare Function EnumProcesses Lib "PSAPI.DLL" ( _
    10.    lpidProcess As Long, ByVal cb As Long, cbNeeded As Long) As Long
    11.  
    12. Private Declare Function EnumProcessModules Lib "PSAPI.DLL" ( _
    13.     ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, lpcbNeeded As Long) As Long
    14.  
    15. Private Declare Function GetModuleBaseName Lib "PSAPI.DLL" Alias "GetModuleBaseNameA" ( _
    16.     ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
    17.  
    18. Private Const PROCESS_VM_READ = &H10
    19. Private Const PROCESS_QUERY_INFORMATION = &H400
    20.  
    21. Private Function IsProcessRunning(ByVal sProcess As String) As Boolean
    22.     Const MAX_PATH As Long = 260
    23.     Dim lProcesses() As Long, lModules() As Long, N As Long, lRet As Long, hProcess As Long
    24.     Dim sName As String
    25.    
    26.     sProcess = UCase$(sProcess)
    27.    
    28.     ReDim lProcesses(1023) As Long
    29.     If EnumProcesses(lProcesses(0), 1024 * 4, lRet) Then
    30.         For N = 0 To (lRet \ 4) - 1
    31.             hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcesses(N))
    32.             If hProcess Then
    33.                 ReDim lModules(1023)
    34.                 If EnumProcessModules(hProcess, lModules(0), 1024 * 4, lRet) Then
    35.                     sName = String$(MAX_PATH, vbNullChar)
    36.                     GetModuleBaseName hProcess, lModules(0), sName, MAX_PATH
    37.                     sName = Left$(sName, InStr(sName, vbNullChar) - 1)
    38.                     If Len(sName) = Len(sProcess) Then
    39.                         If sProcess = UCase$(sName) Then IsProcessRunning = True: Exit Function
    40.                     End If
    41.                 End If
    42.             End If
    43.             CloseHandle hProcess
    44.         Next N
    45.     End If
    46. End Function
    47.  
    48. Private Sub Command1_Click()
    49.     Debug.Print IsProcessRunning("vb6.exe")
    50. 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
  •  



Featured


Click Here to Expand Forum to Full Width