Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

End Task through code

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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

885 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