win7 64获取托盘图标句柄错误

careysucci 2015-12-28 01:02:47
通过下面方法获取托盘图标的句柄是错误的,返回值不是托盘图标的句柄,
请问大神为什么获取到的托盘图标句柄错了? 谢谢


def find_notify_icon(self,titleParam,hwnd):
'''
win32 is vi
:param titleParam:
:param hwnd:
:return:
'''
lpBuffer = LPVOID()
ipHandle = LPVOID()
lpdwProcessId = c_ulong()
nStrOffset = sizeof(TBBUTTON) - sizeof(INT_PTR)
hwndOffset = sizeof(INT_PTR)

if self.is_run_on_win64():
# 当前进程是32位进程,字节是以4字节对齐
# 在64位系统上,托盘图标区域隶属于explorer资源管理器进程,是64位进程,在该进程中
# TBBUTTON中的各字段的长度都变长了,所以要针对64位情况计算偏移(要考虑不同系统中
# 的字节对齐问题,TBBUTTON结构体在64位进程中是8字节对齐
nStrOffset += 4 # bReserved在win64下多出来的4字节
nStrOffset += 4 # DWORD_PTR在win64下多出来的4字节
hwndOffset += 4

baseWin32.GetWindowThreadProcessId(hwnd,byref(lpdwProcessId))
if lpdwProcessId == 0:return ipHandle
hProcess = baseWin32.OpenProcess(processAccessRights['PROCESS_ALL_ACCESS']|processAccessRights['PROCESS_VM_OPERATION']|processAccessRights['PROCESS_VM_READ']|processAccessRights['PROCESS_VM_WRITE'],
False,
lpdwProcessId.value
)
lpAddress = baseWin32.VirtualAllocEx(hProcess,0,4096,memoryAllocation['MEM_COMMIT'],memoryProtectionConstants['PAGE_EXECUTE_READWRITE'])
countButton = baseWin32.SendMessage(hwnd,TB_BUTTONCOUNT,0,0)
for each in range(countButton):
tbbutton = TBBUTTON()
trydata = TRAYDATA()
baseWin32.SendMessage(hwnd,TB_GETBUTTON,each,LPARAM(lpAddress))
#读文本地址
baseWin32.ReadProcessMemory(hProcess,LPCVOID(lpAddress + nStrOffset),byref(lpBuffer),sizeof(lpBuffer),None)
if lpBuffer != -1:
buff = (1024*LPVOID)()
#读文本
baseWin32.ReadProcessMemory(hProcess,lpBuffer,buff,1024,None)
#buff为unicode,转换成字符串:title
title = wstring_at(buff)
print 'title:%s' %title
nullindex = title.find('\0')
if nullindex >0:
title = title[0:nullindex]
if title == titleParam:
ipHandleAdr = LPVOID(0)
szTips = (TCHAR*1024)()
#读句柄地址
baseWin32.ReadProcessMemory(hProcess,LPVOID(int(lpAddress)+40),byref(ipHandleAdr),sizeof(ipHandleAdr),None)
# print ipHandleAdr
baseWin32.ReadProcessMemory(hProcess,ipHandleAdr,byref(ipHandle),sizeof(ipHandle),None)

print 'ipHandle:%r' %trydata.uID
print 'ipHandle1:%r' %trydata.szExePath
print 'ipHandle2:%r' %trydata.hWnd
print 'ipHandle3:%r' %tbbutton.dwData
print 'ipHandle4:%r' %ipHandle.value
break
baseWin32.VirtualFreeEx(hProcess,lpAddress,4096,memoryFreeType['MEM_RELEASE'])
baseWin32.CloseHandle(hProcess)
return ipHandle.value

...全文
503 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-12-29
  • 打赏
  • 举报
回复
获取子窗口句柄使用API FindWindowEx FindWindowEx The FindWindowEx function retrieves a handle to a window whose class name and window name match the specified strings. The function searches child windows, beginning with the one following the given child window. This function does not perform a case-sensitive search. 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 ); Parameters hwndParent Handle to the parent window whose child windows are to be searched. If hwndParent is NULL, the function uses the desktop window as the parent window. The function searches among windows that are child windows of the desktop. Windows NT 5.0 and later: If hwndParent is HWND_MESSAGE, the function searches all message-only windows. hwndChildAfter Handle to a child window. The search begins with the next child window in the Z order. The child window must be a direct child window of hwndParent, not just a descendant window. If hwndChildAfter is NULL, the search begins with the first child window of hwndParent. Note that if both hwndParent and hwndChildAfter are NULL, the function searches all top-level and message-only windows. lpszClass Pointer to a null-terminated string that specifies the class name or is an atom that identifies the class-name string. If this parameter is an atom, it must be a global atom created by a previous call to theGlobalAddAtom function. The atom, a 16-bit value, must be placed in the low-order word of lpszClass; the high-order word must be zero. lpszWindow Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match. Return Values If the function succeeds, the return value is a handle to the window that has the specified class and window names. If the function fails, the return value is NULL. To get extended error information, callGetLastError. QuickInfo Windows NT: Requires version 4.0 or later. Windows: Requires Windows 95 or later. Windows CE: Unsupported. Header: Declared in winuser.h. Import Library: Use user32.lib. Unicode: Implemented as Unicode and ANSI versions on Windows NT. See Also Windows Overview, Window Functions, EnumWindows, FindWindow, GetClassName,GlobalAddAtom
赵4老师 2015-12-29
  • 打赏
  • 举报
回复
托盘图标不是窗口?
careysucci 2015-12-29
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
获取子窗口句柄使用API FindWindowEx

FindWindowEx
The FindWindowEx function retrieves a handle to a window whose class name and window name match the specified strings. The function searches child windows, beginning with the one following the given child window. This function does not perform a case-sensitive search.

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
);

Parameters
hwndParent
Handle to the parent window whose child windows are to be searched.
If hwndParent is NULL, the function uses the desktop window as the parent window. The function searches among windows that are child windows of the desktop.

Windows NT 5.0 and later: If hwndParent is HWND_MESSAGE, the function searches all message-only windows.

hwndChildAfter
Handle to a child window. The search begins with the next child window in the Z order. The child window must be a direct child window of hwndParent, not just a descendant window.
If hwndChildAfter is NULL, the search begins with the first child window of hwndParent.

Note that if both hwndParent and hwndChildAfter are NULL, the function searches all top-level and message-only windows.

lpszClass
Pointer to a null-terminated string that specifies the class name or is an atom that identifies the class-name string. If this parameter is an atom, it must be a global atom created by a previous call to theGlobalAddAtom function. The atom, a 16-bit value, must be placed in the low-order word of lpszClass; the high-order word must be zero.
lpszWindow
Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.
Return Values
If the function succeeds, the return value is a handle to the window that has the specified class and window names.

If the function fails, the return value is NULL. To get extended error information, callGetLastError.

QuickInfo
Windows NT: Requires version 4.0 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.

See Also
Windows Overview, Window Functions, EnumWindows, FindWindow, GetClassName,GlobalAddAtom


是找这些托盘图标的句柄,用findWindowEx找不到的
careysucci 2015-12-28
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
推荐使用Spy4win软件生成提取指定窗口句柄的C++代码片断。
谢谢,试用了下生成的代码没有包含获取托盘图标句柄的代码
赵4老师 2015-12-28
  • 打赏
  • 举报
回复
推荐使用Spy4win软件生成提取指定窗口句柄的C++代码片断。

2,586

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 资源
社区管理员
  • 资源
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧