Results 1 to 18 of 18

Thread: VB - End a process by its window title...

  1. #1

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    VB - End a process by its window title...

    A lot of people have been asking for a code that will kill a process - here is something that I have some up with:

    VB Code:
    1. Private Const WM_CLOSE = &H10
    2. Private g_TheCollWin As Collection
    3.  
    4. Private Declare Function EnumWindows Lib "user32.dll" ( _
    5.     ByVal lpEnumFunc As Long, _
    6.     ByVal lParam As Long) As Boolean
    7. Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" ( _
    8.     ByVal hwnd As Long, _
    9.     ByVal lpString As String, _
    10.     ByVal cch As Long) As Long
    11. Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" ( _
    12.     ByVal hwnd As Long) As Long
    13. Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
    14.     ByVal hwnd As Long, _
    15.     ByVal wMsg As Long, _
    16.     ByVal wParam As Long, _
    17.     lParam As Any) As Long
    18. Private Declare Function SetForegroundWindow Lib "user32.dll" ( _
    19.     ByVal hwnd As Long) As Long
    20.  
    21. Private Sub Kill_Process(ByVal strWindowTitle As String)
    22.     Set g_TheCollWin = New Collection
    23.     g_TheCollWin.Add strWindowTitle
    24.     Call EnumWindows(AddressOf EnumWindowsProc, ByVal 0&)
    25. End Sub
    26.  
    27. Public Function EnumWindowsProc(ByVal lngHwnd As Long, ByVal lngParam As Long) As Boolean
    28.     Dim strSave As String
    29.     Dim lngRet As Long
    30.     Dim varTheItem As Variant
    31.     lngRet = GetWindowTextLength(lngHwnd)
    32.     If lngRet > 0 Then
    33.         strSave = Space$(lngRet)
    34.         Call GetWindowText(lngHwnd, strSave, lngRet + 1)
    35.         For Each varTheItem In g_TheCollWin
    36.             If InStr(1, strSave, varTheItem, vbTextCompare) > 0 Then
    37.                 Call SetForegroundWindow(lngHwnd)
    38.                 Call SendMessage(lngHwnd, WM_CLOSE, 0, 0)
    39.             End If
    40.         Next
    41.     End If
    42.     EnumWindowsProc = True
    43. End Function

    This basically searches through the windows (Using the Collection Object) and then when it finds one with the given title it sends the close mesaage to it, making it to close.

    The call function is simple:

    VB Code:
    1. Kill_Process ("ApiViewer 2004 [Win32api.apv]")

    In the example above, if the API Viewer is open then it will kill it. This string can be a partial or a full string.

    One Note: this Will not work on processes that do not have windows.

    Cheers and hoep that helps someone

    Cheers,

    RyanJ
    Last edited by sciguyryan; May 24th, 2005 at 05:52 AM.
    My Blog.

    Ryan Jones.

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: VB - End a process by its window title...

    another way...

    VB Code:
    1. Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long
    2. Declare Function sendmessagebystring Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    3. Declare Function getwindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    4. Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    5. Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    6.     Public Const WM_CLOSE = &H10
    7.     Public Const GW_CHILD = 5
    8.     Public Const GW_HWNDFIRST = 0
    9.     Public Const GW_HWNDLAST = 1
    10.     Public Const GW_HWNDNEXT = 2
    11.     Public Const GW_HWNDPREV = 3
    12.     Public Const GW_MAX = 5
    13.     Public Const GW_OWNER = 4
    14. '**************************************
    15. ' Name: Close a window(if you know part
    16. '     of the title)
    17. ' Description:This code closes a window,
    18. '     if you know part of it's title. It uses
    19. '     some AOL API's, hehe(sendmessagebystring
    20. '     ). I used it for closing Netscape window
    21. '     s because the "Window Class Name" always
    22. '     changed. Not sure if this code has any u
    23. '     se though...
    24. ' By: Che
    25. '
    26. ' Assumes:1. The title, or part of the t
    27. '     itle.
    28. '2. A form named "Form1", or you could change the code a little
    29. '
    30. 'This code is copyrighted and has' limited warranties.Please see [url]http://w[/url]
    31. '     ww.Planet-Source-Code.com/vb/scripts/Sho
    32. '     wCode.asp?txtCodeId=5499&lngWId=1'for details.'**************************************
    33.  
    34.  
    35.  
    36. Function FindWindowByTitle(Title As String)
    37.     Dim a, b, Caption
    38.     a = getwindow(frmMain.hwnd, GW_OWNER)
    39.     Caption = GetCaption(a)
    40.  
    41.  
    42.     If InStr(1, LCase(Caption), LCase(Title)) <> 0 Then
    43.         FindWindowByTitle = b
    44.         Exit Function
    45.     End If
    46.     b = a
    47.  
    48.  
    49.     Do While b <> 0: DoEvents
    50.         b = getwindow(b, GW_HWNDNEXT)
    51.         Caption = GetCaption(b)
    52.  
    53.  
    54.         If InStr(1, LCase(Caption), LCase(Title)) <> 0 Then
    55.             FindWindowByTitle = b
    56.             Exit Do
    57.             Exit Function
    58.         End If
    59.     Loop
    60. End Function
    61.  
    62.  
    63. Function GetCaption(hwnd)
    64.     Dim hwndLength%, hwndTitle$, a%
    65.     hwndLength% = GetWindowTextLength(hwnd)
    66.     hwndTitle$ = String$(hwndLength%, 0)
    67.     a% = GetWindowText(hwnd, hwndTitle$, (hwndLength% + 1))
    68.     GetCaption = hwndTitle$
    69. End Function
    70.  
    71.  
    72. Sub KillWin(Title As String)
    73.     Dim a, hwnd
    74.     hwnd = FindWindowByTitle(Title)
    75.     a = sendmessagebystring(hwnd, WM_CLOSE, 0, 0)
    76. End Sub

    KillWin ("Diablo II")

  3. #3

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: VB - End a process by its window title...

    Another interesting way, thank you for sharing that with us |2eM!x

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  4. #4
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: VB - End a process by its window title...

    jup jup!

  5. #5
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: VB - End a process by its window title...

    Yet another way, I find this the easiest...

    VB Code:
    1. Option Explicit
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    4. Private Const WM_CLOSE = &H10
    5.  
    6. Private Sub Form_Load()
    7.     Dim WindowName As String
    8.  
    9.     Dim Window As Long
    10.    
    11.         WindowName = InputBox("What window do you want to close?", "What Window")
    12.  
    13.         Window = FindWindow(vbNullString, WindowName)
    14.    
    15.         SendMessage Window, WM_CLOSE, 0, vbNullString
    16.    
    17.         Unload Me
    18. End Sub

    -Sir Loin

  6. #6

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: VB - End a process by its window title...

    That is exactly the same method as mine uses if you noticed

    All the difference and extra code allows for multiple windows to be closed

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  7. #7
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    537

    Re: VB - End a process by its window title...

    Oops, now that I look at that, you're right.
    Sorry, when I posted that I was tired...

    -Sir Loin

  8. #8

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: VB - End a process by its window title...

    Quote Originally Posted by Sir Loin
    Oops, now that I look at that, you're right.
    Sorry, when I posted that I was tired...

    -Sir Loin
    Its fine, it saves all of the code we have posted if you only need to close a single window


    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  9. #9
    Addicted Member
    Join Date
    Feb 2008
    Location
    XP & Vista
    Posts
    181

    Re: VB - End a process by its window title...

    Can anyone tell me how to terminate the process with using the pid?

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: VB - End a process by its window title...

    Look for the TerminateProcess API.

    http://www.vbforums.com/showthread.p...rminateProcess


    Has someone helped you? Then you can Rate their helpful post.

  11. #11
    Addicted Member
    Join Date
    Feb 2008
    Location
    XP & Vista
    Posts
    181

    Re: VB - End a process by its window title...

    Thank for the link... Let me check out now

  12. #12
    New Member
    Join Date
    Jun 2009
    Posts
    1

    Cool Re: VB - End a process by its window title...

    'This is probably the easiest of all.


    'Step 1: make sure to create one button called "Command1" on a new form
    'Step 2: create a text file and type: "taskkill /IM excel.exe /F" into it
    'Note: /F forcefully terminates the process. Leave it out if you would like to gracefully shut it down. This means that if the file must be saved due to changes windows will ask you first.
    'Step3: Name the text file "killprocess" and save to C drive. (this is what I called it and where I saved it in this code)
    'Step4: Change the extention of the text file to ".bat"
    'Note: This is to end all instances of Excel that are running in the task manager. If it is another program you wish to terminate, then find out its process name from the task manager.
    'This works 100%. I have tested it.


    Private Sub Command1_Click()
    Shell "C:\killprocess.bat", vbHide
    End Sub

  13. #13
    Addicted Member
    Join Date
    Feb 2008
    Location
    XP & Vista
    Posts
    181

    Re: VB - End a process by its window title...

    The question was how to do with pid???? Please read the question fully and post reply

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

    Re: VB - End a process by its window title...

    Quote Originally Posted by kpmsivachand View Post
    The question was how to do with pid???? Please read the question fully and post reply
    Code:
    Option Explicit
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal handle As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    
    Public Function KillProcess(ByVal ProcessID As Long) As Boolean
        Dim hProc As Long
        Const SYNCHRONIZE = &H100000
        Const PROCESS_TERMINATE As Long = &H1
        Const fdwAccess As Long = SYNCHRONIZE Or PROCESS_TERMINATE
        hProc = OpenProcess(fdwAccess, 0&, ProcessID)
        If hProc Then
            If TerminateProcess(hProc, 0&) Then KillProcess = True
            Call CloseHandle(hProc)
        End If
    End Function

  15. #15
    Addicted Member
    Join Date
    Feb 2008
    Location
    XP & Vista
    Posts
    181

    Resolved Re: VB - End a process by its window title...

    Quote Originally Posted by Edgemeal View Post
    Code:
    Option Explicit
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal handle As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    
    Public Function KillProcess(ByVal ProcessID As Long) As Boolean
        Dim hProc As Long
        Const SYNCHRONIZE = &H100000
        Const PROCESS_TERMINATE As Long = &H1
        Const fdwAccess As Long = SYNCHRONIZE Or PROCESS_TERMINATE
        hProc = OpenProcess(fdwAccess, 0&, ProcessID)
        If hProc Then
            If TerminateProcess(hProc, 0&) Then KillProcess = True
            Call CloseHandle(hProc)
        End If
    End Function
    Thanks for the code...

  16. #16
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: VB - End a process by its window title...

    Quote Originally Posted by Edgemeal View Post
    Code:
    Option Explicit
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal handle As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    
    Public Function KillProcess(ByVal ProcessID As Long) As Boolean
        Dim hProc As Long
        Const SYNCHRONIZE = &H100000
        Const PROCESS_TERMINATE As Long = &H1
        Const fdwAccess As Long = SYNCHRONIZE Or PROCESS_TERMINATE
        hProc = OpenProcess(fdwAccess, 0&, ProcessID)
        If hProc Then
            If TerminateProcess(hProc, 0&) Then KillProcess = True
            Call CloseHandle(hProc)
        End If
    End Function
    Could you possibly expand on this. I need to return the PID of a particular instance of SvcHost. I want to kill it if it's using more than 80% of CPU cycles. Pointing me in the right direction would be of great help.

    Thanks
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  17. #17
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: VB - End a process by its window title...

    Quote Originally Posted by CDRIVE View Post
    ... I need to return the PID of a particular instance of SvcHost. I want to kill it if it's using more than 80% of CPU cycles. Pointing me in the right direction would be of great help.
    Process Explorer/Process Hacker can help you identify which service is taxing your CPU so you can stop it gracefully.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  18. #18
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: VB - End a process by its window title...

    Quote Originally Posted by Bonnie West View Post
    Process Explorer/Process Hacker can help you identify which service is taxing your CPU so you can stop it gracefully.
    Thank You. I'll check them out.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

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