|
-
Nov 6th, 1999, 09:47 AM
#1
Thread Starter
New Member
Ok, here is the code I'm using to display all the current tasks running on my computer...
Private Sub List_Click()
LoadTaskList
End Sub
Sub LoadTaskList()
Dim CurrWnd As Long
Dim Length As Long
Dim TaskName As String
Dim Parent As Long
List1.Clear
CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
While CurrWnd <> 0
Parent = GetParent(CurrWnd)
Length = GetWindowTextLength(CurrWnd)
TaskName = Space$(Length + 1)
Length = GetWindowText(CurrWnd, TaskName, Length + 1)
TaskName = Left$(TaskName, Len(TaskName) - 1)
If Length > 0 Then
If TaskName <> Me.Caption Then
List1.AddItem TaskName
End If
End If
CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
DoEvents
Wend
End Sub
I want to be able to click on a command button and have it close the app or window that's selected in the Listbox. How do I do that, it may seem simple to you, but could someone please help me out.
thanks
-
Nov 6th, 1999, 10:03 AM
#2
If you have the Window Handle, (Which you do), there are 2 Methods you can use; Close and Terminate. Close will work most of the Time, but for those more Stubourn Apps you may need to tell it to Terminate, here's a couple of Subs I put together you can call to do the job:
In a Module..
Code:
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Public Sub ForceAppClosed(ByVal lHwnd As Long)
'Force the App to Shutdown
Dim lPID As Long
Dim lCode As Long
'Get the Windows ProcessID
Call GetWindowThreadProcessId(lHwnd, lPID)
'Get a Handle to the Process
lPID = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPID)
'Get the Processes Exit Code
Call GetExitCodeProcess(lPID, lCode)
'Tell the Process to Terminate
Call TerminateProcess(lPID, lCode)
End Sub
Public Sub CloseApp(ByVal lHwnd As Long)
'Ask the App to Shutdown Normally
Call SendMessage(lHwnd, WM_CLOSE, 0, 0)
End Sub
Usage: CloseApp WindowHandle
or.. ForceAppClosed WindowHandle
------------------
Aaron Young
Analyst Programmer
[email protected]
[email protected]
-
Nov 6th, 1999, 01:55 PM
#3
Member
I would not recommend either of these solutions. Instead, use the following:
Code:
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
PostMessage (hWnd), WM_CLOSE, 0&, 0&
Hope this helps!
------------------
(¯`·.¸¸.·´¯`·->ShadowCrawler<-·´¯`·.¸¸.·´¯)
Teenage Programmer
Visual Basic, HTML, C++, JavaScript
http://welcome.to/X12Tech
Email: [email protected]
ICQ#: 9872708
-
Nov 6th, 1999, 02:11 PM
#4
Thread Starter
New Member
Thanks to both of you guys, I think I'll try both methods and see which one seems to work better and/or is more efficient.
Those responses were right quick, keep up the good work !
: )
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
|