为什么顶级 windows 的 GetParent(hwnd) 和 (HWND)::GetWindow(hwnd, GW_OWNER) 给出不同的结果?
Why do GetParent(hwnd) and (HWND)::GetWindow(hwnd, GW_OWNER) for top level windows give different results?
我一直在研究 Window 层次结构的工作原理并发现了一个不一致的地方。 return 由两个函数调用 GetParent(hwnd)
和 (HWND)::GetWindow(hwnd, GW_OWNER)
编辑的值虽然大多数同意,但并不总是 用于顶级 windows.
我假设这些是顶级 windows 的原因是因为它们是使用 EnumWindows() function, which are only to enumerate top level windows. It was also confirmed using the test hWnd==GetAncestor(hWnd,GA_ROOT)
as specified in the answer to What's the best way do determine if an HWND represents a top-level window?.
找到的
我在windowclass#32770
AVGUI.exe
和windowclassComboLBox
[= =20=]、notepad++.exe
、TeamViewer.exe
、PrivacyIconClient.exe
、devenv.exe
、……这样的例子还在继续。
GetParent(hwnd)
会 return HWND
of GetDesktopWindow()
,但是 (HWND)::GetWindow(hwnd, GW_OWNER)
会 return nullptr
。因此,如果 GetParent()
应该 return 顶级 window 的所有者,那么当 (HWND)::GetWindow(hwnd, GW_OWNER)
是 returning a nullptr
时,它从哪里得到它?
它确实与 (HWND)::GetWindowLongPtr(hwnd, GWLP_HWNDPARENT)
一致,但这表明它是 child window,这与 window class 有点道理] 很多被列为 ComboLBox
。但是,我已经看到其他 HWND 具有应有的值,并且可能只是根据上下文忽略了该值。这些可能在某一时刻达到 non-top 级别 windows 的另一个原因是 !(GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD)
returns false
.
通过我所做的额外分析,似乎某些应用程序以某种方式将 non-top 级别 windows 提升到顶级 windows,这表明存在一些错误或存在正在使用一些 undocumented/undefined 行为并导致奇怪的 HWND 链接。
任何人都可以确认这些是由错误引起的还是出于某种正当理由正在做的事情?
编辑
最小、完整且可验证的示例:
#include <AtlBase.h> // Conversion routines (CW2A)
#include <Windows.h> // Windows stuff
#include <assert.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <set>
#include <psapi.h>
#include <regex>
auto FIELD_SEPERATOR = L",";
auto& output_window_class_and_title(std::wostream & os, const HWND &hWnd)
{
wchar_t window_class[1024], window_title[1024];
window_class[0] = window_title[0] = 0;
::GetClassNameW(hWnd, window_class, _countof(window_class));
::GetWindowTextW(hWnd, window_title, _countof(window_title));
// replacing any CRLFs with field separators
auto wc
= std::regex_replace(window_class, std::wregex(L"(\r\n?|\n\r?)")
, L" " );
auto wt
= std::regex_replace(window_title, std::wregex(L"(\r\n?|\n\r?)")
, L" " );
os << CW2A(wc.c_str()) << FIELD_SEPERATOR << CW2A(wt.c_str());
return os;
}
// Store exe names
std::set<std::wstring> exe_names;
// Map pid to exe name
std::map<DWORD, std::wstring const*> pid_to_exe_name;
// Get exe name (from cache if possible)
const std::wstring * GetProcessName(DWORD pid)
{
const std::wstring * pProcess_name = nullptr;
auto it_found_pid = pid_to_exe_name.find(pid);
if (it_found_pid == pid_to_exe_name.end()) {
wchar_t exe_name[MAX_PATH]; exe_name[0] = 0;
if (HANDLE hProcess = ::OpenProcess(
PROCESS_ALL_ACCESS | PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, pid))
{
auto chars_copied = ::GetProcessImageFileNameW(hProcess, exe_name, _countof(exe_name));
assert(chars_copied > 0);
exe_name[chars_copied] = 0;
::CloseHandle(hProcess);
auto found = exe_names.emplace(exe_name);
pProcess_name = &*found.first;
}
else
{
auto found = exe_names.emplace(L"* Couldn't open process handle *");
pProcess_name = &*found.first;
}
pid_to_exe_name.try_emplace(pid, pProcess_name);
}
else {
pProcess_name = it_found_pid->second;
}
}
int main()
{
//auto* filename = "window-tree.txt";
//static std::wfstream os(filename, std::ios_base::out | std::ios_base::trunc);
static auto& os = std::wcout;
os.exceptions(os.badbit | os.failbit | os.eofbit);
os << std::hex;
try {
static HWND hDesktop = GetDesktopWindow();
EnumWindows([](_In_ HWND hwnd, _In_ LPARAM lParam) -> BOOL
{
assert(hwnd);
HWND hParent = ::GetParent(hwnd);
if (hParent == hDesktop) {
auto hOwner = (HWND)::GetWindow(hwnd, GW_OWNER);
auto hParent = (HWND)::GetWindowLongPtr(hwnd, GWLP_HWNDPARENT);
auto hParent_from_GetParent = ::GetParent(hwnd);
auto hParent_from_GetAncestor = ::GetAncestor(hwnd, GA_PARENT);
bool is_top_level = (hwnd == GetAncestor(hwnd, GA_ROOT));
bool is_top_level2 = !(GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD);
DWORD pid;
auto tid = ::GetWindowThreadProcessId(hwnd, &pid);
std::wstring const* pProcess_name = GetProcessName(pid);
os
<< std::setw(8) << hwnd
<< FIELD_SEPERATOR << ::IsWindowVisible(hwnd)
<< FIELD_SEPERATOR << is_top_level
<< FIELD_SEPERATOR << is_top_level2
<< FIELD_SEPERATOR << std::setw(8) << hOwner
<< FIELD_SEPERATOR << std::setw(8) << hParent_from_GetParent
<< FIELD_SEPERATOR << std::setw(8) << hParent
<< FIELD_SEPERATOR << std::setw(8) << hParent_from_GetAncestor
<< FIELD_SEPERATOR << std::setw(4) << pid
<< FIELD_SEPERATOR << std::setw(4) << tid
<< FIELD_SEPERATOR << pProcess_name->c_str()
<< FIELD_SEPERATOR;
output_window_class_and_title(os, hwnd);
os
<< std::endl;
}
return TRUE;
}
, 0);
}
catch (std::ios_base::failure& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
示例输出:
000A0FF6,0,1,0,00000000,00010010,00000000,00010010,8520,6d44,\Device\HarddiskVolume2\Program Files (x86)\Notepad++\notepad++.exe,ComboLBox,
0094150C,0,1,0,00000000,00010010,00000000,00010010, 3ac,3f14,\Device\HarddiskVolume4\Windows\explorer.exe,ComboLBox,
0078181E,0,1,0,00000000,00010010,00000000,00010010,5e58,5068,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\Tools\spyxx_amd64.exe,ComboLBox,
00FA16AA,0,1,0,00000000,00010010,00000000,00010010, 3ac,242c,\Device\HarddiskVolume4\Windows\explorer.exe,ComboLBox,
01121B00,0,1,0,00000000,00010010,00000000,00010010,4440,7b98,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
021304D0,0,1,0,00000000,00010010,00000000,00010010,5e1c,5b5c,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
011A11EE,0,1,0,00000000,00010010,00000000,00010010,5e1c,5b5c,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
018D1B7A,0,1,0,00000000,00010010,00000000,00010010,5e1c,5b5c,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
0137042A,0,1,0,00000000,00010010,00000000,00010010,5e1c,5b5c,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
0028065A,0,1,0,00000000,00010010,00000000,00010010,5e1c,5b5c,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
005B0472,0,1,0,00000000,00010010,00000000,00010010,8520,6d44,\Device\HarddiskVolume2\Program Files (x86)\Notepad++\notepad++.exe,ComboLBox,
00421248,0,1,0,00000000,00010010,00000000,00010010,8520,6d44,\Device\HarddiskVolume2\Program Files (x86)\Notepad++\notepad++.exe,ComboLBox,
00BA10F8,0,1,0,00000000,00010010,00000000,00010010,8520,6d44,\Device\HarddiskVolume2\Program Files (x86)\Notepad++\notepad++.exe,ComboLBox,
009E0EE2,0,1,0,00000000,00010010,00000000,00010010,8520,6d44,\Device\HarddiskVolume2\Program Files (x86)\Notepad++\notepad++.exe,ComboLBox,
00040822,0,1,0,00000000,00010010,00000000,00010010, 3ac,3d94,\Device\HarddiskVolume4\Windows\explorer.exe,ComboLBox,
000404A4,0,1,0,00000000,00010010,00000000,00010010, 7e0, 7dc,\Device\HarddiskVolume4\Program Files (x86)\Intel\Intel(R) Management Engine Components\IMSS\PrivacyIconClient.exe,ComboLBox,
000404A0,0,1,0,00000000,00010010,00000000,00010010, 7e0, 7dc,\Device\HarddiskVolume4\Program Files (x86)\Intel\Intel(R) Management Engine Components\IMSS\PrivacyIconClient.exe,ComboLBox,
000102FE,0,1,0,00000000,00010010,00000000,00010010,19dc, 4c4,\Device\HarddiskVolume2\Program Files (x86)\TeamViewer\TeamViewer.exe,ComboLBox,
00010290,0,1,0,00000000,00010010,00000000,00010010,19dc, 4c4,\Device\HarddiskVolume2\Program Files (x86)\TeamViewer\TeamViewer.exe,ComboLBox,
00EF16B4,0,1,0,00000000,00010010,00000000,00010010,4440,7b98,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
00840D20,0,1,0,00000000,00010010,00000000,00010010,4440,7b98,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
016A0E64,0,1,0,00000000,00010010,00000000,00010010,4440,7b98,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
021B11F2,0,1,0,00000000,00010010,00000000,00010010,4440,7b98,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
一个window有一个parent但没有所有者不是顶级window。这是对您描述的场景的明显解释。
更新
好像不是这样的,因为你现在解释windows来自EnumWindows
。它的文档说:
The EnumWindows function does not enumerate child windows, with the exception of a few top-level windows owned by the system that have the WS_CHILD style.
因此我认为这些一定是您所描述的windows。那是系统拥有的顶级 windows,它提供了 WS_CHILD 风格。
我一直在研究 Window 层次结构的工作原理并发现了一个不一致的地方。 return 由两个函数调用 GetParent(hwnd)
和 (HWND)::GetWindow(hwnd, GW_OWNER)
编辑的值虽然大多数同意,但并不总是 用于顶级 windows.
我假设这些是顶级 windows 的原因是因为它们是使用 EnumWindows() function, which are only to enumerate top level windows. It was also confirmed using the test hWnd==GetAncestor(hWnd,GA_ROOT)
as specified in the answer to What's the best way do determine if an HWND represents a top-level window?.
我在windowclass#32770
AVGUI.exe
和windowclassComboLBox
[= =20=]、notepad++.exe
、TeamViewer.exe
、PrivacyIconClient.exe
、devenv.exe
、……这样的例子还在继续。
GetParent(hwnd)
会 return HWND
of GetDesktopWindow()
,但是 (HWND)::GetWindow(hwnd, GW_OWNER)
会 return nullptr
。因此,如果 GetParent()
应该 return 顶级 window 的所有者,那么当 (HWND)::GetWindow(hwnd, GW_OWNER)
是 returning a nullptr
时,它从哪里得到它?
它确实与 (HWND)::GetWindowLongPtr(hwnd, GWLP_HWNDPARENT)
一致,但这表明它是 child window,这与 window class 有点道理] 很多被列为 ComboLBox
。但是,我已经看到其他 HWND 具有应有的值,并且可能只是根据上下文忽略了该值。这些可能在某一时刻达到 non-top 级别 windows 的另一个原因是 !(GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD)
returns false
.
通过我所做的额外分析,似乎某些应用程序以某种方式将 non-top 级别 windows 提升到顶级 windows,这表明存在一些错误或存在正在使用一些 undocumented/undefined 行为并导致奇怪的 HWND 链接。
任何人都可以确认这些是由错误引起的还是出于某种正当理由正在做的事情?
编辑
最小、完整且可验证的示例:
#include <AtlBase.h> // Conversion routines (CW2A)
#include <Windows.h> // Windows stuff
#include <assert.h>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <set>
#include <psapi.h>
#include <regex>
auto FIELD_SEPERATOR = L",";
auto& output_window_class_and_title(std::wostream & os, const HWND &hWnd)
{
wchar_t window_class[1024], window_title[1024];
window_class[0] = window_title[0] = 0;
::GetClassNameW(hWnd, window_class, _countof(window_class));
::GetWindowTextW(hWnd, window_title, _countof(window_title));
// replacing any CRLFs with field separators
auto wc
= std::regex_replace(window_class, std::wregex(L"(\r\n?|\n\r?)")
, L" " );
auto wt
= std::regex_replace(window_title, std::wregex(L"(\r\n?|\n\r?)")
, L" " );
os << CW2A(wc.c_str()) << FIELD_SEPERATOR << CW2A(wt.c_str());
return os;
}
// Store exe names
std::set<std::wstring> exe_names;
// Map pid to exe name
std::map<DWORD, std::wstring const*> pid_to_exe_name;
// Get exe name (from cache if possible)
const std::wstring * GetProcessName(DWORD pid)
{
const std::wstring * pProcess_name = nullptr;
auto it_found_pid = pid_to_exe_name.find(pid);
if (it_found_pid == pid_to_exe_name.end()) {
wchar_t exe_name[MAX_PATH]; exe_name[0] = 0;
if (HANDLE hProcess = ::OpenProcess(
PROCESS_ALL_ACCESS | PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, pid))
{
auto chars_copied = ::GetProcessImageFileNameW(hProcess, exe_name, _countof(exe_name));
assert(chars_copied > 0);
exe_name[chars_copied] = 0;
::CloseHandle(hProcess);
auto found = exe_names.emplace(exe_name);
pProcess_name = &*found.first;
}
else
{
auto found = exe_names.emplace(L"* Couldn't open process handle *");
pProcess_name = &*found.first;
}
pid_to_exe_name.try_emplace(pid, pProcess_name);
}
else {
pProcess_name = it_found_pid->second;
}
}
int main()
{
//auto* filename = "window-tree.txt";
//static std::wfstream os(filename, std::ios_base::out | std::ios_base::trunc);
static auto& os = std::wcout;
os.exceptions(os.badbit | os.failbit | os.eofbit);
os << std::hex;
try {
static HWND hDesktop = GetDesktopWindow();
EnumWindows([](_In_ HWND hwnd, _In_ LPARAM lParam) -> BOOL
{
assert(hwnd);
HWND hParent = ::GetParent(hwnd);
if (hParent == hDesktop) {
auto hOwner = (HWND)::GetWindow(hwnd, GW_OWNER);
auto hParent = (HWND)::GetWindowLongPtr(hwnd, GWLP_HWNDPARENT);
auto hParent_from_GetParent = ::GetParent(hwnd);
auto hParent_from_GetAncestor = ::GetAncestor(hwnd, GA_PARENT);
bool is_top_level = (hwnd == GetAncestor(hwnd, GA_ROOT));
bool is_top_level2 = !(GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD);
DWORD pid;
auto tid = ::GetWindowThreadProcessId(hwnd, &pid);
std::wstring const* pProcess_name = GetProcessName(pid);
os
<< std::setw(8) << hwnd
<< FIELD_SEPERATOR << ::IsWindowVisible(hwnd)
<< FIELD_SEPERATOR << is_top_level
<< FIELD_SEPERATOR << is_top_level2
<< FIELD_SEPERATOR << std::setw(8) << hOwner
<< FIELD_SEPERATOR << std::setw(8) << hParent_from_GetParent
<< FIELD_SEPERATOR << std::setw(8) << hParent
<< FIELD_SEPERATOR << std::setw(8) << hParent_from_GetAncestor
<< FIELD_SEPERATOR << std::setw(4) << pid
<< FIELD_SEPERATOR << std::setw(4) << tid
<< FIELD_SEPERATOR << pProcess_name->c_str()
<< FIELD_SEPERATOR;
output_window_class_and_title(os, hwnd);
os
<< std::endl;
}
return TRUE;
}
, 0);
}
catch (std::ios_base::failure& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
示例输出:
000A0FF6,0,1,0,00000000,00010010,00000000,00010010,8520,6d44,\Device\HarddiskVolume2\Program Files (x86)\Notepad++\notepad++.exe,ComboLBox,
0094150C,0,1,0,00000000,00010010,00000000,00010010, 3ac,3f14,\Device\HarddiskVolume4\Windows\explorer.exe,ComboLBox,
0078181E,0,1,0,00000000,00010010,00000000,00010010,5e58,5068,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\Tools\spyxx_amd64.exe,ComboLBox,
00FA16AA,0,1,0,00000000,00010010,00000000,00010010, 3ac,242c,\Device\HarddiskVolume4\Windows\explorer.exe,ComboLBox,
01121B00,0,1,0,00000000,00010010,00000000,00010010,4440,7b98,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
021304D0,0,1,0,00000000,00010010,00000000,00010010,5e1c,5b5c,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
011A11EE,0,1,0,00000000,00010010,00000000,00010010,5e1c,5b5c,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
018D1B7A,0,1,0,00000000,00010010,00000000,00010010,5e1c,5b5c,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
0137042A,0,1,0,00000000,00010010,00000000,00010010,5e1c,5b5c,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
0028065A,0,1,0,00000000,00010010,00000000,00010010,5e1c,5b5c,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
005B0472,0,1,0,00000000,00010010,00000000,00010010,8520,6d44,\Device\HarddiskVolume2\Program Files (x86)\Notepad++\notepad++.exe,ComboLBox,
00421248,0,1,0,00000000,00010010,00000000,00010010,8520,6d44,\Device\HarddiskVolume2\Program Files (x86)\Notepad++\notepad++.exe,ComboLBox,
00BA10F8,0,1,0,00000000,00010010,00000000,00010010,8520,6d44,\Device\HarddiskVolume2\Program Files (x86)\Notepad++\notepad++.exe,ComboLBox,
009E0EE2,0,1,0,00000000,00010010,00000000,00010010,8520,6d44,\Device\HarddiskVolume2\Program Files (x86)\Notepad++\notepad++.exe,ComboLBox,
00040822,0,1,0,00000000,00010010,00000000,00010010, 3ac,3d94,\Device\HarddiskVolume4\Windows\explorer.exe,ComboLBox,
000404A4,0,1,0,00000000,00010010,00000000,00010010, 7e0, 7dc,\Device\HarddiskVolume4\Program Files (x86)\Intel\Intel(R) Management Engine Components\IMSS\PrivacyIconClient.exe,ComboLBox,
000404A0,0,1,0,00000000,00010010,00000000,00010010, 7e0, 7dc,\Device\HarddiskVolume4\Program Files (x86)\Intel\Intel(R) Management Engine Components\IMSS\PrivacyIconClient.exe,ComboLBox,
000102FE,0,1,0,00000000,00010010,00000000,00010010,19dc, 4c4,\Device\HarddiskVolume2\Program Files (x86)\TeamViewer\TeamViewer.exe,ComboLBox,
00010290,0,1,0,00000000,00010010,00000000,00010010,19dc, 4c4,\Device\HarddiskVolume2\Program Files (x86)\TeamViewer\TeamViewer.exe,ComboLBox,
00EF16B4,0,1,0,00000000,00010010,00000000,00010010,4440,7b98,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
00840D20,0,1,0,00000000,00010010,00000000,00010010,4440,7b98,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
016A0E64,0,1,0,00000000,00010010,00000000,00010010,4440,7b98,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
021B11F2,0,1,0,00000000,00010010,00000000,00010010,4440,7b98,\Device\HarddiskVolume4\Program Files (x86)\Microsoft Visual Studio17\Professional\Common7\IDE\devenv.exe,ComboLBox,
一个window有一个parent但没有所有者不是顶级window。这是对您描述的场景的明显解释。
更新
好像不是这样的,因为你现在解释windows来自EnumWindows
。它的文档说:
The EnumWindows function does not enumerate child windows, with the exception of a few top-level windows owned by the system that have the WS_CHILD style.
因此我认为这些一定是您所描述的windows。那是系统拥有的顶级 windows,它提供了 WS_CHILD 风格。