PDA

Click to See Complete Forum and Search --> : Taskmanager-like program. TerminateProcess


D!SiLLUSiON
Jun 11th, 2000, 03:10 AM
I find it hard to believe that all the ppl who view this post... just don't know the answer.. :((


Well, since i've got yer attention:

I'm working on a sort of task-manager-like program. I can get all the open (normal) windows, and bring them to front.. but how to terminate them? (No code seems to work.. mabey it's because i'm using Win2k Server?)


The Code in my module:

Option Explicit

Public Const SW_SHOW = 5
Public Const SW_RESTORE = 9
Public Const GW_OWNER = 4
Public Const GWL_HWNDPARENT = (-8)
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_TOOLWINDOW = &H80
Public Const WS_EX_APPWINDOW = &H40000
Public Const LB_ADDSTRING = &H180
Public Const LB_SETITEMDATA = &H19A
Public Const EWX_FORCE = 4
Public Const EWX_LOGOFF = 0
Public Const EWX_REBOOT = 2
Public Const EWX_SHUTDOWN = 1
Public Const PROCESS_ALL_ACCESS = 0
Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const MAX_PATH& = 260
Public Const PROCESS_TERMINATE = &H1

Public 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 * MAX_PATH
End Type

Public Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Public Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Public Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public 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
Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

Public Sub pSetForegroundWindow(ByVal hwnd As Long)
Dim lForeThreadID As Long
Dim lThisThreadID As Long
Dim lReturn As Long

If hwnd <> GetForegroundWindow() Then
lForeThreadID = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
lThisThreadID = GetWindowThreadProcessId(hwnd, ByVal 0&)
If lForeThreadID <> lThisThreadID Then
Call AttachThreadInput(lForeThreadID, lThisThreadID, True)
lReturn = SetForegroundWindow(hwnd)
Call AttachThreadInput(lForeThreadID, lThisThreadID, False)
Else
lReturn = SetForegroundWindow(hwnd)
End If
If IsIconic(hwnd) Then
Call ShowWindow(hwnd, SW_RESTORE)
Else
Call ShowWindow(hwnd, SW_SHOW)
End If
End If
End Sub

Public Function fEnumWindows(lst As ListBox) As Long
With lst
.Clear
Call EnumWindows(AddressOf fEnumWindowsCallBack, .hwnd)
fEnumWindows = .ListCount
End With
End Function

Private Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim lReturn As Long
Dim lExStyle As Long
Dim bNoOwner As Boolean
Dim sWindowText As String

If hwnd <> frmCtrlAltDel.hwnd Then
If IsWindowVisible(hwnd) Then
If GetParent(hwnd) = 0 Then
bNoOwner = (GetWindow(hwnd, GW_OWNER) = 0)
lExStyle = GetWindowLong(hwnd, GWL_EXSTYLE)
If (((lExStyle And WS_EX_TOOLWINDOW) = 0) And bNoOwner) Or ((lExStyle And WS_EX_APPWINDOW) And Not bNoOwner) Then
sWindowText = Space$(256)
lReturn = GetWindowText(hwnd, sWindowText, Len(sWindowText))
If lReturn Then
sWindowText = Left$(sWindowText, lReturn)
lReturn = SendMessage(lParam, LB_ADDSTRING, 0, ByVal sWindowText)
Call SendMessage(lParam, LB_SETITEMDATA, lReturn, ByVal hwnd)
End If
End If
End If
End If
End If
fEnumWindowsCallBack = True
End Function


And The Code in my Form:

Option Explicit

Private Sub Form_Load()
Hide
Call pGetTasks
Show
End Sub

Private Sub cmdKill_Click()
With lstTasks
Call pTerminateProcess(.List(.ListIndex))
End With
End Sub

Private Sub cmdSwitch_Click()
With lstTasks
If .ListCount = 0 Then Exit Sub
Call pSetForegroundWindow(.ItemData(.ListIndex))
End With
End Sub

Private Sub cmdRefresh_Click()
Call pGetTasks
End Sub

Private Sub pGetTasks()
On Error Resume Next

Call fEnumWindows(lstTasks)
lstTasks.ListIndex = 0
End Sub

Private Sub pTerminateProcess(WindowCaption As String)
Dim lHwnd As Long
Dim lProcess As Long
Dim lExitCode As Long

lHwnd = FindWindow(WindowCaption, vbNullString)
Call GetWindowThreadProcessId(lHwnd, lProcess)
lProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, lProcess)
Call GetExitCodeProcess(lProcess, lExitCode)
Call TerminateProcess(lProcess, lExitCode)
End Sub


Can anyone see what i'm doing wrong?
I'm getting stressed out here :)

Many Thanks, D!SiLLUSiON...


[Edited by D!SiLLUSiON on 06-13-2000 at 08:07 AM]

Jun 28th, 2000, 01:50 PM
This is a little late, but refer to this link: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=4377