Solved

Using API to cancel Window Task

Posted on 2000-04-20
15
179 Views
Last Modified: 2010-05-02
How do I use API to find out if a particular task (for example PCAnywhere script) is currently running and is listed in Windows Task List.

Once I do find out that it's running, how do I use API to end it.
0
Comment
Question by:BMezzenga
15 Comments
 

Author Comment

by:BMezzenga
ID: 2733626
By the way, I am talking about Windows 98 and VB6.
Just a bit of extra info.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2733656
does pcanywhere have an open window or does it only show in the task list?



0
 
LVL 3

Expert Comment

by:jetforce
ID: 2733761
Try this
Http://www.mvps.org/vb/code/tasklist.zip


and to kill the process

'Declarations
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10  


PostMessage List1.ItemData(List1.ListIndex), WM_CLOSE, 0, 0
0
 
LVL 2

Expert Comment

by:Sage020999
ID: 2735448
you can use the findwindow api to look for a specific task.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2735491
only if you know the EXACT window title
0
 

Author Comment

by:BMezzenga
ID: 2736743
Sage, I am not looking for only one window but a whole process and also child processes which may spawn from it.

eg. PcAnywhere script may be designed to call other computers in which case it may have other windows open when it's dialing

Jetforce, the web site which you gave me does not work. Can you please check if it's correct.
0
 

Author Comment

by:BMezzenga
ID: 2736747
Adjusted points from 100 to 150
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:AzraSound
ID: 2736755
http://www.winmag.com/columns/powertools/ptwinwatch.htm

that should return all those child windows too.  it includes the window handle so you should be able to terminate processes as needed as well
0
 

Author Comment

by:BMezzenga
ID: 2739822
I am trying to use API because this is something that VB is supposed to do while I am not there.

At a certain time, VB program is meant to check what processes are running and then if it finds a perticular task it's looking for, it's supposed to end it.

I don't need tools to show me as a user how to close tasks, I need the program to do it itself.

That's why I am looking for an API.
0
 

Author Comment

by:BMezzenga
ID: 2739824
Adjusted points from 150 to 200
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2740004
and how do you plan to find this task? by executable name?  by window title? it depends on what info you are going to send the program
0
 

Author Comment

by:BMezzenga
ID: 2741863
OK, I don't know what can be returned from the task list to identify a program.
I am not sure if there is a handle which can be returned or name or whatever.

Therefore my first question is:

Is it possible to get the API to return a list of executables by name as they are listed in the Ctrl+Alt+Del (Task list window).

For Example

PCAnywhere
Explorer
SysTray
Program X
Program Y
Program Z
etc........
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
ID: 2741868
this is a mock task manager program i wrote:



'TASK MANAGER IMITATION
'ADD A LISTBOX AND THREE COMMAND BUTTONS TO THE FORM
'************************************************************************
'
'FORM CODE
'
'************************************************************************

'WIN16/32 Directive
#If Win16 Then
    Declare Function ShowWindow Lib "User" (ByVal hWnd As Integer, ByVal flgs As Integer) As Integer
    Declare Function GetWindow Lib "User" (ByVal hWnd As Integer, ByVal wCmd As Integer) As Integer
    Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer, ByVal wIndx As Integer) As Integer
    Declare Function GetWindowLong Lib "User" (ByVal hWnd As Integer, ByVal wIndx As Integer) As Long
    Declare Function GetWindowText Lib "User" (ByVal hWnd As Integer, ByVal lpSting As String, ByVal nMaxCount As Integer) As Integer
    Declare Function GetWindowTextLength Lib "User" (ByVal hWnd As Integer) As Integer
    Declare Function SetWindowPos Lib "User" (ByVal hWnd As Integer, ByVal insaft As Integer, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal flgs As Integer) As Integer
#Else
    Private Declare Function ShowWindow Lib "User32" (ByVal hWnd As Long, ByVal flgs As Long) As Long
    Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetWindowWord Lib "User32" (ByVal hWnd As Long, ByVal wIndx As Long) As Long
    Private Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal wIndx As Long) As Long
    Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpSting As String, ByVal nMaxCount As Long) As Long
    Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
    Private Declare Function SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal insaft As Long, ByVal x%, ByVal y%, ByVal cx%, ByVal cy%, ByVal flgs As Long) As Long
#End If

Const WS_MINIMIZE = &H20000000
Const HWND_TOP = 0
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_SHOWWINDOW = &H40
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Const GWL_STYLE = (-16)
Const SW_RESTORE = 9
Dim IsTask As Long

Const WS_VISIBLE = &H10000000
Const WS_BORDER = &H800000

Const WS_CLIPSIBLINGS = &H4000000
Const WS_THICKFRAME = &H40000
Const WS_GROUP = &H20000
Const WS_TABSTOP = &H10000

Sub Command3_Click()
    Unload Me
End Sub

Sub Command1_Click()
    FindAllApps
End Sub

Sub Command2_Click()
    Dim hWnd As Long
    Dim x As Long
    Dim lngWW As Long
    If List1.ListIndex < 0 Then Beep: Exit Sub
      hWnd = List1.ItemData(List1.ListIndex)
    lngWW = GetWindowLong(hWnd, GWL_STYLE)
    ' If minimized do a restore
    If lngWW And WS_MINIMIZE Then
        x = ShowWindow(hWnd, SW_RESTORE)
    End If
    ' Move window to top of z-order/activate; no move/resize
    x = SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, _
        SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW)
End Sub

Sub FindAllApps()
    Dim hwCurr As Long
    Dim intLen As Long
    Dim strTitle As String
    ' process all top-level windows in master window list
    List1.Clear
    hwCurr = GetWindow(Me.hWnd, GW_HWNDFIRST)  ' get first window
    Do While hwCurr
        If hwCurr <> Me.hWnd And TaskWindow(hwCurr) Then
            intLen = GetWindowTextLength(hwCurr) + 1
            strTitle = Space$(intLen)
            intLen = GetWindowText(hwCurr, strTitle, intLen)
            If intLen > 0 Then
                List1.AddItem strTitle
                List1.ItemData(List1.NewIndex) = hwCurr
            End If
        End If
        hwCurr = GetWindow(hwCurr, GW_HWNDNEXT)
    Loop
End Sub


Sub Form_Load()
    Command1.Caption = "Refresh"
    Command2.Caption = "Switch"
    Command3.Caption = "Exit"
    IsTask = WS_VISIBLE Or WS_BORDER
    FindAllApps
End Sub

Sub Form_Paint()
    FindAllApps
End Sub


Sub List1_DblClick()
    Command2.Value = True
End Sub

Function TaskWindow(hwCurr As Long) As Long
    Dim lngStyle As Long
    lngStyle = GetWindowLong(hwCurr, GWL_STYLE)
    If (lngStyle And IsTask) = IsTask Then TaskWindow = True
End Function

0
 

Author Comment

by:BMezzenga
ID: 2743782
I'll try it out and I'll let you know how it went. Thanks.
0
 

Author Comment

by:BMezzenga
ID: 2758371
The program you gave me works. I also found anohter one on the net which actually selects a task and terminates it as well.

But your program got me there half way, by displaying the running tasks.

Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now