Results 1 to 11 of 11

Thread: Terminating Running EXE

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Posts
    279

    Terminating Running EXE

    I know about all the "examples" here on vbforums and I have searched around but all, and I mean all, literally, the examples didn't work for me. I have XP Pro SP2.

    Could someone kindly help me out with this problem? How to close a process by it's EXE name. So far I've tried TerminateProcess, KillApp, KillAppByName, TerminateEXE, etc. None seem to work for me. When it's called, it simply doesn't do anything and the process keeps loading.

    I would appreciate a responce on this.

  2. #2
    Addicted Member Luke K's Avatar
    Join Date
    Jun 2004
    Location
    Perth, Australia
    Posts
    183

    Re: Terminating Running EXE

    I know exactly what you mean Tantrum3k, I was in the same position you were before writing this myself and dis-regarding everyone's examples. I also have XP Pro SP2 so this should definately work for you. I'v commented it so you can really know what it is doing

    VB Code:
    1. Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    2. Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    3. Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    4. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    5. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    6.  
    7. Private Const TH32CS_SNAPPROCESS As Long = 2&
    8. Private Const MAX_PATH As Integer = 260
    9.  
    10. Private Type PROCESSENTRY32
    11.     dwSize As Long
    12.     cntUsage As Long
    13.     th32ProcessID As Long
    14.     th32DefaultHeapID As Long
    15.     th32ModuleID As Long
    16.     cntThreads As Long
    17.     th32ParentProcessID As Long
    18.     pcPriClassBase As Long
    19.     dwFlags As Long
    20.     szExeFile As String * MAX_PATH
    21. End Type
    22.  
    23. Private Sub Form_Load()
    24. Dim lLng As Long, lA As Long, lExCode As Long
    25. Dim procObj As PROCESSENTRY32
    26. Dim hSnap As Long
    27. Dim lRet As Long
    28. Dim sExeNam As String
    29.  
    30. sExeNam$ = "YourProgramName.exe"
    31. hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) 'create a snapshot of the system process information
    32. procObj.dwSize = Len(procObj)
    33. lRet = Process32First(hSnap, procObj) 'Query information on the top-most running process
    34.  
    35. Do While Process32Next(hSnap, procObj) 'loop through all the processes
    36.     If InStr(1, LCase(procObj.szExeFile), LCase(sExeNam$)) > 0 Then 'Your exe name has been found
    37.         lLng = OpenProcess(&H1, ByVal 0&, procObj.th32ProcessID) 'Open the process as to get its handle
    38.         lA = TerminateProcess(lLng, lExCode) 'Terminate the process
    39.         Exit Do
    40.     End If
    41. Loop
    42. End Sub
    Artificial Intelligence At War! - The best game of its genre
    Program your own robot and watch it fight in 3d!
    Droidarena 3

    If I have been useful, please Rate My Post

    Support FireFox -
    Microsoft Visual Studio .NET Professional 2003
    Microsoft Visual Studio 6, Enterprise Edition
    Microsoft Windows XP Professional, Service Pack 2

  3. #3
    Lively Member Tw1sted L0gic's Avatar
    Join Date
    Jan 2005
    Posts
    88

    Re: Terminating Running EXE

    Will this also work for other versions of Windows? (Such as 98, ME, NT, 2K etc.)
    Naughty but Nice

  4. #4
    Frenzied Member ice_531's Avatar
    Join Date
    Aug 2002
    Location
    Sitting w/ Bob Status: -Next -To- Null- Friend: Philip
    Posts
    1,152

    Re: Terminating Running EXE

    Kill App.path & "/myprogram.exe"
    :::`DISCLAIMER`:::
    Do NOT take anything i have posted to be truthful in any way, shape or form.
    Thank You!

    --------------------------------
    "Never heard about "hiking" poles. I usualy just grab a stick from the nature, and use that as a pole." - NoteMe
    "Finaly I can look as gay as I want..." - NoteMe
    Languages: VB6, BASIC, Java, C#. C++

  5. #5
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564

    Re: Terminating Running EXE

    That just deletes an .exe file, not stop a running process.

    Quote Originally Posted by ice_531
    Kill App.path & "/myprogram.exe"

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Terminating Running EXE

    You can not delete an exe if it is currently running. You get an error if you try.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    Re: Terminating Running EXE

    Also keep in mind that when you run an VB Exe normally it runs with the Logged in User credential, so if the process you want to terminate is a System Process then those API wont work unless the Logged in user is part of Administrator user Group.

    Just thought I will point that out incase that is the cause of process not terminationg...
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  8. #8
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Terminating Running EXE

    I have used the following code for years, and it's never failed me, and works perfectly.
    Code:
    Option Explicit
    
    Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
    Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
    Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    
    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 * 260
    End Type
    
    Private Type OSVERSIONINFO
      dwOSVersionInfoSize As Long
      dwMajorVersion As Long
      dwMinorVersion As Long
      dwBuildNumber As Long
      dwPlatformId As Long
      szCSDVersion As String * 128
    End Type
    
    Private Const PROCESS_TERMINATE = &H1
    Private Const VER_PLATFORM_WIN32_WINDOWS = 1
    Private Const PROCESS_QUERY_INFORMATION = 1024
    Private Const PROCESS_VM_READ = 16
    Private Const TH32CS_SNAPPROCESS = &H2
    
    Private Function CheckVersion() As Long
    Dim tOS As OSVERSIONINFO
      tOS.dwOSVersionInfoSize = Len(tOS)
      Call GetVersionEx(tOS)
      CheckVersion = tOS.dwPlatformId
    End Function
    
    Private Function GetEXEProcessID(ByVal sEXE As String) As Long
      Dim aPID() As Long
      Dim lProcesses As Long
      Dim lProcess As Long
      Dim lModule As Long
      Dim sName As String
      Dim iIndex As Integer
      Dim bCopied As Long
      Dim lSnapShot As Long
      Dim tPE As PROCESSENTRY32
      Dim bDone As Boolean
      
      If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
        'Windows 9x
        'Create a SnapShot of the Currently Running Processes
        lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
        If lSnapShot < 0 Then Exit Function
        tPE.dwSize = Len(tPE)
        'Buffer the First Processes Info..
        bCopied = Process32First(lSnapShot, tPE)
        Do While bCopied
          'While there are Processes List them..
          sName = Left$(tPE.szExeFile, InStr(tPE.szExeFile, Chr(0)) - 1)
          sName = Mid(sName, InStrRev(sName, "\") + 1)
          If InStr(sName, Chr(0)) Then
            sName = Left(sName, InStr(sName, Chr(0)) - 1)
          End If
          bCopied = Process32Next(lSnapShot, tPE)
          If StrComp(sEXE, sName, vbTextCompare) = 0 Then
            GetEXEProcessID = tPE.th32ProcessID
            Exit Do
          End If
        Loop
        
      Else
        'Windows NT
        'The EnumProcesses Function doesn't indicate how many Process there are,
        'so you need to pass a large array and trim off the empty elements
        'as cbNeeded will return the no. of Processes copied.
        ReDim aPID(255)
        Call EnumProcesses(aPID(0), 1024, lProcesses)
        lProcesses = lProcesses / 4
        ReDim Preserve aPID(lProcesses)
        
        For iIndex = 0 To lProcesses - 1
          'Get the Process Handle, by Opening the Process
          lProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, aPID(iIndex))
          If lProcess Then
            'Just get the First Module, all we need is the Handle to get
            'the Filename..
            If EnumProcessModules(lProcess, lModule, 4, 0&) Then
              sName = Space(260)
              Call GetModuleFileNameExA(lProcess, lModule, sName, Len(sName))
              If InStr(sName, "\") > 0 Then
                sName = Mid(sName, InStrRev(sName, "\") + 1)
              End If
              If InStr(sName, Chr(0)) Then
                sName = Left(sName, InStr(sName, Chr(0)) - 1)
              End If
              If StrComp(sEXE, sName, vbTextCompare) = 0 Then
                GetEXEProcessID = aPID(iIndex)
                bDone = True
              End If
            End If
            'Close the Process Handle
            CloseHandle lProcess
            If bDone Then Exit For
          End If
        Next
      End If
    End Function
    
    Public Function TerminateEXE(ByVal sEXE As String) As Boolean
      Dim lPID As Long
      Dim lProcess As Long
      
      lPID = GetEXEProcessID(sEXE)
      If lPID = 0 Then Exit Function
      lProcess = OpenProcess(PROCESS_TERMINATE, 0, lPID)
      Call TerminateProcess(lProcess, 0&)
      Call CloseHandle(lProcess)
      
      TerminateEXE = True
    End Function
    Add that to a module, then in your form do:
    Code:
    Private Sub cmdCloseApp_Click()
    Dim blnRet As Boolean
       blnRet = TerminateEXE("Woof.exe")
       If Not blnRet Then
           MsgBox "Terminate failed!"
       End if
    End Sub
    Hope that helps.
    I know the code above is a little messy.
    If anyone wants me to tidy it up then I will.

    Woka

  9. #9
    New Member
    Join Date
    Nov 2005
    Posts
    1

    Re: Terminating Running EXE

    This is excellent!
    I've been looking for a way to check whether a specific app is running or not, and act accordingly.
    This code works fine - slightly adapted, using it as a Boolean Function

    thanks..!

    VB Code:
    1. Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    2. Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    3. Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    4. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    5. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    6.  
    7. Private Const TH32CS_SNAPPROCESS As Long = 2&
    8. Private Const MAX_PATH As Integer = 260
    9.  
    10. Private Type PROCESSENTRY32
    11.     dwSize As Long
    12.     cntUsage As Long
    13.     th32ProcessID As Long
    14.     th32DefaultHeapID As Long
    15.     th32ModuleID As Long
    16.     cntThreads As Long
    17.     th32ParentProcessID As Long
    18.     pcPriClassBase As Long
    19.     dwFlags As Long
    20.     szExeFile As String * MAX_PATH
    21. End Type
    22.  
    23. Private Sub Form_Load()
    24. Dim lLng As Long, lA As Long, lExCode As Long
    25. Dim procObj As PROCESSENTRY32
    26. Dim hSnap As Long
    27. Dim lRet As Long
    28. Dim sExeNam As String
    29.  
    30. sExeNam$ = "YourProgramName.exe"
    31. hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) 'create a snapshot of the system process information
    32. procObj.dwSize = Len(procObj)
    33. lRet = Process32First(hSnap, procObj) 'Query information on the top-most running process
    34.  
    35. Do While Process32Next(hSnap, procObj) 'loop through all the processes
    36.     If InStr(1, LCase(procObj.szExeFile), LCase(sExeNam$)) > 0 Then 'Your exe name has been found
    37.         lLng = OpenProcess(&H1, ByVal 0&, procObj.th32ProcessID) 'Open the process as to get its handle
    38.         lA = TerminateProcess(lLng, lExCode) 'Terminate the process
    39.         Exit Do
    40.     End If
    41. Loop
    42. End Sub
    [/QUOTE]

  10. #10
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    89

    Re: Terminating Running EXE

    Quote Originally Posted by Luke K View Post
    I know exactly what you mean Tantrum3k, I was in the same position you were before writing this myself and dis-regarding everyone's examples. I also have XP Pro SP2 so this should definately work for you. I'v commented it so you can really know what it is doing

    VB Code:
    1. Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    2. Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    3. Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    4. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    5. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    6.  
    7. Private Const TH32CS_SNAPPROCESS As Long = 2&
    8. Private Const MAX_PATH As Integer = 260
    9.  
    10. Private Type PROCESSENTRY32
    11.     dwSize As Long
    12.     cntUsage As Long
    13.     th32ProcessID As Long
    14.     th32DefaultHeapID As Long
    15.     th32ModuleID As Long
    16.     cntThreads As Long
    17.     th32ParentProcessID As Long
    18.     pcPriClassBase As Long
    19.     dwFlags As Long
    20.     szExeFile As String * MAX_PATH
    21. End Type
    22.  
    23. Private Sub Form_Load()
    24. Dim lLng As Long, lA As Long, lExCode As Long
    25. Dim procObj As PROCESSENTRY32
    26. Dim hSnap As Long
    27. Dim lRet As Long
    28. Dim sExeNam As String
    29.  
    30. sExeNam$ = "YourProgramName.exe"
    31. hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) 'create a snapshot of the system process information
    32. procObj.dwSize = Len(procObj)
    33. lRet = Process32First(hSnap, procObj) 'Query information on the top-most running process
    34.  
    35. Do While Process32Next(hSnap, procObj) 'loop through all the processes
    36.     If InStr(1, LCase(procObj.szExeFile), LCase(sExeNam$)) > 0 Then 'Your exe name has been found
    37.         lLng = OpenProcess(&H1, ByVal 0&, procObj.th32ProcessID) 'Open the process as to get its handle
    38.         lA = TerminateProcess(lLng, lExCode) 'Terminate the process
    39.         Exit Do
    40.     End If
    41. Loop
    42. End Sub
    Thank you very much it worked g8

    How can we do (Exit Process Tree) using VB6?

  11. #11
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Terminating Running EXE

    Quote Originally Posted by Mehdi Jazini View Post
    How can we do (Exit Process Tree) using VB6?
    You are better off creating you own topic to ask the question is instead of hijack someone else's topic.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width