问题描述
以下示例可靠地返回与活动窗口相关联的进程的名称,但不适用于较新的现代/通用应用程序,因为它返回辅助进程的名称 WWAHost.exe 在Windows 8和 ApplicationFrameHost.exe 在Windows 10而不是应用程序的名称。
The following sample has reliably returned the name of the process that is associated with the active window, but does not work with the newer modern/universal apps because it returns the name of a helper process WWAHost.exe on Windows 8 and ApplicationFrameHost.exe on Windows 10 rather than the name of the app.
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是创建窗口句柄的过程,
With Windows 10 the ApplicationFrameHost.exe is the process that creates the window handles and is what gets returned by GetWindowThreadProcessId(), is there another Win32 API that can be used to get the active process of universal app that is active?
也尝试过使用GetApplicationUserModelId()和GetPackageFullName()方法来获取活动的通用应用程序的活动进程。GetWindowThreadProcessId()没有成功,因为它们分别返回APPMODEL_ERROR_NO_APPLICATION和APPMODEL_ERROR_NO_PACKAGE,因为active_process句柄只是辅助进程而不是活动应用程序的进程。
Also tried using GetApplicationUserModelId() and GetPackageFullName() with no success as they return APPMODEL_ERROR_NO_APPLICATION and APPMODEL_ERROR_NO_PACKAGE respectively because the active_process handle is just the helper process and not the process of the active application.
任何其他API用于获取给予窗口hwnd的现代/通用应用程序的进程名称,或者以其他方式确定通用应用程序的进程名称是活动的。
Any other APIs to use to get the process name of a Modern/Universal application given the hwnd of the window, or otherwise figure out the process name of the universal app is active.
提前感谢! / p>
Thanks in advance!
推荐答案
当你要反向工程时,请务必使用Spy ++实用程序。包括在Visual Studio中,您需要64位版本的Common7 \Tools\spyxx_amd64.exe。使用搜索>查找窗口并将舷窗拖动到UWP应用程序,如天气。
Be sure to use the Spy++ utility when you want to reverse-engineer something like this. Included with Visual Studio, you need the 64-bit version in Common7\Tools\spyxx_amd64.exe. Use Search > Find Window and drag the bullseye to a UWP app, like Weather.
您将看到使用GetForegroundWindow()最少3个子窗口:
You'll see the window you'll find with GetForegroundWindow(), it has at least 3 child windows:
- ApplicationFrameTitleBarWindow
- ApplicationFrameInputSinkWindow
- Windows.Core.UI.CoreWindow,这是UWP应用程序和您感兴趣的主机窗口。右键单击它并选择属性,进程选项卡,单击进程ID。
因此,您只需要从已经拥有的代码中额外完成一个步骤有,你只需要枚举子窗口,并寻找一个具有不同的所有者进程。一些C代码,试图使其尽可能普遍,没有做太多的假设和没有足够的错误检查:
So you just need to make an extra step from the code you already have, you just have to enumerate the child windows and look for one with a different owner process. Some C code, trying to make it as universal as possible without making too many assumptions and not enough error checking:
#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;
}
Weather程式上的输出:
Output on the Weather program:
这篇关于Windows 8/10中活动窗口的进程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!