Results 1 to 15 of 15

Thread: i need to kill a app that is not responding

  1. #1

    Thread Starter
    Hyperactive Member Cmdr0Sunburn's Avatar
    Join Date
    May 2001
    Location
    g0t r00t?
    Posts
    461

    Question i need to kill a app that is not responding

    i need some api to harshly kill a app that is not responding.
    I know a lot oF Vb, expert in C++, and i think in assembly.
    MSVC++6.NET
    vb6
    masm
    Windowz Xp
    I find my self using this a lot in C++

    __asm {
    }

  2. #2

    Thread Starter
    Hyperactive Member Cmdr0Sunburn's Avatar
    Join Date
    May 2001
    Location
    g0t r00t?
    Posts
    461
    come on!
    I know a lot oF Vb, expert in C++, and i think in assembly.
    MSVC++6.NET
    vb6
    masm
    Windowz Xp
    I find my self using this a lot in C++

    __asm {
    }

  3. #3
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Sure. Here is by path...

    Code:
    Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    
    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 * 6400
    End Type
    
    
    Public Function StopApp(myName As String) As Boolean
        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
        
        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))
            If Right$(szExename, Len(myName)) = LCase$(myName) Then
                StopApp = True
                appCount = appCount + 1
                myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                AppKill = TerminateProcess(myProcess, exitCode)
                Call CloseHandle(myProcess)
            End If
            rProcessFound = ProcessNext(hSnapshot, uProcess)
        Loop
    
        Call CloseHandle(hSnapshot)
    Finish:
    End Function

  4. #4
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Here is by caption

    Code:
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    Const WM_CLOSE = &H10
    Const WM_DESTROY = &H2
    
    Public sub CloseWindowByCaption(Caption As String)
    dim hParent as long 
        hParent = FindWindow(vbNullString, Caption)
        If hParent <> 0 Then 'close it
    
            PostMessage hParent, WM_CLOSE, 0, 0
            PostMessage hParent, WM_DESTROY, 0, 0
        End If
        
    End sub

    Usage:

    CloseWindowByCaption "Calculator"

  5. #5
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Closing a window by Hwnd

    Code:
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    Const WM_CLOSE = &H10
    Const WM_DESTROY = &H2
    
    Public Sub CloseWindowByhWnd(hwnd As Long)
            PostMessage hwnd, WM_CLOSE, 0, 0
            PostMessage hwnd, WM_DESTROY, 0, 0
    End Sub
    Usage:
    CloseWindowByHwnd 1228

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    If he wants to kill Apps that aren't responding, shouldn't you use SendMessage instead of PostMessage? Just curious...
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  7. #7
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Nope.

    I think you should terminate it. Save yourself the time.

  8. #8
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    If you dont know the entire caption use this to get the Hwnd.

    Code:
    Option Explicit
    Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Declare Function GetForegroundWindow Lib "user32.dll" () As Long
    
    Public Const GW_HWNDNEXT = 2
    Public Const GW_CHILD = 5
    
    '********************************************
    '*Give it part of the window text your looking for
    '*it will give you the hWnd
    '*usefull for windows that text is like "[project] - microsoft visual basic [design]"
    '*usage:
    '*Msgbox FindWindowLike("visual basic")
    '*Returns 0 if not found
    '*******************************************
    
    Function FindWindowLike(strPartOfCaption As String) As Long
    Dim hWnd As Long
    Dim strCurrentWindowText As String
    Dim r As Integer
    
    hWnd = GetForegroundWindow
    
    Do Until hWnd = 0
            strCurrentWindowText = Space$(255)
            r = GetWindowText(hWnd, strCurrentWindowText, 255)
            strCurrentWindowText = Left$(strCurrentWindowText, r)
            'hWnd = GetWindow(hWnd, GW_CHILD)
            If InStr(1, LCase(strCurrentWindowText), LCase(strPartOfCaption)) <> 0 Then GoTo Found
            hWnd = GetWindow(hWnd, GW_HWNDNEXT)
    Loop
    
    Exit Function
    Found:
    FindWindowLike = hWnd
    End Function
    usage:
    Msgbox FindWindowLike("Visual Basic")

  9. #9
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    here is a bit shorter and by name.

    VB Code:
    1. shell "c:\windows\system32\taskkill.exe /f /im " & appname
    FlameWave Technologies - internet tools

  10. #10
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Originally posted by flamewavetech
    here is a bit shorter and by name.

    VB Code:
    1. shell "c:\windows\system32\taskkill.exe /f /im " & appname
    Exclusive to NT though. =\

  11. #11
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Oh... this is how to kill a program that you dont know the entire name.

    VB Code:
    1. Option Explicit
    2. Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    3. Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    4. Declare Function GetForegroundWindow Lib "user32.dll" () As Long
    5.  
    6. Public Const GW_HWNDNEXT = 2
    7. Public Const GW_CHILD = 5
    8.  
    9. Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    10. Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    11. Const WM_CLOSE = &H10
    12. Const WM_DESTROY = &H2
    13.  
    14. Public Sub CloseWindowByhWnd(hwnd As Long)
    15.         PostMessage hwnd, WM_CLOSE, 0, 0
    16.         PostMessage hwnd, WM_DESTROY, 0, 0
    17. End Sub
    18.  
    19.  
    20. Function FindWindowLike(strPartOfCaption As String) As Long
    21. Dim hWnd As Long
    22. Dim strCurrentWindowText As String
    23. Dim r As Integer
    24.  
    25. hWnd = GetForegroundWindow
    26.  
    27. Do Until hWnd = 0
    28.         strCurrentWindowText = Space$(255)
    29.         r = GetWindowText(hWnd, strCurrentWindowText, 255)
    30.         strCurrentWindowText = Left$(strCurrentWindowText, r)
    31.         'hWnd = GetWindow(hWnd, GW_CHILD)
    32.         If InStr(1, LCase(strCurrentWindowText), LCase(strPartOfCaption)) <> 0 Then GoTo Found
    33.         hWnd = GetWindow(hWnd, GW_HWNDNEXT)
    34. Loop
    35.  
    36. Exit Function
    37. Found:
    38. FindWindowLike = hWnd
    39. End Function


    Usage:

    CloseWindowByHwnd FindWindowLike("Visual Basic")

  12. #12
    Fanatic Member
    Join Date
    Aug 2001
    Posts
    746
    .
    Exclusive to NT though. =\
    i have windows XP. and it works on that too. i havent tried it on any others
    FlameWave Technologies - internet tools

  13. #13
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    XP is a NT... its the upgrade. So it should work on that one.

  14. #14
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    You might need this to.

    This gets the top windows Caption.

    VB Code:
    1. Declare Function GetForegroundWindow Lib "user32.dll" () As Long
    2. Const GW_HWNDNEXT = 2
    3. Const WM_GETTEXT = &HD
    4. Const WM_GETTEXTLENGTH = &HE
    5. Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    6. Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hwndParent As Long, ByVal hwndChildAfter As Long, ByVal lpszClass As Any, ByVal lpszWindow As Any) As Long
    7. Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    8. Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    9. Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwprocessid As Long) As Long
    10.  
    11. Function GetCaption(Hwnd As Long) As String
    12. Dim Wintext As String
    13. Dim slength As Long
    14. Dim retval As Long
    15.  
    16. 'Gets the Length of the Caption
    17. slength = SendMessage(Hwnd, WM_GETTEXTLENGTH, ByVal CLng(0), ByVal CLng(0)) + 1
    18. 'Makes Space for the Caption
    19. Wintext = Space(slength)
    20. 'Gets The Caption
    21. retval = SendMessage(Hwnd, WM_GETTEXT, ByVal slength, ByVal Wintext)
    22. 'Puts It in GetCaption
    23. GetCaption = Left(Wintext, retval)
    24. End Function
    25.  
    26. Function GetTopWindowCaption() As String
    27. GetTopWindowCaption = GetCaption(GetForegroundWindow)
    28. End Function

  15. #15
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    This should work on all windows os.

    VB Code:
    1. Sub CloseWindowByHwnd(ByVal hwnd&)
    2.     '
    3.     'Nucleus
    4.     '
    5.     Dim lPid                    As Long
    6.     Dim lHp                     As Long
    7.  
    8.     SendMessageTimeout hwnd, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0
    9.     '
    10.     ' If the window doesn't like gentle persuasion, bring out the nipple clamps to force it to close
    11.     '
    12.     If IsWindow(hwnd) Then
    13.         Call GetWindowThreadProcessId(hwnd, lPid)
    14.         lHp = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid)
    15.         TerminateProcess lHp&, 0&
    16.         CloseHandle lHp
    17.     End If
    18.  
    19.  
    20. End Sub

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