Results 1 to 9 of 9

Thread: List Open Apps...

  1. #1

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125

    Question 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
    <removed by admin>

  2. #2
    Tygur
    Guest
    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:
    1. 'This is the API declaration
    2. 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
    3.  
    4. 'This code will blindly throw all the hWnd's in a string and display the string
    5. Dim lHwnd As Long
    6. Dim lReply As Long
    7. Dim sHwnds As String
    8. lHwnd = FindWindowEx(0, 0, vbNullString, vbNullString)
    9. Do Until lHwnd = 0
    10.     sHwnds = sHwnds & Hex(lHwnd) & " "
    11.     lHwnd = FindWindowEx(0, lHwnd, vbNullString, vbNullString)
    12. Loop
    13. MsgBox sHwnds

  3. #3

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    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
    <removed by admin>

  4. #4
    Tygur
    Guest
    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:
    1. Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    2.  
    3. Public Hwnds() As Long
    4. Public HwndsCount As Long
    5.  
    6. Public Function EnumWindowsProc(ByVal hwnd&, ByVal lParam&) As Long
    7. HwndsCount = HwndsCount + 1
    8. ReDim Preserve Hwnds(HwndsCount)
    9. Hwnds(HwndsCount) = hwnd
    10. EnumWindowsProc = 1
    11. End Function

    Put this in a form with a command button:
    VB Code:
    1. Private Sub Command1_Click()
    2. Erase Hwnds
    3. HwndsCount = -1
    4. 'By the way, the zero at the end gets passed into
    5. 'the EnumWindowsProc function as lParam
    6. EnumWindows AddressOf EnumWindowsProc, 0
    7.  
    8. 'All the handles are in the array
    9. 'Now display them
    10. Dim vHwnd, sHwnds As String
    11. For Each vHwnd In Hwnds
    12.     sHwnds = sHwnds & Hex(vHwnd) & " "
    13. Next vHwnd
    14. MsgBox sHwnds
    15. End Sub

  5. #5
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    How can I get the names of them all??

    Thanks,
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  6. #6
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    You can list all the applications in the taskbar (and their captions) using the EventVB.dll thus:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim WithEvents vbLink As EventVB.APIFunctions
    4.  
    5.  
    6. Private Sub Form_Load()
    7.  
    8. Dim wndThis As ApiWindow
    9.  
    10. Set vbLink = New APIFunctions
    11.  
    12. For Each wndThis In vbLink.System.TopLevelWindows
    13.     If wndThis.WindowText > "" And wndThis.IsWindowStyleSet(WS_EX_APPWINDOW, True) Then
    14.         Debug.Print wndThis.WindowText
    15.     End If
    16. Next wndThis
    17.  
    18. End Sub

    Hope this helps,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  7. #7
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    thanks, will try it out

    but how do I do it without event32.dll??

    Thanks in advance,
    /: Tim :\____________________
    \: VB, HTML, ASP, VBScript, QBASIC, JavaScript :/

  8. #8
    Fanatic Member tim_l_012's Avatar
    Join Date
    Mar 2001
    Location
    Next to a Coffee Cup.
    Posts
    641
    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 :/

  9. #9
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    what is this?
    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
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width