Results 1 to 6 of 6

Thread: [RESOLVED] Get TaskBar button text

  1. #1

    Thread Starter
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Resolved [RESOLVED] Get TaskBar button text

    I'm trying to get the taskbar button ID for my App. I only need to do this once, so I figured I'd just loop through buttons, get the caption and compare it to my app's caption. I'm using TB_GETBUTTONTEXT to get the captions, but whilst its returning the length of the caption, it's not populating the buffer.

    Here's the code:
    VB Code:
    1. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    2.     ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
    4.     ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    5. Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    6.     ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    7.  
    8. Public Const WM_USER = &H400
    9. Public Const TB_GETBUTTONTEXT = (WM_USER + 45)
    10.  
    11. Public Function GetAppsID(ByVal sCaption As String) As Long
    12.     Dim lhWnd As Long, N As Long, lLen As Long, sBuff As String
    13.     lhWnd = FindWindow("Shell_TrayWnd", vbNullString)
    14.     lhWnd = FindWindowEx(lhWnd, 0&, "ReBarWindow32", vbNullString)
    15.     lhWnd = FindWindowEx(lhWnd, 0&, "MSTaskSwWClass", vbNullString)
    16.     lhWnd = FindWindowEx(lhWnd, 0&, "ToolbarWindow32", vbNullString)
    17.    
    18.     Do
    19.         lLen = SendMessage(lhWnd, TB_GETBUTTONTEXT, N, Null)
    20.         If lLen = -1 Then Exit Do
    21.         sBuff = Space$(lLen + 1)
    22.         lLen = SendMessage(lhWnd, TB_GETBUTTONTEXT, N, sBuff)
    23.         Debug.Print N, lLen, Trim$(sBuff)
    24.         N = N + 1
    25.     Loop Until N = 50
    26. End Function
    Any ideas?

    be warned: passing the lParam ByVal causes an error in explorer.

  2. #2
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: Get TaskBar button text

    i think you need to use OpenProcess and to allocate some memory for the buffer in the target app (explorer) for this to work. then use SendMessage with a pointer to that buffer and ReadProcessMemory to get the text. and don't forget to free the buffer and close the process when you're done.

  3. #3

    Thread Starter
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Get TaskBar button text

    yeah, that's what thought - ah well, bit annoying.

    Here's the code for anyone interested:
    VB Code:
    1. ' General
    2. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    3.     ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    4.    
    5. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
    6.     ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    7.    
    8. ' Process And Memory
    9. Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
    10.     ByVal hWnd As Long, lpdwProcessId As Long) As Long
    11.    
    12. Private Declare Function OpenProcess Lib "kernel32" ( _
    13.     ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    14.    
    15. Private Declare Function CloseHandle Lib "kernel32" ( _
    16.     ByVal hObject As Long) As Long
    17.    
    18. Private Declare Function VirtualFreeEx Lib "kernel32" ( _
    19.     ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
    20.    
    21. Private Declare Function VirtualAllocEx Lib "kernel32" ( _
    22.     ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, _
    23.     ByVal flProtect As Long) As Long
    24.    
    25. Private Declare Function ReadProcessMemory Lib "kernel32" ( _
    26.     ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, _
    27.     Optional lpNumberOfBytesWritten As Long) As Long
    28.  
    29. Const PROCESS_VM_READ = (&H10)
    30. Const PROCESS_VM_WRITE = (&H20)
    31. Const PROCESS_VM_OPERATION = (&H8)
    32. Const MEM_COMMIT = &H1000
    33. Const MEM_RESERVE = &H2000
    34. Const MEM_RELEASE = &H8000
    35. Const PAGE_READWRITE = &H4
    36.  
    37. Const WM_USER = &H400
    38. Const TB_ISBUTTONHIDDEN = (WM_USER + 12)
    39. Const TB_BUTTONCOUNT = (WM_USER + 24)
    40. Const TB_GETBUTTONTEXTA = (WM_USER + 45)
    41.  
    42. Private Sub Form_Load()
    43.     Dim hTaskBar As Long, pID As Long, hProcess As Long
    44.     Dim N As Long, lCount As Long, lNum As Long, lLen As Long
    45.     Dim sCaption As String * 128, lpCaption As Long
    46.    
    47.     Me.Show
    48.    
    49.     hTaskBar = GetNotificationWindow
    50.     GetWindowThreadProcessId hTaskBar, pID
    51.     hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, 0, pID)
    52.    
    53.     lpCaption = VirtualAllocEx(hProcess, ByVal 0&, Len(sCaption), MEM_COMMIT Or MEM_RESERVE, PAGE_READWRITE)
    54.     lNum = SendMessage(hTaskBar, TB_BUTTONCOUNT, 0, ByVal 0&)
    55.    
    56.     Do Until lCount = lNum
    57.         lLen = SendMessage(hTaskBar, TB_GETBUTTONTEXTA, N, ByVal lpCaption)
    58.         If lLen > -1 Then
    59.             If SendMessage(hTaskBar, TB_ISBUTTONHIDDEN, N, 0&) = 0 Then
    60.                 ReadProcessMemory hProcess, ByVal lpCaption, ByVal sCaption, Len(sCaption)
    61.                 Debug.Print Left$(sCaption, InStr(sCaption, vbNullChar) - 1)
    62.             End If
    63.             lCount = lCount + 1
    64.         End If
    65.         N = N + 1
    66.     Loop
    67.        
    68.     VirtualFreeEx 0, lpCaption, 0, MEM_RELEASE
    69.     CloseHandle hProcess
    70. End Sub
    71.  
    72. Private Function GetNotificationWindow() As Long
    73.     Dim lhWnd As Long
    74.     lhWnd = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString)
    75.     lhWnd = FindWindowEx(lhWnd, 0&, "ReBarWindow32", vbNullString)
    76.     lhWnd = FindWindowEx(lhWnd, 0&, "MSTaskSwWClass", vbNullString)
    77.     GetNotificationWindow = FindWindowEx(lhWnd, 0&, "ToolbarWindow32", vbNullString)
    78. End Function

    modified from this code for getting the processes running in the SysTray.

  4. #4
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: [RESOLVED] Get TaskBar button text

    Ha Ha ! I was trying with the same code, too.

    BTW, looks like for every window, Explorer creates a hidden button. Any light on this ?
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  5. #5

    Thread Starter
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [RESOLVED] Get TaskBar button text

    Quote Originally Posted by iPrank
    BTW, looks like for every window, Explorer creates a hidden button.
    it's because of grouping (even if you haven't got it enabled) - so that it can collate the buttons.

  6. #6
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: [RESOLVED] Get TaskBar button text

    I hate XP.
    2K Pro was the best OS, IMO.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


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