Enumerate Processes and Terminate Selected 
Author Message
 Enumerate Processes and Terminate Selected

Hello everyone,

I want to enumerate open processes in a listview and terminate the selected
process. I found a code which can enumerate but termination doesnt work.

any input is welcome, thanks!

here is the code
' Enumerate open processes
Private Sub EnumProcess(Optional ByVal sExeName As String = "")
    Dim lSnapShot As Long
    Dim lNextProcess As Long
    Dim tPE As PROCESSENTRY32

    ' Create snapshot
    lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
    If lSnapShot <> -1 Then
        ' Clear list
        lstProcess.Clear
        ' Length of the structure
        tPE.dwSize = Len(tPE)

        ' Find first process
        lNextProcess = Process32First(lSnapShot, tPE)
        Do While lNextProcess
            ' Found specified process
            If sExeName = Left$(tPE.szExeFile, Len(sExeName)) And
Len(sExeName) > 0 Then
                Dim lProcess As Long
                Dim lExitCode As Long
                ' Open process
                lProcess = OpenProcess(0, False, tPE.th32ProcessID)
                ' Terminate process
                TerminateProcess lProcess, lExitCode
                ' Close handle
                CloseHandle lProcess
            Else
                ' Add exe to list
                lstProcess.AddItem tPE.szExeFile
            End If
            ' Get next process
            lNextProcess = Process32Next(lSnapShot, tPE)
        Loop

        ' Close handle
        CloseHandle (lSnapShot)

    Else
        lstProcess.AddItem "Cannot enumerate running process!"
    End If
End Sub



Sat, 05 Nov 2011 01:26:07 GMT  
 Enumerate Processes and Terminate Selected
Look at Process Monitor examples on PSC (Planet Source Code).
http://www.planet-source-code.com/vb/default.asp?lngWId=1
They work.
I have used some of that code in a working app.
Quote:

> Hello everyone,

> I want to enumerate open processes in a listview and terminate the selected
> process. I found a code which can enumerate but termination doesnt work.

> any input is welcome, thanks!

> here is the code
> ' Enumerate open processes
> Private Sub EnumProcess(Optional ByVal sExeName As String = "")
>     Dim lSnapShot As Long
>     Dim lNextProcess As Long
>     Dim tPE As PROCESSENTRY32

>     ' Create snapshot
>     lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
>     If lSnapShot <> -1 Then
>         ' Clear list
>         lstProcess.Clear
>         ' Length of the structure
>         tPE.dwSize = Len(tPE)

>         ' Find first process
>         lNextProcess = Process32First(lSnapShot, tPE)
>         Do While lNextProcess
>             ' Found specified process
>             If sExeName = Left$(tPE.szExeFile, Len(sExeName)) And
> Len(sExeName) > 0 Then
>                 Dim lProcess As Long
>                 Dim lExitCode As Long
>                 ' Open process
>                 lProcess = OpenProcess(0, False, tPE.th32ProcessID)
>                 ' Terminate process
>                 TerminateProcess lProcess, lExitCode
>                 ' Close handle
>                 CloseHandle lProcess
>             Else
>                 ' Add exe to list
>                 lstProcess.AddItem tPE.szExeFile
>             End If
>             ' Get next process
>             lNextProcess = Process32Next(lSnapShot, tPE)
>         Loop

>         ' Close handle
>         CloseHandle (lSnapShot)

>     Else
>         lstProcess.AddItem "Cannot enumerate running process!"
>     End If
> End Sub



Sat, 05 Nov 2011 01:52:06 GMT  
 Enumerate Processes and Terminate Selected

Quote:
>                ' Open process
>                lProcess = OpenProcess(0, False, tPE.th32ProcessID)
>                ' Terminate process
>                TerminateProcess lProcess, lExitCode

You didn't request any access when calling OpenProcess, so TerminateProcess
would have failed with error access denied. Use PROCESS_TERMINATE in the
call to OpenProcess.


Sat, 05 Nov 2011 02:01:05 GMT  
 Enumerate Processes and Terminate Selected
Its working now,

here is the fix

Const PROCESS_TERMINATE As Long = (&H1)
'Open process
lProcess = OpenProcess(PROCESS_TERMINATE, False, tPE.th32ProcessID)

Thanks a lot, god bless u



| >                ' Open process
| >                lProcess = OpenProcess(0, False, tPE.th32ProcessID)
| >                ' Terminate process
| >                TerminateProcess lProcess, lExitCode
|
| You didn't request any access when calling OpenProcess, so
TerminateProcess
| would have failed with error access denied. Use PROCESS_TERMINATE in the
| call to OpenProcess.
|
|



Sat, 05 Nov 2011 07:29:51 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Can Terminate a process by process ID

2. terminate process VB5

3. Terminate Process?

4. TERMINATE PROCESS

5. Terminating only 1 window of a process

6. Terminate Process by Window Handle

7. Q: Terminate Process

8. terminate a process

9. Terminating Excel Process

10. Terminating Processes

11. How to Determine When a Shelled Process has Terminated

12. Help: Forceably terminate a Shell-initiated process?

 

 
Powered by phpBB® Forum Software