Results 1 to 2 of 2

Thread: VB - Find and kill running applications

  1. #1

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    VB - Find and kill running applications

    This code finds and kills all versions of a named application (NotePad in this example).

    I'll leave it to the moderators if it should be allowed here. I searched the forums for this and got a lot of hits but as a beginner programmer I didn't see a thread with an example of code that worked the very first time for someone and also worked on Windows 2000. By that I mean some of the responses said how to do it without code examples and some of the threads the posters had problems and the thread ended before they were resolved. This worked "right out of the box" for me.

    It works by creating five versions of a NotePad.exe and then displays a message as each one is killed. When it is done it displays a sucessful or not sucessful message.

    Note: The line with the blinking eye smilies should be:
    (hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)

    Create a form with a cmd button called Command1. In it's click event add:

    VB Code:
    1. Dim pID As Long
    2.     Dim i As Integer
    3.     Dim strExe As String
    4.     strExe = "Notepad.Exe"
    5.     For i = 0 To 4
    6.         pID = Shell(strExe, vbNormalFocus)
    7.     Next i
    8.     'Five instances of notpade.exe is now created
    9.     Debug.Assert False
    10.     MsgBox "It is " & _
    11.         KillApp(strExe) & _
    12.         " that all instances of " & vbCrLf & _
    13.         strExe & _
    14.         " have been terminated!"

    Add a module called modKillApp and copy in this code:

    VB Code:
    1. Option Explicit
    2. Const MAX_PATH& = 260
    3.  
    4. Declare Function TerminateProcess _
    5.     Lib "kernel32" (ByVal ApphProcess As Long, _
    6.     ByVal uExitCode As Long) As Long
    7. Declare Function OpenProcess Lib _
    8.     "kernel32" (ByVal dwDesiredAccess As Long, _
    9.     ByVal blnheritHandle As Long, _
    10.     ByVal dwAppProcessId As Long) As Long
    11. Declare Function ProcessFirst _
    12.     Lib "kernel32" Alias "Process32First" _
    13.     (ByVal hSnapshot As Long, _
    14.     uProcess As PROCESSENTRY32) As Long
    15. Declare Function ProcessNext _
    16.     Lib "kernel32" Alias "Process32Next" _
    17.     (ByVal hSnapshot As Long, _
    18.     uProcess As PROCESSENTRY32) As Long
    19. Declare Function CreateToolhelpSnapshot _
    20.     Lib "kernel32" Alias "CreateToolhelp32Snapshot" _
    21.     (ByVal lFlags As Long, _
    22.     lProcessID As Long) As Long
    23. Declare Function CloseHandle _
    24.     Lib "kernel32" (ByVal hObject As Long) As Long
    25.  
    26. Private Type LUID
    27.    lowpart As Long
    28.    highpart As Long
    29. End Type
    30.  
    31. Private Type TOKEN_PRIVILEGES
    32.     PrivilegeCount As Long
    33.     LuidUDT As LUID
    34.     Attributes As Long
    35. End Type
    36.  
    37. Const TOKEN_ADJUST_PRIVILEGES = &H20
    38. Const TOKEN_QUERY = &H8
    39. Const SE_PRIVILEGE_ENABLED = &H2
    40. Const PROCESS_ALL_ACCESS = &H1F0FFF
    41.  
    42. Private Declare Function GetVersion _
    43.     Lib "kernel32" () As Long
    44. Private Declare Function GetCurrentProcess _
    45.     Lib "kernel32" () As Long
    46. Private Declare Function OpenProcessToken _
    47.     Lib "advapi32" (ByVal ProcessHandle As Long, _
    48.     ByVal DesiredAccess As Long, _
    49.     TokenHandle As Long) As Long
    50. Private Declare Function LookupPrivilegeValue _
    51.     Lib "advapi32" Alias "LookupPrivilegeValueA" _
    52.     (ByVal lpSystemName As String, _
    53.     ByVal lpName As String, _
    54.     lpLuid As LUID) As Long
    55. Private Declare Function AdjustTokenPrivileges _
    56.     Lib "advapi32" (ByVal TokenHandle As Long, _
    57.     ByVal DisableAllPrivileges As Long, _
    58.     NewState As TOKEN_PRIVILEGES, _
    59.     ByVal BufferLength As Long, _
    60.     PreviousState As Any, _
    61.     ReturnLength As Any) As Long
    62.  
    63. Type PROCESSENTRY32
    64.   dwSize As Long
    65.   cntUsage As Long
    66.   th32ProcessID As Long
    67.   th32DefaultHeapID As Long
    68.   th32ModuleID As Long
    69.   cntThreads As Long
    70.   th32ParentProcessID As Long
    71.   pcPriClassBase As Long
    72.   dwFlags As Long
    73.   szexeFile As String * MAX_PATH
    74. End Type
    75. '---------------------------------------
    76. Public Function KillApp(myName As String) As Boolean
    77.    Const TH32CS_SNAPPROCESS As Long = 2&
    78.    Const PROCESS_ALL_ACCESS = 0
    79.    Dim uProcess As PROCESSENTRY32
    80.    Dim rProcessFound As Long
    81.    Dim hSnapshot As Long
    82.    Dim szExename As String
    83.    Dim exitCode As Long
    84.    Dim myProcess As Long
    85.    Dim AppKill As Boolean
    86.    Dim appCount As Integer
    87.    Dim i As Integer
    88.    On Local Error GoTo Finish
    89.    appCount = 0
    90.    
    91.    uProcess.dwSize = Len(uProcess)
    92.    hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    93.    rProcessFound = ProcessFirst(hSnapshot, uProcess)
    94.    Do While rProcessFound
    95.        i = InStr(1, uProcess.szexeFile, Chr(0))
    96.        szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    97.        If Right$(szExename, Len(myName)) = LCase$(myName) Then
    98.            KillApp = True
    99.            appCount = appCount + 1
    100.            myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    101.             If KillProcess(uProcess.th32ProcessID, 0) Then
    102.                 'For debug.... Remove this
    103.                 MsgBox "Instance no. " & appCount & " of " & szExename & " was terminated!"
    104.             End If
    105.  
    106.        End If
    107.        rProcessFound = ProcessNext(hSnapshot, uProcess)
    108.    Loop
    109.    Call CloseHandle(hSnapshot)
    110.    Exit Function
    111. Finish:
    112.     MsgBox "Error!"
    113. End Function
    114.  
    115. 'Terminate any application and return an exit code to Windows.
    116. Function KillProcess(ByVal hProcessID As Long, Optional ByVal exitCode As Long) As Boolean
    117.     Dim hToken As Long
    118.     Dim hProcess As Long
    119.     Dim tp As TOKEN_PRIVILEGES
    120.    
    121.  
    122.     If GetVersion() >= 0 Then
    123.  
    124.         If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then
    125.             GoTo CleanUp
    126.         End If
    127.  
    128.         If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
    129.             GoTo CleanUp
    130.         End If
    131.  
    132.         tp.PrivilegeCount = 1
    133.         tp.Attributes = SE_PRIVILEGE_ENABLED
    134.  
    135.         If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then
    136.             GoTo CleanUp
    137.         End If
    138.     End If
    139.  
    140.     hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
    141.     If hProcess Then
    142.  
    143.         KillProcess = (TerminateProcess(hProcess, exitCode) <> 0)
    144.         ' close the process handle
    145.         CloseHandle hProcess
    146.     End If
    147.    
    148.     If GetVersion() >= 0 Then
    149.         ' under NT restore original privileges
    150.         tp.Attributes = 0
    151.         AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
    152.        
    153. CleanUp:
    154.         If hToken Then CloseHandle hToken
    155.     End If
    156.    
    157. End Function

    Then just run it.
    Last edited by TysonLPrice; May 28th, 2004 at 12:43 PM.

  2. #2
    New Member
    Join Date
    Jul 2010
    Posts
    1

    Re: VB - Find and kill running applications

    works just fine.
    thank you

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