|
-
Dec 4th, 2001, 05:03 PM
#1
Thread Starter
Hyperactive Member
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 {
}
-
Dec 4th, 2001, 05:12 PM
#2
Thread Starter
Hyperactive Member
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 {
}
-
Dec 4th, 2001, 05:18 PM
#3
Frenzied Member
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
-
Dec 4th, 2001, 05:19 PM
#4
Frenzied Member
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"
-
Dec 4th, 2001, 05:22 PM
#5
Frenzied Member
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
-
Dec 4th, 2001, 05:22 PM
#6
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.
-
Dec 4th, 2001, 05:24 PM
#7
Frenzied Member
Nope.
I think you should terminate it. Save yourself the time.
-
Dec 4th, 2001, 05:25 PM
#8
Frenzied Member
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")
-
Dec 4th, 2001, 05:26 PM
#9
Fanatic Member
here is a bit shorter and by name.
VB Code:
shell "c:\windows\system32\taskkill.exe /f /im " & appname
FlameWave Technologies - internet tools
-
Dec 4th, 2001, 05:31 PM
#10
Frenzied Member
Originally posted by flamewavetech
here is a bit shorter and by name.
VB Code:
shell "c:\windows\system32\taskkill.exe /f /im " & appname
Exclusive to NT though. =\
-
Dec 4th, 2001, 05:39 PM
#11
Frenzied Member
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")
-
Dec 4th, 2001, 05:48 PM
#12
Fanatic Member
.
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
-
Dec 4th, 2001, 05:52 PM
#13
Frenzied Member
XP is a NT... its the upgrade. So it should work on that one.
-
Dec 4th, 2001, 05:58 PM
#14
Frenzied Member
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
-
Dec 4th, 2001, 06:01 PM
#15
Registered User
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|