Kill a system process from Visual Basic 6

I have this code from another question and it works properly when I try to execute a program or process but I wanna know how to kill it, or kill some process if I knew the name of the .exe.

Option Explicit

Declare Function OpenProcess Lib "kernel32" (ByVal dwAccess As Long, ByVal fInherit As Integer, ByVal hObject As Long) As Long
Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Declare Function GetTickCount Lib "kernel32" () As Long

Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)


Option Explicit
Dim pID&
Dim pHandle&

Public Sub APISleep(interval&)
  sT = GetTickCount
     pT = GetTickCount
       If pT - sT > interval Then
           Exit Do
        End If
End Sub

Private Sub Command1_Click()
Dim tmp&
pID = Shell("calc.exe", 1)
pHandle = OpenProcess(&H100000, True, pID)
APISleep 5000
tmp = TerminateProcess(pHandle, 0)
End Sub

Private Sub Command2_Click()
ExitProcess pHandle
End Sub

The function ExitProcess ends my form1 and VisualBasic and TerminateProcess it doesnt work...

Someone can help me?
Who is Participating?
tomlanderConnect With a Mentor Commented:
The following will kill a process passed into it:

'First, put these in general declarations:

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 Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" _
    (ByVal lFlags As Long, lProcessID As Long) As Long

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

Private Const MAX_PATH = 260

    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_PATH
    End Type

'call the function:
sub form_load
    KillApp ("notepad.exe")
end sub

Private Function KillApp(AppToKill As String) As Boolean
    Dim uProcess As PROCESSENTRY32
    Dim rProcessFound As Long
    Dim hSnapshot As Long
    Dim szExename As String
    Dim exitCode As Long
    Dim myProcess As Long
    Dim bAppKill As Boolean
    Dim appCount As Integer
    Dim i As Integer
    Dim FoundApp As Boolean
    FoundApp = False
    appCount = 0
    Const TH32CS_SNAPPROCESS As Long = 2&
    uProcess.dwSize = Len(uProcess)
    hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    rProcessFound = ProcessFirst(hSnapshot, uProcess)
    Do While rProcessFound
        i = InStr(1, uProcess.szexeFile, Chr(0))
        szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
            'PrintMessage "DEBUG: szExename= " & szExename
        If Right$(szExename, Len(AppToKill)) = LCase$(AppToKill) Then
            FoundApp = True
            'appCount = appCount + 1
            myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                PrintMessage "DEBUG: uProcess.th32ProcessID= " & uProcess.th32ProcessID
                PrintMessage "DEBUG: myProcess= " & myProcess
            If myProcess = 0 Then
                PrintMessage "Process " & AppToKill & " couldnt be opened!"
                bAppKill = TerminateProcess(myProcess, exitCode)
                    If (bAppKill = True) Then
                        PrintMessage "Appkill/TerminateProcess= " & bAppKill
                        PrintMessage "Process " & AppToKill & " terminated"
                        KillApp = True
                    End If
            End If
            Call CloseHandle(myProcess)
        End If
        rProcessFound = ProcessNext(hSnapshot, uProcess)
    If FoundApp = False Then
        PrintMessage "Process " & AppToKill & " not running!"
    End If

    Call CloseHandle(hSnapshot)

End Function
Adry3000Author Commented:
Only cut and paste :D
thank you very much!!! you're a crack ;)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.