【windows】获取托盘图标信息
win11 22261.1413版本更新后,原来的获取托盘的应用程序信息功能失效了,这里的demo参考了这边文章,加上一个遍历窗口逻辑,来试图找到新版本的托盘图标信息应该从哪个窗口中获取出来。
// Test_Console_3.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <windows.h>
#include <string>
#include <commctrl.h>
#include <atlstr.h>
using namespace std;
// 判断 x64 系统
BOOL Is64bitSystem()
{
SYSTEM_INFO si;
GetNativeSystemInfo(&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
return TRUE;
else
return FALSE;
}
// 获取托盘窗口句柄
HWND FindTrayWnd()
{
HWND hWnd = NULL;
hWnd = FindWindow(_T("Shell_TrayWnd"), NULL);
hWnd = FindWindowEx(hWnd, NULL, _T("TrayNotifyWnd"), NULL);
hWnd = FindWindowEx(hWnd, NULL, _T("SysPager"), NULL);
hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);
return hWnd;
}
// 获取折叠托盘窗口句柄
HWND FindNotifyIconOverflowWindow()
{
HWND hWnd = NULL;
hWnd = FindWindow(_T("NotifyIconOverflowWindow"), NULL);
hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);
return hWnd;
}
std::string WStringToString(const std::wstring& wstr)
{
int len = static_cast<int>(wstr.length());
int resultLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), len, nullptr, 0, nullptr, nullptr);
if (resultLen == 0) {
return "";
}
std::string value(resultLen, '\0');
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), len, const_cast<LPSTR>(value.c_str()),
resultLen, nullptr, nullptr);
return value;
}
//
//char* wideCharToMultiByte(const wchar_t* pWCStrKey)
//{
// //第一次调用确认转换后单字节字符串的长度,用于开辟空间
// int pSize = WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), NULL, 0, NULL, NULL);
// char* pCStrKey = new char[pSize + 1];
// //第二次调用将双字节字符串转换成单字节字符串
// WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), pCStrKey, pSize, NULL, NULL);
// pCStrKey[pSize] = '\0';
// return pCStrKey;
//
// //如果想要转换成string,直接赋值即可
// //string pKey = pCStrKey;
//}
// 遍历窗口
BOOL EnumNotifyWindow(HWND hWnd)
{
// 获取托盘进程ID
DWORD dwProcessId = 0;
GetWindowThreadProcessId(hWnd, &dwProcessId);
if (dwProcessId == 0) {
cout << "GetWindowThreadProcessId failed:" << GetLastError() << endl;
return FALSE;
}
HANDLE hProcess;
LPVOID p_tbbutton = NULL;
bool result = true;
do {
// 获取托盘进程句柄
hProcess = OpenProcess(
PROCESS_VM_OPERATION | // 需要在进程的地址空间上执行操作
PROCESS_VM_READ | // 需要使用 ReadProcessMemory 读取进程中的内存
PROCESS_VM_WRITE, // 需要在使用 WriteProcessMemory 的进程中写入内存
FALSE, // 子进程不继承句柄
dwProcessId // 目标进程 PID
);
if (hProcess == NULL) {
cout << "OpenProcess failed:" << GetLastError() << endl;
result = false;
break;
}
// 在进程虚拟空间中分配内存,用来接收 TBBUTTON 结构体指针
p_tbbutton = VirtualAllocEx(
hProcess, // 目标进程句柄
0, // 内存起始地址(默认)
4096, // 内存大小
MEM_COMMIT, // 内存类型(提交)
PAGE_EXECUTE_READWRITE // 内存保护属性(可读可写可执行)
);
if (p_tbbutton == NULL) {
cout << "VirtualAllocEx failed:" << GetLastError() << endl;
result = false;
break;
}
// 初始化
DWORD dw_addr_dwData = 0;
BYTE buff[1024] = { 0 };
wstring ws_filePath = L"";
wstring ws_tile = L"";
HWND h_mainWnd = NULL;
int i_data_offset = 12;
int i_str_offset = 18;
// 判断 x64
if (Is64bitSystem()) {
i_data_offset += 4;
i_str_offset += 6;
}
// 获取托盘图标个数
DWORD_PTR i_buttons = 0;
int ret = SendMessageTimeout(hWnd, TB_BUTTONCOUNT, 0, 0, SMTO_ABORTIFHUNG, 2000, &i_buttons);
if (!ret) {
cout << "Send msg timeout" << endl;
break;
}
//i_buttons = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);
if (i_buttons == 0) {
//cout << "TB_BUTTONCOUNT message failed:" << GetLastError() << endl;
result = false;
break;
}
if (i_buttons > 1000) {
cout << "Exception!!!" << endl;
break;
}
cout << "handle: " << hWnd << "buttons: " << i_buttons << endl;
// 遍历托盘
for (int i = 0; i < i_buttons; i++) {
// 获取 TBBUTTON 结构体指针
if (!SendMessageTimeout(hWnd, TB_GETBUTTON, i, (LPARAM)p_tbbutton, SMTO_ABORTIFHUNG, 1000, 0)) {
//if (!SendMessage(hWnd, TB_GETBUTTON, i, (LPARAM)p_tbbutton)) {
cout << "TB_GETBUTTON message failed:" << GetLastError() << endl;
break;
}
// 读 TBBUTTON.dwData(附加信息)
if (!ReadProcessMemory(hProcess, (LPVOID)((DWORD)p_tbbutton + i_data_offset), &dw_addr_dwData, 4, 0)) {
cout << "ReadProcessMemory failed:" << GetLastError() << endl;
break;
}
// 读文本
if (dw_addr_dwData) {
if (!ReadProcessMemory(hProcess, (LPCVOID)dw_addr_dwData, buff, 1024, 0)) {
cout << "ReadProcessMemory failed:" << GetLastError() << endl;
break;
}
h_mainWnd = (HWND)(*((DWORD*)buff));
ws_filePath = (WCHAR*)buff + i_str_offset;
ws_tile = (WCHAR*)buff + i_str_offset + MAX_PATH;
cout << "hMainWnd = " << hex << h_mainWnd << endl;
cout << "strFilePath = " << WStringToString(ws_filePath.c_str()) << endl;
cout << "strTile = " << WStringToString(ws_tile.c_str()) << endl;
}
// 清理
dw_addr_dwData = 0;
h_mainWnd = NULL;
ws_filePath = L"";
ws_tile = L"";
}
} while (0);
if (p_tbbutton != NULL && VirtualFreeEx(hProcess, p_tbbutton, 0, MEM_RELEASE) == 0) {
cout << "VirtualFreeEx failed:" << GetLastError() << endl;
return FALSE;
}
if (hProcess != NULL && CloseHandle(hProcess) == 0) {
cout << "CloseHandle failed:" << GetLastError() << endl;
return FALSE;
}
return TRUE;
}
//typedef BOOL(CALLBACK* WNDENUMPROC)(HWND, LPARAM);
BOOL EnumChileWindowsProc(HWND window, LPARAM param)
{
EnumNotifyWindow(window);
return true;
}
BOOL EnumWindowsProc(HWND window, LPARAM param)
{
EnumChildWindows(window, (WNDENUMPROC)EnumChileWindowsProc, NULL);
EnumNotifyWindow(window);
return true;
}
int main()
{
// 解决控制台中文 '?'
//setlocale(LC_ALL, "chs");
//_wsetlocale(LC_ALL, L"chs");
// 获取托盘句柄
HWND h_tray = FindTrayWnd();
HWND h_tray_fold = FindNotifyIconOverflowWindow();
cout << "Shell_TrayWnd ->SysPager -> ToolbarWindow32" << endl;
EnumNotifyWindow(h_tray); //0x581aa0
cout << "-------------------" << endl;
cout << "NotifyIconOverflowWindow ->ToolbarWindow32" << endl;
EnumNotifyWindow(h_tray_fold); //0x6d1af2
cout << "-------------------" << endl;
EnumWindows((WNDENUMPROC)EnumWindowsProc, NULL);
// 遍历托盘窗口
return 0;
}