Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Kill a system process from Visual Basic 6

Posted on 2003-03-23
2
Medium Priority
?
688 Views
Last Modified: 2013-11-25
Hello!
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.

MODULE1-----------------------------------------------------------
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)


-------------------------------------------------------------------
FORM1--------------------------------------------------------------

Option Explicit
Dim pID&
Dim pHandle&


Public Sub APISleep(interval&)
  sT = GetTickCount
   Do
     DoEvents
     pT = GetTickCount
       If pT - sT > interval Then
           Exit Do
        End If
   Loop
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?
0
Comment
Question by:Adry3000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
tomlander earned 200 total points
ID: 8190549
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

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


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


'=======================================================
Private Function KillApp(AppToKill As String) As Boolean
    Const PROCESS_ALL_ACCESS = &H1F0FFF
    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!"
            Else
                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)
    Loop
       
    If FoundApp = False Then
        PrintMessage "Process " & AppToKill & " not running!"
    End If

    Call CloseHandle(hSnapshot)

End Function
0
 

Author Comment

by:Adry3000
ID: 8190924
Excelent!!
Only cut and paste :D
thank you very much!!! you're a crack ;)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question