Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

End Task through code

Posted on 1998-11-17
1
Medium Priority
?
151 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 80 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

718 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