Terminate process by exe name-VBForums
Results 1 to 12 of 12

Thread: Terminate process by exe name

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2008
    Posts
    1,260

    Terminate process by exe name

    I would like some code that will terminate a process if i know its exe name.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,237

    Re: Terminate process by exe name

    ' Heres one way, example would close all running "calc.exe" files.
    Code:
    Private Sub Command1_Click()
        TerminateProcess ("calc.exe")
    End Sub
     
    Private Sub TerminateProcess(app_exe As String)
        Dim Process As Object
        For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & app_exe & "'")
            Process.Terminate
        Next
    End Sub

  3. #3
    Addicted Member
    Join Date
    May 2007
    Location
    Belgium
    Posts
    156

    Re: Terminate process by exe name

    Or, use the tskill.exe,
    Code:
    Shell "tskill appnamehere.exe"

  4. #4
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,435

    Re: Terminate process by exe name

    Posted this in your other thread and will work for both with the previous post mods I mentioned.
    http://www.vbforums.com/showpost.php...73&postcount=8
    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!
    Star Wars Gangsta Rap 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, Corsair H100i v2 water cooler, Geforce GTX1060, Samsung 500 GB SSD, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2010, VS 2010

  5. #5
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: Terminate process by exe name

    robdog888, i have try your code. it works for me, but got some problem. in my case i got three exe. your code work fine with one exe, but if in situation got randomly two exe or three exe running, termination will failed. how to change this part of code so it work correctly?
    Code:
    Dim blnRet As Boolean
       blnRet = TerminateEXE("Woof.exe")
       If Not blnRet Then
           MsgBox "Terminate failed!"
       End if
    i thought can change like this but failed.
    Code:
    Dim blnRet As Boolean
       blnRet = TerminateEXE("Woof.exe & notepad.exe & paint.exe")
       If Not blnRet Then
           MsgBox "Terminate failed!"
       End if

  6. #6
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Terminate process by exe name

    Are you saying that the exe names your program will start will be random exe names?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  7. #7
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: Terminate process by exe name

    hack: no. i hv three fixed exe name. let say i hv exe1, exe2, exe3. if i open exe1 and close, the code work. but if i open exe 1 and exe2, then close, the code wouldnt work anf termination failed. so i mean how to change the code so that when randomly open those three exe (maybe 1 exe only, maybe 2 exe or all 3 exe), all can be terminated when closed.

  8. #8
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,435

    Re: Terminate process by exe name

    You need to check for all 3 and not just one exe.

    Code:
    Dim blnRet As Boolean
    blnRet = TerminateEXE("Woof.exe")
    If Not blnRet Then
        MsgBox "Terminate failed!"
    End if
    blnRet = TerminateEXE("Meow.exe")
    If Not blnRet Then
        MsgBox "Terminate failed!"
    End if
    blnRet = TerminateEXE("Moo.exe")
    If Not blnRet Then
        MsgBox "Terminate failed!"
    End if
    But I wouldnt really say it should say failed since the exe may not be running. The fail or error should be dealt with in the called procedure
    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!
    Star Wars Gangsta Rap 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, Corsair H100i v2 water cooler, Geforce GTX1060, Samsung 500 GB SSD, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2010, VS 2010

  9. #9
    Addicted Member
    Join Date
    Jul 2008
    Posts
    172

    Re: Terminate process by exe name

    robdog888, thanks a lot. i got it.

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

    Re: Terminate process by exe name

    Quote Originally Posted by masterkert3 View Post
    Or, use the tskill.exe,
    Code:
    Shell "tskill appnamehere.exe"
    good idea... but it's unable to kill exe files that contains capital names or contains space in between. it seems it's not case sensitive

  11. #11
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: Terminate process by exe name

    Quote Originally Posted by Mehdi Jazini View Post
    good idea... but it's unable to kill exe files that contains capital names or contains space in between. it seems it's not case sensitive
    That is not true. try:

    Code:
    Shell ("TSKILL ""Exe NaMe Here""")

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

    Re: Terminate process by exe name

    Quote Originally Posted by stum View Post
    That is not true. try:

    Code:
    Shell ("TSKILL ""Exe NaMe Here""")
    tnx but:

    1) i have tested your code in VB6 but it couldn't kill the task

    2) i have opened MS-DOS and executed the following command in it, but i think the MS-DOS programmer used something like LCASE() function in TSKILL arguments! look at the result:

    C:\>tskill "Connection Manager.exe"
    Could not find process: connection manager.exe

    C:\>
    Last edited by Mahdi Jazini; Jan 6th, 2017 at 05:25 AM.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.