Does anyone knows how to kill a process in the task manager?
Printable View
Does anyone knows how to kill a process in the task manager?
'this code taken from a Q & A Experts
'written by someone called: AzraSound
'
'make a new project
'add 4 commandbuttons and a listbox
'
'list all items running in task
'kill a specified task
' notice the listing of all handles and names in a
' listbox, you could easily add this to a 2D array
' instead, do a search for the name and then kill
' that process accordingly.
'this goes in a bas module
Option Explicit
Public Const WM_CLOSE = &H10
Public TitleCnt As Long
Public TopFndCnt As Long
Public ChildFndCnt As Long
Public TopDispCnt As Long
Public ChildDispCnt As Long
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type BY_HANDLE_FILE_INFORMATION
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
dwVolumeSerialNumber As Long
nFileSizeHigh As Long
nFileSizeLow As Long
nNumberOfLinks As Long
nFileIndexHigh As Long
nFileIndexLow As Long
End Type
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam 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 PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
Public Function EnumWinProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
Dim retval As Long
Dim WinTitleBuf As String * 255
Dim WinTitle As String
retval = GetWindowText(lhWnd, WinTitleBuf, 255)
WinTitle = Trim$(StripNulls(WinTitleBuf))
If WinTitle > " " Then
Form1.List1.AddItem lhWnd & " " & WinTitle
Form1.List1.ItemData(Form1.List1.NewIndex) = lhWnd
TopDispCnt = TopDispCnt + 1
End If
TitleCnt = TitleCnt + 1
TopFndCnt = TopFndCnt + 1
retval = EnumChildWindows(lhWnd, AddressOf EnumChildProc, lParam)
EnumWinProc = True
End Function
Public Function EnumChildProc(ByVal lhWnd As Long, ByVal lParam As Long) As Long
Dim retval As Long
Dim WinTitleBuf As String * 255
Dim WinTitle As String
Dim s As String
Dim bHide As Boolean
retval = GetWindowText(lhWnd, WinTitleBuf, 255)
WinTitle = Trim$(StripNulls(WinTitleBuf))
If WinTitle > " " Then
Form1.List1.AddItem lhWnd & " " & WinTitle
Form1.List1.ItemData(Form1.List1.NewIndex) = lhWnd
ChildDispCnt = ChildDispCnt + 1
End If
ChildFndCnt = ChildFndCnt + 1
EnumChildProc = True
End Function
Public Function StripNulls(OriginalStr As String) As String
' This removes the extra Nulls so String comparisons will work
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function
'===========================================================
'===========================================================
'form code
Option Explicit
Dim Scanning As Boolean
Dim fileInfo As BY_HANDLE_FILE_INFORMATION
Private Sub Command2_Click()
Dim retval As Long
Dim pos As Long
Dim hwnd As Long
pos = InStr(1, List1.List(List1.ListIndex), " ", vbBinaryCompare)
hwnd = Left(List1.List(List1.ListIndex), pos)
retval = PostMessage(hwnd, WM_CLOSE, 0&, 0&)
End Sub
Private Sub Command3_Click()
Scan
End Sub
Private Sub Command4_Click()
Unload Me
End Sub
Private Sub Form_Load()
Command1.Caption = "Minimize"
Command2.Caption = "End Task"
Command3.Caption = "Refresh"
Command4.Caption = "Close"
Scan
End Sub
Private Sub Scan()
If Scanning Then Exit Sub
Scanning = True
List1.Clear
TopFndCnt = 0
TopDispCnt = 0
ChildFndCnt = 0
ChildDispCnt = 0
TitleCnt = 0
EnumWindows AddressOf EnumWinProc, 0
If List1.ListCount > 0 Then List1.ListIndex = 0
Scanning = False
End Sub
Private Sub List1_DblClick()
Dim retval As Long
Dim pos As Long
Dim hwnd As Long
pos = InStr(1, List1.List(List1.ListIndex), " ", vbBinaryCompare)
hwnd = Left(List1.List(List1.ListIndex), pos)
retval = GetFileInformationByHandle(hwnd, fileInfo)
If retval <> 0 Then
Text1.Text = fileInfo.dwVolumeSerialNumber
Else
MsgBox "error"
End If
End Sub
Thanks HeSaidJoe. This is what I am looking for, but it is kind of an overkill.
Just want to let you know two things.
First, you have the first command button on the form but there is no codes for it.
Second, Ending the task is not the same way how you would end from the Task Manager window. I am trying to close a program that is residue in the Task Manager Process Tab. Closing it from the Task Manager does not give me any other dialog box. Closing with your codes still does.
I am going to play around with it more and update back to you.
Thanks again HeSaidJoe...