VC++ 如何获取window 所属的主程序的名称?
VC++ How do I get the name of the main program a window belongs to?
示例测试环境:
我在 Windows 桌面上放置了两个文件夹,每个文件夹都有自己的文件和子文件夹内容。其中一个文件夹名为 ALPHA,另一个名为 BRAVO。我双击了这些文件夹中的每一个,所以我在屏幕上打开了这两个 Windows Explorer windows。
我还启动了 Microsoft Word 并创建了一个新的空文档(名为 Document1)。然后按 Ctrl+N 创建另一个空文档(名为 Document2)。所以我也打开了两个 Word windows。
我启动了 Firefox 并导航到 bluepaint.com,然后打开另一个浏览器 window 并导航到 yellowsand.org,所以我的屏幕上也有两个 Firefox windows。
代码:
然后在我的 Windows 应用程序(VS2019 社区)中,我捕获鼠标按钮 UP 事件并执行类似这样的操作...
TCHAR msg_tchar[256];
HWND wh_parent_window; // parent_window or owner_window
const unsigned int max_wintitle_length = 256;
TCHAR window_title_string[max_wintitle_length];
HWND wh_current_window = GetForegroundWindow();
GetWindowText(wh_current_window, window_title_string, (max_wintitle_length-1));
StringCchPrintf(msg_tchar, 255, _T("--NewWindow: \"%s\"\r\n"), window_title_string);
printToLogfile(msg_tchar);
if ((wh_parent_window = GetWindow(wh_current_window, GW_OWNER)) != NULL) {
GetWindowText(wh_parent_window, window_title_string, (max_wintitle_length-1));
StringCchPrintf(msg_tchar, 255, _T("ownerWindow: \"%s\"\r\n"), window_title_string);
printToLogfile(msg_tchar);
} //// if getOwnerWindow
if ((wh_parent_window = GetAncestor(wh_current_window, GA_PARENT)) != NULL) {
GetWindowText(wh_parent_window, window_title_string, (max_wintitle_length-1));
StringCchPrintf(msg_tchar, 255, _T("parentWindow: \"%s\"\r\n"), window_title_string);
printToLogfile(msg_tchar);
} //// if getParendWindow
它编译并运行良好,并且按照我的指示进行。单击我屏幕上的 windows,我得到以下输出。
--NewWindow: "Yellow Sand Beach informational portal - Mozilla Firefox"
parentWindow: ""
--NewWindow: "Categories of blue paints - Mozilla Firefox"
parentWindow: ""
--NewWindow: "Document2 - Word"
parentWindow: ""
--NewWindow: "Document1 - Word"
parentWindow: ""
--NewWindow: "ALPHA"
parentWindow: ""
--NewWindow: "BRAVO"
parentWindow: ""
Apparently,没有 OwnerWindow 行。我不太关心。
但我真正关心的是这些 windows 属于什么应用程序。看来我走错了路,因为我希望获得每个 window 的主要应用程序名称。所以对于 ALPHA 和 BRAVO,我想得到类似“Windows Explorer”或“explorer.exe”的东西,因为我想获得类似“Microsoft Office 2019”或“Microsoft Word”或“[=55=”之类的文档]”,对于网页 windows,我想得到类似“firefox.exe”或“Firefox”或“Mozilla Firefox”。 是的,我可以看到可以从它们的 window 标题中提取 Firefox 和 Word,但这不适用于 ALPHA 和 BRAVO 以及其他应用程序的 windows。
所以 代替 window 标题,我如何获得 parent 应用程序名称或拥有的可执行文件名称?
我很高兴阅读文档,只是不知道要查找什么函数名。向正确的方向推进将不胜感激。
感谢 Igor 在正确方向上的努力,我编写了以下函数,它满足了我一直在寻找的目的。
bool queryEXEnameOfWindow(HWND p_window_handle, std::wstring& p_exe_path) {
TCHAR tmp_path_str[MAX_PATH] = {0};
DWORD tmp_processID = 0;
DWORD tmp_length = MAX_PATH-1;
GetWindowThreadProcessId(p_window_handle, &tmp_processID);
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, tmp_processID);
BOOL tmp_result = QueryFullProcessImageName(processHandle, 0, tmp_path_str, &tmp_length);
CloseHandle(processHandle);
if (tmp_result) {
p_exe_path = tmp_path_str;
return true;
} //// if
return false;
} //// funcDef
wstring 中返回的结果如下:
C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE
C:\Program Files\Firefox\firefox.exe
C:\Windows\explorer.exe
C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE
C:\Program Files\LibreOffice\program\soffice.bin
示例测试环境:
我在 Windows 桌面上放置了两个文件夹,每个文件夹都有自己的文件和子文件夹内容。其中一个文件夹名为 ALPHA,另一个名为 BRAVO。我双击了这些文件夹中的每一个,所以我在屏幕上打开了这两个 Windows Explorer windows。
我还启动了 Microsoft Word 并创建了一个新的空文档(名为 Document1)。然后按 Ctrl+N 创建另一个空文档(名为 Document2)。所以我也打开了两个 Word windows。
我启动了 Firefox 并导航到 bluepaint.com,然后打开另一个浏览器 window 并导航到 yellowsand.org,所以我的屏幕上也有两个 Firefox windows。
代码:
然后在我的 Windows 应用程序(VS2019 社区)中,我捕获鼠标按钮 UP 事件并执行类似这样的操作...
TCHAR msg_tchar[256];
HWND wh_parent_window; // parent_window or owner_window
const unsigned int max_wintitle_length = 256;
TCHAR window_title_string[max_wintitle_length];
HWND wh_current_window = GetForegroundWindow();
GetWindowText(wh_current_window, window_title_string, (max_wintitle_length-1));
StringCchPrintf(msg_tchar, 255, _T("--NewWindow: \"%s\"\r\n"), window_title_string);
printToLogfile(msg_tchar);
if ((wh_parent_window = GetWindow(wh_current_window, GW_OWNER)) != NULL) {
GetWindowText(wh_parent_window, window_title_string, (max_wintitle_length-1));
StringCchPrintf(msg_tchar, 255, _T("ownerWindow: \"%s\"\r\n"), window_title_string);
printToLogfile(msg_tchar);
} //// if getOwnerWindow
if ((wh_parent_window = GetAncestor(wh_current_window, GA_PARENT)) != NULL) {
GetWindowText(wh_parent_window, window_title_string, (max_wintitle_length-1));
StringCchPrintf(msg_tchar, 255, _T("parentWindow: \"%s\"\r\n"), window_title_string);
printToLogfile(msg_tchar);
} //// if getParendWindow
它编译并运行良好,并且按照我的指示进行。单击我屏幕上的 windows,我得到以下输出。
--NewWindow: "Yellow Sand Beach informational portal - Mozilla Firefox"
parentWindow: ""
--NewWindow: "Categories of blue paints - Mozilla Firefox"
parentWindow: ""
--NewWindow: "Document2 - Word"
parentWindow: ""
--NewWindow: "Document1 - Word"
parentWindow: ""
--NewWindow: "ALPHA"
parentWindow: ""
--NewWindow: "BRAVO"
parentWindow: ""
Apparently,没有 OwnerWindow 行。我不太关心。 但我真正关心的是这些 windows 属于什么应用程序。看来我走错了路,因为我希望获得每个 window 的主要应用程序名称。所以对于 ALPHA 和 BRAVO,我想得到类似“Windows Explorer”或“explorer.exe”的东西,因为我想获得类似“Microsoft Office 2019”或“Microsoft Word”或“[=55=”之类的文档]”,对于网页 windows,我想得到类似“firefox.exe”或“Firefox”或“Mozilla Firefox”。 是的,我可以看到可以从它们的 window 标题中提取 Firefox 和 Word,但这不适用于 ALPHA 和 BRAVO 以及其他应用程序的 windows。 所以 代替 window 标题,我如何获得 parent 应用程序名称或拥有的可执行文件名称?
我很高兴阅读文档,只是不知道要查找什么函数名。向正确的方向推进将不胜感激。
感谢 Igor 在正确方向上的努力,我编写了以下函数,它满足了我一直在寻找的目的。
bool queryEXEnameOfWindow(HWND p_window_handle, std::wstring& p_exe_path) {
TCHAR tmp_path_str[MAX_PATH] = {0};
DWORD tmp_processID = 0;
DWORD tmp_length = MAX_PATH-1;
GetWindowThreadProcessId(p_window_handle, &tmp_processID);
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, FALSE, tmp_processID);
BOOL tmp_result = QueryFullProcessImageName(processHandle, 0, tmp_path_str, &tmp_length);
CloseHandle(processHandle);
if (tmp_result) {
p_exe_path = tmp_path_str;
return true;
} //// if
return false;
} //// funcDef
wstring 中返回的结果如下:
C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE
C:\Program Files\Firefox\firefox.exe
C:\Windows\explorer.exe
C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE
C:\Program Files\LibreOffice\program\soffice.bin