;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; FileName: TrayIcon.asm
; Function: Demo the way to enum the icons in system tray
; Author: Purple Endurer | 紫郢剑侠㊣ (PurpleEndurer@163.com)
; DevEnv: Win2000 pro SP4, MASM32 v8
;
; log
; ----------------------------------------------------------------------------------
; 2007-09-29 Can run under Win XP
; 2007-09-22 Created!
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.386
.model flat, stdcall
option casemap: none
include /masm32/ include/windows.inc
include /masm32/ include/kernel32.inc
includelib /masm32/ lib/kernel32.lib
include /masm32/ include/user32.inc
includelib /masm32/ lib/user32.lib
GetSysTrayToolBarHandle proto
EnumSubCtl proto :HWND, :LPARAM
GetSysTrayIconCount proto
EnumSysTrayIcon proto
.data
g_szAppName db "EnumSysTrayIcon", 0
g_szTaskBarCls db "Shell_TrayWnd", 0
g_szSysPagerCls db "SysPager", 0 ;WinXP need!
g_szTrayNotifyWndCls db "TrayNotifyWnd", 0
g_szToolbarWindow32Cls db "ToolbarWindow32", 0
g_szFailGetSysTray db "Fail to get system tray!", 0
g_hSysTray HANDLE ?
g_dwTrayIconCount dword ?
g_stTbButton TBBUTTON <>
g_szIconText db MAX_PATH dup (?)
.code
start:
invoke GetSysTrayToolBarHandle
test eax, eax ; .if eax==NULL
.if ZERO?
invoke MessageBox, NULL, addr g_szFailGetSysTray, addr g_szAppName, MB_ICONERROR
.else
mov g_hSysTray, eax
invoke EnumSysTrayIcon
.endif
invoke ExitProcess,NULL
;
; Function: Get the handle ToolbarWindow32 of in system tray
; Shell_TrayWnd -> TrayNotifyWnd -> (WinXP:SysPager) -> ToolbarWindow32
; Onput: if fail eax=NULL, else eax = handle
;/
GetSysTrayToolBarHandle proc
;--- Get the handle of task bar
invoke FindWindow, addr g_szTaskBarCls, NULL
cmp eax, NULL
je @GetSysTrayToolBarHandleRet ; fail
; HWND FindWindowEx(
; HWND hwndParent, // handle to parent window
; HWND hwndChildAfter, // handle to a child window
; LPCTSTR lpszClass, // pointer to class name
; LPCTSTR lpszWindow // pointer to window name
; );
;--- Get the handle of TrayNotifyWnd in task bar
invoke FindWindowEx, eax, NULL, addr g_szTrayNotifyWndCls, NULL
cmp eax, NULL
je @GetSysTrayToolBarHandleRet
;--- (WinXP Only) Get the handle of g_szSysPager in TrayNotifyWnd
push eax
invoke FindWindowEx, eax, NULL, addr g_szSysPagerCls, NULL
.if ( eax==NULL)
pop eax
.else
pop edi
.endif
;--- Get the handle of ToolbarWindow32 in TrayNotifyWnd
invoke FindWindowEx, eax, NULL, addr g_szToolbarWindow32Cls, NULL
@GetSysTrayToolBarHandleRet:
ret
GetSysTrayToolBarHandle endp
;/
; Function: Enum the Child window in task bar
;/
EnumSubCtl proc proc hWnd: HWND, lParam: LPARAM
invoke GetClassName, hWnd, addr g_szIconText, sizeof g_szIconText
invoke MessageBox, NULL, addr g_szIconText, addr g_szIconText, MB_OK
mov eax, TRUE
ret
EnumSubCtl endp
;/
; Function: Get the count of icon in system tray
;/
GetSysTrayIconCount proc
invoke SendMessage,g_hSysTray, TB_BUTTONCOUNT, 0, 0
mov g_dwTrayIconCount, eax
ret
GetSysTrayIconCount endp
;///
; Function: Enum the icon in system tray
;///
EnumSysTrayIcon proc
local dwProcID, dwReaded: dword
local hProcess: HANDLE
local pMem: dword
invoke GetSysTrayIconCount
invoke GetWindowThreadProcessId, g_hSysTray, addr dwProcID
invoke OpenProcess, PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, FALSE, dwProcID
mov hProcess, eax
invoke VirtualAllocEx, hProcess, NULL, 1024, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE
mov pMem, eax
xor eax, eax
.while ( eax < g_dwTrayIconCount)
push eax
invoke SendMessage, g_hSysTray, TB_GETBUTTON, eax, pMem
invoke ReadProcessMemory, hProcess, pMem, addr g_stTbButton, sizeof g_stTbButton, addr dwReaded
invoke SendMessage, g_hSysTray, TB_GETBUTTONTEXT, g_stTbButton.idCommand, pMem
inc eax ; If fail, the return value is -1
jz @F ; Fail, skip
invoke ReadProcessMemory, hProcess, pMem, addr g_szIconText, sizeof g_szIconText, addr dwReaded
invoke MessageBox, NULL, addr g_szIconText, addr g_szAppName, NULL
@@:
pop eax
inc eax
.endw
invoke VirtualFreeEx, hProcess, pMem, 0, MEM_RELEASE
invoke CloseHandle, hProcess
ret
EnumSysTrayIcon endp
end start
; FileName: TrayIcon.asm
; Function: Demo the way to enum the icons in system tray
; Author: Purple Endurer | 紫郢剑侠㊣ (PurpleEndurer@163.com)
; DevEnv: Win2000 pro SP4, MASM32 v8
;
; log
; ----------------------------------------------------------------------------------
; 2007-09-29 Can run under Win XP
; 2007-09-22 Created!
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.386
.model flat, stdcall
option casemap: none
include /masm32/ include/windows.inc
include /masm32/ include/kernel32.inc
includelib /masm32/ lib/kernel32.lib
include /masm32/ include/user32.inc
includelib /masm32/ lib/user32.lib
GetSysTrayToolBarHandle proto
EnumSubCtl proto :HWND, :LPARAM
GetSysTrayIconCount proto
EnumSysTrayIcon proto
.data
g_szAppName db "EnumSysTrayIcon", 0
g_szTaskBarCls db "Shell_TrayWnd", 0
g_szSysPagerCls db "SysPager", 0 ;WinXP need!
g_szTrayNotifyWndCls db "TrayNotifyWnd", 0
g_szToolbarWindow32Cls db "ToolbarWindow32", 0
g_szFailGetSysTray db "Fail to get system tray!", 0
g_hSysTray HANDLE ?
g_dwTrayIconCount dword ?
g_stTbButton TBBUTTON <>
g_szIconText db MAX_PATH dup (?)
.code
start:
invoke GetSysTrayToolBarHandle
test eax, eax ; .if eax==NULL
.if ZERO?
invoke MessageBox, NULL, addr g_szFailGetSysTray, addr g_szAppName, MB_ICONERROR
.else
mov g_hSysTray, eax
invoke EnumSysTrayIcon
.endif
invoke ExitProcess,NULL
;
; Function: Get the handle ToolbarWindow32 of in system tray
; Shell_TrayWnd -> TrayNotifyWnd -> (WinXP:SysPager) -> ToolbarWindow32
; Onput: if fail eax=NULL, else eax = handle
;/
GetSysTrayToolBarHandle proc
;--- Get the handle of task bar
invoke FindWindow, addr g_szTaskBarCls, NULL
cmp eax, NULL
je @GetSysTrayToolBarHandleRet ; fail
; HWND FindWindowEx(
; HWND hwndParent, // handle to parent window
; HWND hwndChildAfter, // handle to a child window
; LPCTSTR lpszClass, // pointer to class name
; LPCTSTR lpszWindow // pointer to window name
; );
;--- Get the handle of TrayNotifyWnd in task bar
invoke FindWindowEx, eax, NULL, addr g_szTrayNotifyWndCls, NULL
cmp eax, NULL
je @GetSysTrayToolBarHandleRet
;--- (WinXP Only) Get the handle of g_szSysPager in TrayNotifyWnd
push eax
invoke FindWindowEx, eax, NULL, addr g_szSysPagerCls, NULL
.if ( eax==NULL)
pop eax
.else
pop edi
.endif
;--- Get the handle of ToolbarWindow32 in TrayNotifyWnd
invoke FindWindowEx, eax, NULL, addr g_szToolbarWindow32Cls, NULL
@GetSysTrayToolBarHandleRet:
ret
GetSysTrayToolBarHandle endp
;/
; Function: Enum the Child window in task bar
;/
EnumSubCtl proc proc hWnd: HWND, lParam: LPARAM
invoke GetClassName, hWnd, addr g_szIconText, sizeof g_szIconText
invoke MessageBox, NULL, addr g_szIconText, addr g_szIconText, MB_OK
mov eax, TRUE
ret
EnumSubCtl endp
;/
; Function: Get the count of icon in system tray
;/
GetSysTrayIconCount proc
invoke SendMessage,g_hSysTray, TB_BUTTONCOUNT, 0, 0
mov g_dwTrayIconCount, eax
ret
GetSysTrayIconCount endp
;///
; Function: Enum the icon in system tray
;///
EnumSysTrayIcon proc
local dwProcID, dwReaded: dword
local hProcess: HANDLE
local pMem: dword
invoke GetSysTrayIconCount
invoke GetWindowThreadProcessId, g_hSysTray, addr dwProcID
invoke OpenProcess, PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, FALSE, dwProcID
mov hProcess, eax
invoke VirtualAllocEx, hProcess, NULL, 1024, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE
mov pMem, eax
xor eax, eax
.while ( eax < g_dwTrayIconCount)
push eax
invoke SendMessage, g_hSysTray, TB_GETBUTTON, eax, pMem
invoke ReadProcessMemory, hProcess, pMem, addr g_stTbButton, sizeof g_stTbButton, addr dwReaded
invoke SendMessage, g_hSysTray, TB_GETBUTTONTEXT, g_stTbButton.idCommand, pMem
inc eax ; If fail, the return value is -1
jz @F ; Fail, skip
invoke ReadProcessMemory, hProcess, pMem, addr g_szIconText, sizeof g_szIconText, addr dwReaded
invoke MessageBox, NULL, addr g_szIconText, addr g_szAppName, NULL
@@:
pop eax
inc eax
.endw
invoke VirtualFreeEx, hProcess, pMem, 0, MEM_RELEASE
invoke CloseHandle, hProcess
ret
EnumSysTrayIcon endp
end start