|
-
Jun 23rd, 2006, 09:12 PM
#1
[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:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public 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
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_USER = &H400
Public Const TB_GETBUTTONTEXT = (WM_USER + 45)
Public Function GetAppsID(ByVal sCaption As String) As Long
Dim lhWnd As Long, N As Long, lLen As Long, sBuff As String
lhWnd = FindWindow("Shell_TrayWnd", vbNullString)
lhWnd = FindWindowEx(lhWnd, 0&, "ReBarWindow32", vbNullString)
lhWnd = FindWindowEx(lhWnd, 0&, "MSTaskSwWClass", vbNullString)
lhWnd = FindWindowEx(lhWnd, 0&, "ToolbarWindow32", vbNullString)
Do
lLen = SendMessage(lhWnd, TB_GETBUTTONTEXT, N, Null)
If lLen = -1 Then Exit Do
sBuff = Space$(lLen + 1)
lLen = SendMessage(lhWnd, TB_GETBUTTONTEXT, N, sBuff)
Debug.Print N, lLen, Trim$(sBuff)
N = N + 1
Loop Until N = 50
End Function
Any ideas?
be warned: passing the lParam ByVal causes an error in explorer.
-
Jun 24th, 2006, 05:54 AM
#2
Lively Member
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.
-
Jun 24th, 2006, 07:44 AM
#3
Re: Get TaskBar button text
yeah, that's what thought - ah well, bit annoying.
Here's the code for anyone interested:
VB Code:
' General
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
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
' Process And Memory
Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" ( _
ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32" ( _
ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32" ( _
ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, _
ByVal flProtect As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" ( _
ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, _
Optional lpNumberOfBytesWritten As Long) As Long
Const PROCESS_VM_READ = (&H10)
Const PROCESS_VM_WRITE = (&H20)
Const PROCESS_VM_OPERATION = (&H8)
Const MEM_COMMIT = &H1000
Const MEM_RESERVE = &H2000
Const MEM_RELEASE = &H8000
Const PAGE_READWRITE = &H4
Const WM_USER = &H400
Const TB_ISBUTTONHIDDEN = (WM_USER + 12)
Const TB_BUTTONCOUNT = (WM_USER + 24)
Const TB_GETBUTTONTEXTA = (WM_USER + 45)
Private Sub Form_Load()
Dim hTaskBar As Long, pID As Long, hProcess As Long
Dim N As Long, lCount As Long, lNum As Long, lLen As Long
Dim sCaption As String * 128, lpCaption As Long
Me.Show
hTaskBar = GetNotificationWindow
GetWindowThreadProcessId hTaskBar, pID
hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, 0, pID)
lpCaption = VirtualAllocEx(hProcess, ByVal 0&, Len(sCaption), MEM_COMMIT Or MEM_RESERVE, PAGE_READWRITE)
lNum = SendMessage(hTaskBar, TB_BUTTONCOUNT, 0, ByVal 0&)
Do Until lCount = lNum
lLen = SendMessage(hTaskBar, TB_GETBUTTONTEXTA, N, ByVal lpCaption)
If lLen > -1 Then
If SendMessage(hTaskBar, TB_ISBUTTONHIDDEN, N, 0&) = 0 Then
ReadProcessMemory hProcess, ByVal lpCaption, ByVal sCaption, Len(sCaption)
Debug.Print Left$(sCaption, InStr(sCaption, vbNullChar) - 1)
End If
lCount = lCount + 1
End If
N = N + 1
Loop
VirtualFreeEx 0, lpCaption, 0, MEM_RELEASE
CloseHandle hProcess
End Sub
Private Function GetNotificationWindow() As Long
Dim lhWnd As Long
lhWnd = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString)
lhWnd = FindWindowEx(lhWnd, 0&, "ReBarWindow32", vbNullString)
lhWnd = FindWindowEx(lhWnd, 0&, "MSTaskSwWClass", vbNullString)
GetNotificationWindow = FindWindowEx(lhWnd, 0&, "ToolbarWindow32", vbNullString)
End Function
modified from this code for getting the processes running in the SysTray.
-
Jun 24th, 2006, 08:21 AM
#4
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 ?
-
Jun 24th, 2006, 08:24 AM
#5
Re: [RESOLVED] Get TaskBar button text
 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.
-
Jun 24th, 2006, 10:06 AM
#6
Re: [RESOLVED] Get TaskBar button text
I hate XP. 
2K Pro was the best OS, IMO.
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
|