Skip Navigation Links.
Getting the Windows Task List
Language(s):Visual Basic 6.0
Category(s):API

How to get the Window Titles of the active tasks.

' 1) Create a new project. Form1 will be created by default.
' 2) Add a Listbox to the Form. 
' 3) Add a Command Button below the Listbox. 
' 4) Add a Module to the Form. 
' 5) Paste the following code into the declarations section of Form1:

' --- Begin code for Form1

' Getting the Windows Task List
'
' Jon Vote, Idioma Software Inc.
'
' 02/2002
'
' www.idioma-software.com
'
Option Explicit

Private Sub Command1_Click()
  
  Dim i As Integer
    
  Call LoadTasks(Me)
  List1.Clear
  For i = 0 To UBound(g_strWindowTitles)
    If g_strWindowTitles(i) <> "" Then
      List1.AddItem g_strWindowTitles(i)
    End If
  Next i
  
End Sub

Private Sub Form_Load()
  
  Me.Caption = "Getting the Windows Task List"
  Command1.Caption = "&Load"
  
End Sub

' --- End code for Form1 ---

' 6) Paste the following code into Module1.

' --- Begin code for Module1

Option Explicit

'This string will contain the window titles of the active tasks.
Public g_strWindowTitles() As String

Public Const GW_CHILD = 5
Public Const GW_HWNDFIRST = 0
Public Const GW_HWNDLAST = 1
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDPREV = 3
Public Const GW_OWNER = 4

Public Declare Function getWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

Sub LoadTasks(frmStartWindow As Form)
    
' Author Jon Vote
'
' (c) 1998 Idioma Software inc.
'
' www.idioma-software.com
' jon@idoma-software.com
'
'
  Dim intLastWIndowSpace As Integer
  Dim lngLength As Long
  Dim lngEntry As String
  Dim lngRC As Long
  Dim hwnd As Long
  Dim intWindowTitle As Integer
  Dim intLastWindowTitle As Integer

  intLastWIndowSpace = 20
  Erase g_strWindowTitles
  ReDim g_strWindowTitles(intLastWIndowSpace)

  hwnd = getWindow(frmStartWindow.hwnd, GW_HWNDFIRST)
  intWindowTitle = 0
  Do While hwnd <> 0
      lngLength = GetWindowTextLength(hwnd)
      If lngLength > 0 Then
          lngEntry = Space$(lngLength + 1)
          lngRC = GetWindowText(hwnd, lngEntry, lngLength + 1)
          If intWindowTitle = intLastWIndowSpace Then
            intLastWIndowSpace = intLastWIndowSpace + 10
            ReDim Preserve g_strWindowTitles(intLastWIndowSpace)
          End If
          g_strWindowTitles(intWindowTitle) = lngEntry
          intWindowTitle = intWindowTitle + 1
      End If
      hwnd = getWindow(hwnd, GW_HWNDNEXT)
 Loop
 intLastWindowTitle = intWindowTitle - 1

End Sub

' --- End code for Module1---

This article has been viewed 5625 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.