|
-
Jan 11th, 2002, 11:12 PM
#1
Thread Starter
PowerPoster
List Open Apps...
Is there a way to get the Handles for all the applications in the Taskbar? I have searched, and the only thing I can find was code to get the filename of all the files that were running. I just want to list all the program Classnames in a list box, then when the user clicks the item in the listbox, it will use FindWindow to find the handle of the window, and send a message to it using SendMessage. I've got the find and sending part done, I just need to list the programs running.
Thanks
-Joey
-
Jan 12th, 2002, 12:23 AM
#2
There are two ways. You can use FindWindowEx to loop through and get each window, or you can use EnumWindows with a callback function to get them all.
You should be able to find information about them in the MSDN Library. Let me know if you want the EnumWindows code. The EnumWindows code is more reliable when windows switch focus while the loop is going. The FindWindowEx code is posted below because it's easier to do:
VB Code:
'This is the API declaration
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
'This code will blindly throw all the hWnd's in a string and display the string
Dim lHwnd As Long
Dim lReply As Long
Dim sHwnds As String
lHwnd = FindWindowEx(0, 0, vbNullString, vbNullString)
Do Until lHwnd = 0
sHwnds = sHwnds & Hex(lHwnd) & " "
lHwnd = FindWindowEx(0, lHwnd, vbNullString, vbNullString)
Loop
MsgBox sHwnds
-
Jan 12th, 2002, 12:54 AM
#3
Thread Starter
PowerPoster
Thanks Tygur. That code is pretty good. How much more difficult is the EnumWindow code? I can use the code here if you don't want to post the EnumWindow snippet, or if you don't have it, you don't need to go looking for it. This is good enough.
Thanks again
-
Jan 12th, 2002, 01:20 AM
#4
Most of the code I post is made on the spot (including this code). The EnumWindows code just takes a little longer to write and I try to make these posts as quick as I could so people don't post by the time I'm done.
Put this in a Module:
VB Code:
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Hwnds() As Long
Public HwndsCount As Long
Public Function EnumWindowsProc(ByVal hwnd&, ByVal lParam&) As Long
HwndsCount = HwndsCount + 1
ReDim Preserve Hwnds(HwndsCount)
Hwnds(HwndsCount) = hwnd
EnumWindowsProc = 1
End Function
Put this in a form with a command button:
VB Code:
Private Sub Command1_Click()
Erase Hwnds
HwndsCount = -1
'By the way, the zero at the end gets passed into
'the EnumWindowsProc function as lParam
EnumWindows AddressOf EnumWindowsProc, 0
'All the handles are in the array
'Now display them
Dim vHwnd, sHwnds As String
For Each vHwnd In Hwnds
sHwnds = sHwnds & Hex(vHwnd) & " "
Next vHwnd
MsgBox sHwnds
End Sub
-
Jun 3rd, 2002, 06:13 AM
#5
Fanatic Member
How can I get the names of them all??
Thanks,
/: Tim :\____________________
\: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/
-
Jun 3rd, 2002, 06:27 AM
#6
Frenzied Member
You can list all the applications in the taskbar (and their captions) using the EventVB.dll thus:
VB Code:
Option Explicit
Dim WithEvents vbLink As EventVB.APIFunctions
Private Sub Form_Load()
Dim wndThis As ApiWindow
Set vbLink = New APIFunctions
For Each wndThis In vbLink.System.TopLevelWindows
If wndThis.WindowText > "" And wndThis.IsWindowStyleSet(WS_EX_APPWINDOW, True) Then
Debug.Print wndThis.WindowText
End If
Next wndThis
End Sub
Hope this helps,
Duncan
-
Jun 3rd, 2002, 06:31 AM
#7
Fanatic Member
thanks, will try it out
but how do I do it without event32.dll??
Thanks in advance,
/: Tim :\____________________
\: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/
-
Jun 3rd, 2002, 06:35 AM
#8
Fanatic Member
what is this?
Code:
Public Function GetWindowText(ByVal m_hwnd As Long) As String
Dim lret As Long
Dim sRet As String
lret = SendMessageLong(m_hwnd, WM_GETTEXTLENGTH, 0, 0)
If lret > 0 Then
sRet = String$(lret + 1, 0)
lret = SendMessageByString(m_hwnd, WM_GETTEXT, Len(sRet), sRet)
'\\ Returns length up to NULL terminator
If lret > 0 Then
sRet = Left$(sRet, lret)
End If
ElseIf Err.LastDllError > 0 Then
ReportError Err.LastDllError, "ApiWindow:WindowText", GetLastSystemError
Else
sRet = String$(GetWindowTextLength(m_hwnd), 0)
lret = GetWindowTextApi(m_hwnd, sRet, Len(sRet))
sRet = Left$(sRet, lret)
End If
GetWindowText = sRet
End Function
/: Tim :\____________________
\: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/
-
Jun 3rd, 2002, 06:54 AM
#9
Frenzied Member
Code to return the window text of any window. If the window is a top level window then this returns it's captions.
The API calls required are: SendMessage, GetWindowTextLength and GetWindowText
Hope this helps,
Duncan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|