How can i End Proccess of 1 running program usingf VB6.0 Application..

Posted on 2006-04-17
Last Modified: 2008-02-01
Hello experts newbie here ...

I want to put this source at my project .. when i am click at the command1 to End Process of 1 of the running applications ... it is possible to do that in VB6.0 project?
Question by:KingSencat
    LVL 13

    Accepted Solution

    short answer: yes that is possible

    longer answer:

    to use code:

    Private Sub Command1_Click()
       KillApp "Notepad.exe"
    End Sub


    Option Explicit

    Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
    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 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
    Private Const MAX_PATH& = 260
    Private Const TOKEN_ADJUST_PRIVILEGES = &H20
    Private Const TOKEN_QUERY = &H8
    Private Const SE_PRIVILEGE_ENABLED = &H2
    Private Const PROCESS_ALL_ACCESS = &H1F0FFF
    Private Type LUID
        lowpart As Long
        highpart As Long
    End Type
        PrivilegeCount As Long
        LuidUDT As LUID
        Attributes As Long
    End Type
    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

    Private Function KillApp(myName As String) As Boolean
        Const TH32CS_SNAPPROCESS As Long = 2&
        Const PROCESS_ALL_ACCESS = 0
        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 AppKill As Boolean
        Dim appCount As Integer
        Dim i As Integer
        On Local Error GoTo Finish
        appCount = 0
        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))
            If Right$(szExename, Len(myName)) = LCase$(myName) Then
                KillApp = True
                appCount = appCount + 1
                myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                KillProcess uProcess.th32ProcessID, 0
            End If
            rProcessFound = ProcessNext(hSnapshot, uProcess)
        Call CloseHandle(hSnapshot)
        Exit Function
        MsgBox "Error!"
    End Function

    Private 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)
            ' close the process handle
            CloseHandle hProcess
        End If

        If GetVersion() >= 0 Then
            ' under NT restore original privileges
            tp.Attributes = 0
            AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
            If hToken Then CloseHandle hToken
        End If

    End Function


    Author Comment

    It's still on the task manager ... is running as SYSTEM by the way...
    LVL 14

    Expert Comment


    Author Comment

    Mark_FreeSoftware ... ?
    LVL 13

    Expert Comment


    which process do you want to kill?

    Author Comment

    the problem that is running as USERNAME: SYSTEM ..

    Author Comment

    It's one onother application that i create and i create it to run as a system file
    ... this source what u give me cannot end proccess of a system
    LVL 14

    Expert Comment

    Here is another sample code, but I guess the Mark's code does efectively the same

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    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…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now