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

End Task through code

Posted on 1998-11-17
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
Question by:newyorker
1 Comment

Accepted Solution

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)

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.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to skip loop 6 64
Windows 10 start screen issues 9 55
Sorting multiple rows and columns, and count duplicates in Excel 2013 4 87
Passing a Text Box name to a Sub 6 99
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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 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…

839 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