Link to home
Start Free TrialLog in
Avatar of RichW
RichWFlag for United States of America

asked on

Terminate Processes

Hi All,

I am able to get a list of running processes, but I am having trouble terminating targeted processes.

I have a list of the running processes, and I want to terminate a process according to the process name.

For example, if explorer.exe is running and iexplore.exe is running with a bunch of other processes, what I want is to target explorer.exe and iexplorer.exe and end those processes without ending the others in the list.

I have tried the TerminateProcess function and I cannot get it to work.

If you have a function that will work by my passing a process name to it I would greatly appreciate it.

For example:  Call EndProcess("explorer.exe")

Thanks

RichW

Avatar of vadim63
vadim63
Flag of United States of America image

Avatar of zzzzzooc
zzzzzooc

Try the below. The type of enumeration used will work on NT-based systems (2k, XP, etc.).


Form1:
------------
Option Explicit

Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Private Const PROCESS_TERMINATE = &H1
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16
Private Sub Form_Load()
    Dim intCnt As Integer
    intCnt = KillProcByName("\calc.exe")
    Call MsgBox("Program was terminated " & intCnt & " time(s).")
End Sub
Private Function KillProcByName(ByVal strProcName As String) As Integer
    Dim lngRet As Long
    Dim lngCb As Long, lngCbNeeded As Long, lngCbNeeded2 As Long
    Dim intLoop As Integer
    Dim lngProc As Long, lngProcIDs() As Long
    Dim lngModules(0) As Long, strModule As String
    lngCb = 8
    lngCbNeeded = 96
    Do While lngCb <= lngCbNeeded
        lngCb = lngCb * 2
        ReDim lngProcIDs(lngCb / 4) As Long
        lngRet = EnumProcesses(lngProcIDs(0), lngCb, lngCbNeeded)
    Loop
    For intLoop = 1 To (lngCbNeeded / 4)
        lngProc = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ Or PROCESS_TERMINATE, 0, lngProcIDs(intLoop))
        If lngProc <> 0 Then
            lngRet = EnumProcessModules(lngProc, lngModules(0), 1, lngCbNeeded2)
            If lngRet <> 0 Then
                strModule = Space(256)
                Call GetModuleFileNameExA(lngProc, lngModules(0), strModule, Len(strModule))
                strModule = Left(strModule, InStr(1, strModule, vbNullChar) - 1)
                If StrComp(Right(strModule, Len(strProcName)), strProcName, vbTextCompare) = 0 Then
                    Call TerminateProcess(lngProc, 0)
                    KillProcByName = KillProcByName + 1
                End If
            End If
        End If
        Call CloseHandle(lngProc)
    Next intLoop
End Function
ASKER CERTIFIED SOLUTION
Avatar of aelatik
aelatik
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RichW

ASKER

vadim63, I already tried that and could not get it to work.  It doesn't find the processes I'm trying to close.

zzzzzooc, your's didn't work either.  I tried the following:

Dim intCnt As Integer
   intCnt = KillProcByName("\explorer.exe")

And it did not terminate Windows Explorer, or any other process name placed in there.


aelatik, your's works on simple processes like Windows Explorer, but not on all processes, like lexbces.exe, which is a printer process I have running.

What OS and privileges do you have while trying to run these?

>>which is a printer process I have running.
If it's a service, you'll probably have to terminate it using a different method.
Avatar of RichW

ASKER

aelatik's answer was the only one that I could get to work.

zzzzzooc, I still could not get your code to close even Windows Explorer, but I appreciate your trying to help.

Thanks to all!

RichW
Use the OpenProcess API to open  a handle on the process with PROCESS_ALL_ACCESS, and then use TerminateProcess on its process ID  (with exit code 0), and then close the handle.