Results 1 to 15 of 15

Thread: How can you terminate a process by its name?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2002
    Posts
    628

    How can you terminate a process by its name?

    anyone know?

    such as ..
    vb6.exe
    explorer.exe
    svchost.exe

    etc

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Here goes, noobie

    VB Code:
    1. Public Function KillAppByName(MyName As String) As Boolean 'kills applications by name
    2.  
    3.     Const PROCESS_ALL_ACCESS = 0
    4.     Dim uProcess As PROCESSENTRY32
    5.     Dim rProcessFound As Long
    6.     Dim hSnapshot As Long
    7.     Dim szExename As String
    8.     Dim exitCode As Long
    9.     Dim AppKill As Long
    10.     Dim i As Integer
    11.     Dim lProcHnd As Long
    12.     Dim hWnd As Long
    13.    
    14.     On Local Error GoTo ErrTrap
    15.    
    16.     If winVersion = "WNT3" Or winVersion = "WNT4" Then Exit Function
    17.    
    18.     Const TH32CS_SNAPPROCESS As Long = 2&
    19.    
    20.     uProcess.dwSize = Len(uProcess)
    21.     hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    22.     rProcessFound = ProcessFirst(hSnapshot, uProcess)
    23.    
    24.     Do While rProcessFound
    25.         i = InStr(1, uProcess.szexeFile, Chr(0))
    26.         szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    27.         If Right$(szExename, Len(MyName)) = LCase$(MyName) Then
    28.        
    29.             lProcHnd = OpenProcess(PROCESS_TERMINATE, 0&, uProcess.th32ProcessID)
    30.             AppKill = TerminateProcess(lProcHnd, exitCode)
    31.             Call CloseHandle(lProcHnd)
    32.            
    33.         End If
    34.         rProcessFound = ProcessNext(hSnapshot, uProcess)
    35.     Loop
    36.  
    37.     Call CloseHandle(hSnapshot)
    38.    
    39.     Exit Function
    40.    
    41. End Function

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Declarations:

    Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long

    Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long

    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

    Private Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)

    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

    Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long

    Private Const PROCESS_TERMINATE = &H1
    Private Const SC_CLOSE = &HF060&
    Private Const WM_SYSCOMMAND = &H112
    Private Const PROCESS_ALL_ACCESS = &H1F0FFF

  4. #4
    Lively Member Algar's Avatar
    Join Date
    Jun 2003
    Location
    A place that never existed
    Posts
    127

    Re: How can you terminate a process by its name?

    Originally posted by n00bie
    anyone know?

    such as ..
    vb6.exe
    explorer.exe
    svchost.exe

    etc
    First off, you'll need to know the Apps title. Use the FindWindow API to get the handle of the app. Next, PostMessage API to the app WM_CLOSE. Both of these APIs and examples of how to use them are at www.allapi.net.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    My function simply needs the name of the program, such as word.exe,...

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2002
    Posts
    628
    doesnt work (im on xp)

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2002
    Posts
    628
    bumpy!

  8. #8
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    I thought I'd posted this before, but couldn't find it, anyway...

    Try the following in a Standard Code Module:
    VB Code:
    1. Private Type PROCESSENTRY32
    2.   dwSize As Long
    3.   cntUsage As Long
    4.   th32ProcessID As Long
    5.   th32DefaultHeapID As Long
    6.   th32ModuleID As Long
    7.   cntThreads As Long
    8.   th32ParentProcessID As Long
    9.   pcPriClassBase As Long
    10.   dwFlags As Long
    11.   szExeFile As String * 260
    12. End Type
    13.  
    14. Private Type OSVERSIONINFO
    15.   dwOSVersionInfoSize As Long
    16.   dwMajorVersion As Long
    17.   dwMinorVersion As Long
    18.   dwBuildNumber As Long
    19.   dwPlatformId As Long
    20.   szCSDVersion As String * 128
    21. End Type
    22.  
    23. Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    24. Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    25. Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
    26. Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    27. Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    28. 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
    29. 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
    30. Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    31. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
    32. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    33.  
    34. Private Const PROCESS_TERMINATE = &H1
    35. Private Const VER_PLATFORM_WIN32_WINDOWS = 1
    36. Private Const PROCESS_QUERY_INFORMATION = 1024
    37. Private Const PROCESS_VM_READ = 16
    38. Private Const TH32CS_SNAPPROCESS = &H2
    39.  
    40. Private Function CheckVersion() As Long
    41.   Dim tOS As OSVERSIONINFO
    42.   tOS.dwOSVersionInfoSize = Len(tOS)
    43.   Call GetVersionEx(tOS)
    44.   CheckVersion = tOS.dwPlatformId
    45. End Function
    46.  
    47. Public Function GetEXEProcessID(ByVal sEXE As String) As Long
    48.   Dim aPID() As Long
    49.   Dim lProcesses As Long
    50.   Dim lProcess As Long
    51.   Dim lModule As Long
    52.   Dim sName As String
    53.   Dim iIndex As Integer
    54.   Dim bCopied As Long
    55.   Dim lSnapShot As Long
    56.   Dim tPE As PROCESSENTRY32
    57.   Dim bDone As Boolean
    58.  
    59.   If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
    60.     'Windows 9x
    61.     'Create a SnapShot of the Currently Running Processes
    62.     lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    63.     If lSnapShot < 0 Then Exit Function
    64.     tPE.dwSize = Len(tPE)
    65.     'Buffer the First Processes Info..
    66.     bCopied = Process32First(lSnapShot, tPE)
    67.     Do While bCopied
    68.       'While there are Processes List them..
    69.       sName = Left$(tPE.szExeFile, InStr(tPE.szExeFile, Chr(0)) - 1)
    70.       sName = Mid(sName, InStrRev(sName, "\") + 1)
    71.       If InStr(sName, Chr(0)) Then
    72.         sName = Left(sName, InStr(sName, Chr(0)) - 1)
    73.       End If
    74.       bCopied = Process32Next(lSnapShot, tPE)
    75.       If StrComp(sEXE, sName, vbTextCompare) = 0 Then
    76.         GetEXEProcessID = tPE.th32ProcessID
    77.         Exit Do
    78.       End If
    79.     Loop
    80.    
    81.   Else
    82.     'Windows NT
    83.     'The EnumProcesses Function doesn't indicate how many Process there are,
    84.     'so you need to pass a large array and trim off the empty elements
    85.     'as cbNeeded will return the no. of Processes copied.
    86.     ReDim aPID(255)
    87.     Call EnumProcesses(aPID(0), 1024, lProcesses)
    88.     lProcesses = lProcesses / 4
    89.     ReDim Preserve aPID(lProcesses)
    90.    
    91.     For iIndex = 0 To lProcesses - 1
    92.       'Get the Process Handle, by Opening the Process
    93.       lProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, aPID(iIndex))
    94.       If lProcess Then
    95.         'Just get the First Module, all we need is the Handle to get
    96.         'the Filename..
    97.         If EnumProcessModules(lProcess, lModule, 4, 0&) Then
    98.           sName = Space(260)
    99.           Call GetModuleFileNameExA(lProcess, lModule, sName, Len(sName))
    100.           If InStr(sName, "\") > 0 Then
    101.             sName = Mid(sName, InStrRev(sName, "\") + 1)
    102.           End If
    103.           If InStr(sName, Chr(0)) Then
    104.             sName = Left(sName, InStr(sName, Chr(0)) - 1)
    105.           End If
    106.           If StrComp(sEXE, sName, vbTextCompare) = 0 Then
    107.             GetEXEProcessID = aPID(iIndex)
    108.             bDone = True
    109.           End If
    110.         End If
    111.         'Close the Process Handle
    112.         lRet = CloseHandle(lProcess)
    113.         If bDone Then Exit For
    114.       End If
    115.     Next
    116.   End If
    117. End Function
    118.  
    119. Public Function TerminateEXE(ByVal sEXE As String) As Boolean
    120.   Dim lPID As Long
    121.   Dim lProcess As Long
    122.  
    123.   lPID = GetEXEProcessID(sEXE)
    124.   If lPID = 0 Then Exit Function
    125.   lProcess = OpenProcess(PROCESS_TERMINATE, 0, lPID)
    126.   Call TerminateProcess(lProcess, 0&)
    127.   Call CloseHandle(lProcess)
    128.  
    129.   TerminateEXE = True
    130. End Function
    Example Usage:
    VB Code:
    1. TerminateEXE "Notepad.exe"

  9. #9
    Fanatic Member
    Join Date
    Feb 2003
    Location
    C:\Windows\Microsoft.NET\Framework
    Posts
    574
    Both our friends, Davos and Aaron Young, have written code that will do the trick, but they are for different platforms. While Davos's code was for Win9x platforms, Aron's code is only for NT based platforms and will not work on Win 9x since Win 9x platforms do not support PSAPI.dll (Process Status API). However, Davos's code will work on both Win 9x as well as Win 2000.

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    218
    Thanks for this. I did not realise.

  11. #11
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    The code I posted will work on both Win9x and NT based platforms.

  12. #12

  13. #13
    Hyperactive Member Steve Stunning's Avatar
    Join Date
    Jul 1999
    Location
    Fairfax, Virginia
    Posts
    314
    Thanks Aaron Young,


    Works great!
    Steve Stunning

  14. #14
    Member
    Join Date
    Nov 1999
    Location
    Kansas, USA
    Posts
    58
    When I try this code on Windows 98, it causes a crash on the line:

    lProcess = OpenProcess(PROCESS_TERMINATE, 0, lPID)

    or possibly the next line. The process does not terminate. I'm using this on an ActiveX EXE btw - need to close it during an install. Works great on WinXP though. Any ideas?

  15. #15
    Member
    Join Date
    Dec 2004
    Location
    California
    Posts
    39

    Re: How can you terminate a process by its name?

    The documentation of TerminateProcess says:
    Use it only in extreme circumstances.
    The terms terminate and kill should be used carefully; they usually imply unconditional and immediate removal from the system, as is done by TerminateProcess, which should be avoided. Many problems can be caused by TerminateProcess, including loss of data.

    Any time someone says they want to terminate a process but they are not clear about the necessity to actually terminate a process, I ask for clarification.

    The first solution that should be tried/suggested is to simply send a WM_CLOSE message to the application's main window. The WM_CLOSE message is essentially the equivalent to clicking the "X" at the top right of the window.

    Use of TerminateProcess should be the last resort. It should never be used if there is another way to get the application to end.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width