Solved

End Task through code

Posted on 1998-11-17
1
150 Views
Last Modified: 2010-05-03
Is there a way through code to end a specific task that would show up when you hit Alt-Tab
0
Comment
Question by:newyorker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
MrOBrian earned 20 total points
ID: 1445219
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
'Declare constants used by GetWindow.
Global Const GW_CHILD = 5
Global Const GW_HWNDFIRST = 0
Global Const GW_HWNDLAST = 1
Global Const GW_HWNDNEXT = 2
Global Const GW_HWNDPREV = 3
Global Const GW_OWNER = 4

 Dim CurrWnd As Long
 Dim Length As Long
 Dim ListItem As String
 Dim s As String
 Dim Handle As Long
 Const NILL = 0&
 Const WM_SYSCOMMAND = &H112
 Const SC_CLOSE = &HF060&
 
 CurrWnd = GetWindow(frmBanner.hwnd, GW_HWNDFIRST)
 While CurrWnd <> 0
  Length = GetWindowTextLength(CurrWnd)
  ListItem = Space(Length + 1)
  Length = GetWindowText(CurrWnd, ListItem, Length + 1)
  If Length > 0 Then
   If InStr(LCase(ListItem), "Explorer") <> 0 Then
    '* Post a message to the bad program to end its existence.
    Handle = SendMessage(CurrWnd, WM_SYSCOMMAND, SC_CLOSE, NILL)
   End If
  End If
  CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
  DoEvents
 Wend

Be careful of line wrapping in the declares...
The above code will get a task list and shows how to kill the Explorer, though I wouldn't really recommend it...  I actually use it to kill various other programs.  The While Wend loop will return a whole bunch of tasks that are running.  As far as I can tell, you'll get all the same tasks as if you hit Ctrl-Alt-Del, plus a bunch of internal Windows stuff.
I think I got all the constants and declares, but if any are missing, please let me know.  I just grabbed this from the source of a program I am writing, but I hope I got everything.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question