i need some api to harshly kill a app that is not responding.
Printable View
i need some api to harshly kill a app that is not responding.
come on!
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
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"
Closing a window by Hwnd
Usage: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
CloseWindowByHwnd 1228
If he wants to kill Apps that aren't responding, shouldn't you use SendMessage instead of PostMessage? Just curious...
Nope.
I think you should terminate it. Save yourself the time.
If you dont know the entire caption use this to get the Hwnd.
usage: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
Msgbox FindWindowLike("Visual Basic")
here is a bit shorter and by name.
VB Code:
shell "c:\windows\system32\taskkill.exe /f /im " & appname
Exclusive to NT though. =\Quote:
Originally posted by flamewavetech
here is a bit shorter and by name.
VB Code:
shell "c:\windows\system32\taskkill.exe /f /im " & appname
Oh... this is how to kill a program that you dont know the entire name.
VB 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 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 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:
CloseWindowByHwnd FindWindowLike("Visual Basic")
.i have windows XP. and it works on that too. i havent tried it on any othersQuote:
Exclusive to NT though. =\
XP is a NT... its the upgrade. So it should work on that one.
You might need this to.
This gets the top windows Caption.
VB Code:
Declare Function GetForegroundWindow Lib "user32.dll" () As Long Const GW_HWNDNEXT = 2 Const WM_GETTEXT = &HD Const WM_GETTEXTLENGTH = &HE Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long 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 Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwprocessid As Long) As Long Function GetCaption(Hwnd As Long) As String Dim Wintext As String Dim slength As Long Dim retval As Long 'Gets the Length of the Caption slength = SendMessage(Hwnd, WM_GETTEXTLENGTH, ByVal CLng(0), ByVal CLng(0)) + 1 'Makes Space for the Caption Wintext = Space(slength) 'Gets The Caption retval = SendMessage(Hwnd, WM_GETTEXT, ByVal slength, ByVal Wintext) 'Puts It in GetCaption GetCaption = Left(Wintext, retval) End Function Function GetTopWindowCaption() As String GetTopWindowCaption = GetCaption(GetForegroundWindow) End Function
This should work on all windows os.
VB Code:
Sub CloseWindowByHwnd(ByVal hwnd&) ' 'Nucleus ' Dim lPid As Long Dim lHp As Long SendMessageTimeout hwnd, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0 ' ' If the window doesn't like gentle persuasion, bring out the nipple clamps to force it to close ' If IsWindow(hwnd) Then Call GetWindowThreadProcessId(hwnd, lPid) lHp = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid) TerminateProcess lHp&, 0& CloseHandle lHp End If End Sub