lbowers
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
' * 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
ASKER
Fantastic - I've upped the points to 150 as a thank you. Cheers.
ASKER
When running the "TopLevelWindows" exmaple code I get a "Can't Assign To Array" error on the line:
sArray = TopLevelWindows
What's wrong?
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
otherwise, you can pass the array as a parameter,
or declaring like this
Public Function TopLevelWindows() As Variant
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
pass the array as a parameter,
or declaring like this
Public Function TopLevelWindows() As Variant
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...
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...
http://www.mvps.org/vbnet/code/system/toolhelpprocesses.htm
http://support.microsoft.com/support/kb/articles/Q192/9/86.asp
http://support.microsoft.com/support/kb/articles/q187/9/13.asp
Shows how to determine and display Windows' Alt+Tab order.
http://www.mvps.org/vb/samples.htm#AltTab