下面的示例可靠地返回与活动窗口关联的进程的名称,但不适用于较新的现代/通用应用程序,因为它返回Windows 8和ApplicationFrameHost.exe上的助手进程WWAHost.exe的名称Windows 10而不是应用程序的名称。
HWND active_window = GetForegroundWindow(); GetWindowThreadProcessId(active_window, &active_process_id); HANDLE active_process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, active_process_id); GetProcessImageFileName(active_process, image_name, 512);
在Windows 10中,ApplicationFrameHost.exe是创build窗口句柄的过程,它是由GetWindowThreadProcessId()返回的,还有另外一个Win32 API可以用来获取活动的通用应用程序的活动进程吗?
还尝试使用GetApplicationUserModelId()和GetPackageFullName()没有成功,因为它们分别返回APPMODEL_ERROR_NO_APPLICATION和APPMODEL_ERROR_NO_PACKAGE,因为active_process句柄只是辅助进程而不是活动应用程序的进程。
任何其他API用于获取给定窗口的hwnd的Modern / Universal应用程序的进程名称,或以其他方式指出通用应用程序的进程名称是活动的。
提前致谢!
当你想要反向工程这样的事情时,一定要使用Spy ++工具。 包含在Visual Studio中,您需要Common7 \ Tools \ spyxx_amd64.exe中的64位版本。 使用“搜索”>“查找窗口”,然后将靶心拖到UWP应用程序,如天气。
您将看到使用GetForegroundWindow()可以找到的窗口,它至少有三个子窗口:
所以你只需要从你已经有的代码中多做一步,你只需要枚举子窗口,然后寻找一个拥有不同所有者进程的程序。 一些C代码,试图使其尽可能通用,而不用做太多的假设,也没有足够的错误检查:
#include <stdio.h> #include <Windows.h> typedef struct { DWORD ownerpid; DWORD childpid; } windowinfo; BOOL CALLBACK EnumChildWindowsCallback(HWND hWnd, LPARAM lp) { windowinfo* info = (windowinfo*)lp; DWORD pid = 0; GetWindowThreadProcessId(hWnd, &pid); if (pid != info->ownerpid) info->childpid = pid; return TRUE; } int main() { Sleep(2000); HWND active_window = GetForegroundWindow(); windowinfo info = { 0 }; GetWindowThreadProcessId(active_window, &info.ownerpid); info.childpid = info.ownerpid; EnumChildWindows(active_window, EnumChildWindowsCallback, (LPARAM)&info); HANDLE active_process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, info.childpid); WCHAR image_name[MAX_PATH] = { 0 }; DWORD bufsize = MAX_PATH; QueryFullProcessImageName(active_process, 0, image_name, &bufsize); wprintf(L"%s\n", image_name); CloseHandle(active_process); return 0; }
天气预报输出:
C:\ Program Files \ WindowsApps \ Microsoft.BingWeather_4.5.168.0_x86__8wekyb3d8bbwe \ Microsoft.Msn.Weather.exe
这是一个连续的小型控制台应用程序应用程序(因此您可以轻松地在桌面上选择不同的窗口进行测试)显示有关当前前台窗口进程和存储过程的信息(如果有的话)。
应用程序可以具有可跨越多个进程的窗口层次结构。 我在这里做的是搜索具有“Windows.UI.Core.CoreWindow”类名的第一个子窗口。
这个应用程序使用UIAutomation API (以及#import指令提供的智能指针,智能BSTR和智能VARIANT)。 我想你可以用标准的Windows SDK来做同样的事情,但是我发现使用这种方式的UIAutomation非常优雅。
#include "stdafx.h" #import "UIAutomationCore.dll" using namespace UIAutomationClient; int main() { // initialize COM, needed for UIA CoInitialize(NULL); // initialize main UIA class IUIAutomationPtr pUIA(__uuidof(CUIAutomation)); do { // get the Automation element for the foreground window IUIAutomationElementPtr foregroundWindow = pUIA->ElementFromHandle(GetForegroundWindow()); wprintf(L"pid:%i\n", foregroundWindow->CurrentProcessId); // prepare a [class name = 'Windows.UI.Core.CoreWindow'] condition _variant_t prop = L"Windows.UI.Core.CoreWindow"; IUIAutomationConditionPtr condition = pUIA->CreatePropertyCondition(UIA_ClassNamePropertyId, prop); // get the first element (window hopefully) that satisfies this condition IUIAutomationElementPtr coreWindow = foregroundWindow->FindFirst(TreeScope::TreeScope_Children, condition); if (coreWindow) { // get the process id property for that window wprintf(L"store pid:%i\n", coreWindow->CurrentProcessId); } Sleep(1000); } while (TRUE); cleanup: CoUninitialize(); return 0; }
快照正在运行的进程,并通过比较进程ID不起作用拉出名称? 完整的参考资料:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686837(v=vs.85).aspx
但是,您使用CreateToolhelp32Snapshot()修复快照。
或从WTSEnumerateProcesses()和周围的API?
很确定它在Win 8中工作。它在10中被破坏了吗?
从Win10 Anniversary更新开始ApplicationFrameHost子窗口返回除UWP应用程序以外的任何内容。 重新登录后,它仅在平板电脑模式下工作。