Solved

End Task through code

Posted on 1998-11-17
1
144 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
1 Comment
 

Accepted Solution

by:
MrOBrian earned 20 total points
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now