Visual Basic 6 function "KillProcess"

Go back

Below you'll find the source for the Visual Basic 6 function KillProcess.

Attribute VB_Name = "modKillProcess"
' This function is downloaded from:
' You may freely distribute this file but please leave all comments, including this one, in it.
' @Author Stefan Thoolen <>

Option Explicit

Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

' Closes a process
' @param    long    processId       The ID of the process that needs to be closed
' @param    long    ExitCode        Optional, sends this value to the process that's getting killed
' @return   boolean                 True if the process is succesfully terminated, false if not
' @author   Stefan Thoolen <>
Public Function KillProcess(processId As Long, Optional ByVal ExitCode As Long = 0) As Boolean
    Dim handle As Long, l As Long
    handle = OpenProcess(&H1, False, processId)
    If handle = 0 Then Exit Function
    l = TerminateProcess(handle, ExitCode)
    CloseHandle handle
    If l = 0 Then Exit Function
    KillProcess = True
End Function