-
I am looking for a way to list task similar to “ps –eaf” in Unix (Solaris). The solution must work in VB5, possibly using an API call. If the list looks like the ctrl-alt-Del list, that would be fine. I have played with "user32" with some code I found see below:
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetParent Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetWindowTextLength Lib _
"user32" Alias "GetWindowTextLengthA" (ByVal hwnd 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
But It seems like there should be an easier way than that, if not easier than cleaner.
Thanks
-
The problem is that the number of windows within Windows at any one time is enormous - typically 5 to 10,000.
You need to weed out of this long list all the windows that are not relevant. I just wanted the last application that the user was using (ALT TAB).
The weeding I use for my purpose is:
- Remove windows with parents
- Remove windows without captions and titles
- Remove windows that are not visible.
- Remove this window.
The code that I used is:
Private Sub Command1_Click()
thisHwnd = GetActiveWindow()
FirstApp = 0
Call EnumWindows(AddressOf EnumWinProc, 0)
MsgBox "The last application we were using was " _
& FirstApp & ", and has a Title of " & FirstAppTitle
End Sub
And this calls the code module consisting of:
Option Explicit
Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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
Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Declare Function GetActiveWindow Lib "user32" () As Long
Public thisHwnd As Long
Public FirstApp As Long
Public FirstAppTitle As String
Public Const GWL_STYLE = -16
Public Const WS_CAPTION = &HC00000
Public Const MAX_LEN = 260
Public Function EnumWinProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim lRet, lWindowStyle, lWindowParent As Long
Dim strBuffer As String
'Only keep windows that are visible,
' don't have parents, have a title bar and a caption,
' and are not this application, and are the first application found
If hWnd <> thisHwnd Then ' Not this app
If IsWindowVisible(hWnd) Then ' Window visible
lWindowParent = GetParent(hWnd)
If lWindowParent = 0 Then ' No parents
lWindowStyle = GetWindowLong(hWnd, GWL_STYLE)
If (lWindowStyle And WS_CAPTION) Then ' Has a caption
strBuffer = Space(MAX_LEN)
lRet = GetWindowText(hWnd, strBuffer, Len(strBuffer))
If lRet Then ' Has a title
' If you want a list, this is where to put strBuffer into a list box
If FirstApp = 0 Then
FirstApp = hWnd
FirstAppTitle = strBuffer
End If
End If
End If
End If
End If
End If
EnumWinProc = 1
End Function