Link to home
Start Free TrialLog in
Avatar of lbowers
lbowersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Determining what apps are currently running...

I need to know what applications are currently running in Winodws at the point my VB6 app executes. Is it possible to retrieve a list of all resident programs and perhaps even retrieve a little information from each of them (Window captions etc).
ASKER CERTIFIED SOLUTION
Avatar of waty
waty
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lbowers

ASKER

First of all your answer is superb, but before I close this question I would like to know if it is possible to retrieve the windows captions of any processes/applications that have interfaces. I need to extract some information from the applications title bar... Thanks again...
' #VBIDEUtils#************************************************************
' * Programmer Name  : FreeVBCode
' * Web Site         : http://www.freevbcode.com/ShowCode.Asp?ID=487
' * E-Mail           : waty.thierry@usa.net
' * Date             : 17/11/1999
' * Time             : 09:52
' **********************************************************************
' * Comments         : Retrieve the Captions of All Open Top-Level Windows
' *
' *
' **********************************************************************
Option Explicit

Private Declare Function EnumWindows Lib "user32" _
   (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Private Declare Function GetWindowText Lib "user32" Alias _
   "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
   ByVal cch As Long) As Long

Private asTopLevelWindows() As String

Public Function TopLevelWindows() As String()

   'PURPOSE: RETURNS AN ARRAY OF TOPLEVEL WINDOWS
   'REQUIRES: VB 6
   'SAMPLE:
   'Dim sArray() As String
   'Dim iCtr As Integer

   'sArray = TopLevelWindows
   'For iCtr = 0 To UBound(sArray)
   'Debug.Print sArray(iCtr)
   'Next

   ReDim asTopLevelWindows(0) As String
   Dim lRet As Long
   lRet = EnumWindows(AddressOf AddElement, 0)
   TopLevelWindows = asTopLevelWindows
End Function

Private Function AddElement(ByVal lhWnd As Long, ByVal lParam As Long) As Long

   Dim sTitle As String
   Dim lRet As Long
   Dim iNew As Integer

   sTitle = Space(255)
   lRet = GetWindowText(lhWnd, sTitle, 255)

   sTitle = StripNull(sTitle)

   If sTitle <> "" Then
      If asTopLevelWindows(0) = "" Then
         iNew = 0
      Else
         iNew = UBound(asTopLevelWindows) + 1
         ReDim Preserve asTopLevelWindows(iNew) As String
      End If

      asTopLevelWindows(iNew) = sTitle

   End If

   AddElement = True

End Function

Private Function StripNull(ByVal InString As String) As String

   'Input: String containing null terminator (Chr(0))
   'Returns: all character before the null terminator

   Dim iNull As Integer
   If Len(InString) > 0 Then
      iNull = InStr(InString, vbNullChar)
      Select Case iNull
         Case 0
            StripNull = InString
         Case 1
            StripNull = ""
         Case Else
            StripNull = Left$(InString, iNull - 1)
      End Select
   End If

End Function
Avatar of lbowers

ASKER

Fantastic - I've upped the points to 150 as a thank you. Cheers.
Avatar of lbowers

ASKER

When running the "TopLevelWindows" exmaple code I get a "Can't Assign To Array" error on the line:

sArray = TopLevelWindows

What's wrong?
   
If I remember well, you need VB6.
otherwise, you can pass the array as a parameter,
or declaring like this

Public Function TopLevelWindows() As Variant
Avatar of lbowers

ASKER

I am using Visual Basic 6 SP3...
This is strange

pass the array as a parameter,
or declaring like this

Public Function TopLevelWindows() As Variant
Avatar of lbowers

ASKER

Waty,

Thanks again for your help.
I'd actually made a stupid error - I caled my .BAS file the same name as the function TopLevelWindows !!!

Sorry...