Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 03-09-2009
Member
 
Join Date: Jun 2009
Posts: 50
VB code to kill running processes

I am a VB developer, and I am looking for a simple way to kill any running process. I know this can be done using Windows Task Manager but I want it via codes. I want to create my own code rather then depending on some software or freeware to do the operation. Has anyone tried to build any code in VB that kills running applications?
Reply With Quote
  #2  
Old 03-09-2009
Member
 
Join Date: Nov 2008
Posts: 1,192
Re: VB code to kill running processes

Code:
Option Explicit
Const MAX& = 260

Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Declare Function ProcessFirst Lib "kernel32" Alias "Proc32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32" Alias "Proc32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhlp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Type LUID
   low As Long
   high As Long
End Type

Private Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    LuidUDT As LUID
    Attributes As Long
End Type

Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Declare Function GetVersion Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As Any, ReturnLength As Any) As Long

Type PROCESSENTRY32
  dwSize As Long
  cntUsage As Long
  th32ProcessID As Long
  th32DefaultHeapID As Long
  th32ModuleID As Long
  cntThreads As Long
  th32ParentProcessID As Long
  pcPriClassBase As Long
  dwFlags As Long
  szexeFile As String * MAX
End Type

Public Function KillApp(myName As String) As Boolean
   Const TH32CS_SNAPPROCESS As Long = 2&
   Const PROCESS_ALL_ACCESS = 0
   Dim myProcess As PROCESSENTRY32
   Dim rProcessFound As Long
   Dim hSnapshot As Long
   Dim szExename As String
   Dim exitCode As Long
   Dim myProcess As Long
   Dim AppKill As Boolean
   Dim appCount As Integer
   Dim i As Integer
   On Local Error GoTo Finish
   appCount = 0
   
   myProcess.dwSize = Len(myProcess)
   hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
   rProcessFound = ProcessFirst(hSnapshot, myProcess)
   Do While rProcessFound
       i = InStr(1, myProcess.szexeFile, Chr(0))
       szExename = LCase$(Left$(myProcess.szexeFile, i - 1))
       If Right$(szExename, Len(myName)) = LCase$(myName) Then
           KillApp = True
           appCount = appCount + 1
           myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, myProcess.th32ProcessID)
            If KillProcess(myProcess.th32ProcessID, 0) Then
                MsgBox "Instance no. " & appCount & " of " & szExename & " was terminated!"
            End If

       End If
       rProcessFound = ProcessNext(hSnapshot, myProcess)
   Loop
   Call CloseHandle(hSnapshot)
   Exit Function
Finish:
    MsgBox "Error!"
End Function

Function KillProcess(ByVal hProcessID As Long, Optional ByVal exitCode As Long) As Boolean
    Dim hToken As Long
    Dim hProcess As Long
    Dim tp As TOKEN_PRIVILEGES
   
    If GetVersion() >= 0 Then

        If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then
            GoTo CleanUp
        End If

        If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
            GoTo CleanUp
        End If

        tp.PrivilegeCount = 1
        tp.Attributes = SE_PRIVILEGE_ENABLED

        If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then
            GoTo CleanUp
        End If
    End If

    hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
    If hProcess Then

        KillProcess = (TerminateProcess(hProcess, exitCode) <> 0)
        CloseHandle hProcess
    End If
   
    If GetVersion() >= 0 Then
        tp.Attributes = 0
        AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
       
CleanUp:
        If hToken Then CloseHandle hToken
    End If
   
End Function

Private Sub cmdKill_Click()
    Dim pID As Long
    Dim i As Integer
    Dim strExe As String
    strExe = "Notepad.Exe"
    For i = 0 To 4
        pID = Shell(strExe, vbNormalFocus)
    Next i
    Debug.Assert False
    MsgBox "It is " & KillApp(strExe) & " that all instances of " & vbCrLf & strExe & " have been terminated!"
End Sub
Reply With Quote
  #3  
Old 03-09-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: VB code to kill running processes

Here is a simpler code:

Code:
Dim CurrentProcess As System.Diagnostics.Process = Process.GetProcessesByName("explorer.exe")(0)
CurrentProcess.Kill()
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "VB code to kill running processes"
Thread Thread Starter Forum Replies Last Post
Is it safe to kill all background processes in ICS devices Gautama-Gopa Portable Devices 3 22-08-2012 04:22 PM
how can i create a Batch file to kill some processes by name Hansel Ortiz -[BrEcHaWarr] Windows Server Help 7 07-06-2012 11:24 AM
How many processes do you have running Langward Overclocking & Computer Modification 4 21-03-2010 01:38 AM
Task Manager CANNOT KILL PROCESSES j90qfj90q34fjwefi2 Vista Help 12 31-01-2010 03:24 AM
Kill Windows Explorer Processes Easily in Windows Vista hatred Tips & Tweaks 1 11-02-2009 10:32 PM


All times are GMT +5.5. The time now is 12:38 AM.