Link to home
Start Free TrialLog in
Avatar of Nobuo Miwa
Nobuo Miwa

asked on

Retrieve PID of MicrosoftEdge.exe with GetForegroundWindow()

Hello Experts,

I want to retrieve process ID of MicrosoftEdge.exe from window handle of GetForegroundWindow() on Windows 10(64 bit) with C++.

HWND hWnd = GetForegroundWindow();
DWORD processID = NULL;
GetWindowThreadProcessId( hWnd, &processID);

Open in new window


When I used Edge and browsing some site, I got PID of ApplicationFrameHost.exe, not of MicrosoftEdge.exe.
How can I get PID of MicrosoftEdge.exe from GetForegroundWindow() ?

Any hints welcome.

Regards
Avatar of sarabande
sarabande
Flag of Luxembourg image

GetForegroundWindow() is the wrong api function. it only works for windows of the own process.

you have to use EnumProcesses, OpenProcess, and perhaps EnumProcessModules to  get the information.

see https://msdn.microsoft.com/de-de/library/windows/desktop/ms682623%28v=vs.85%29.aspx

for a code sample.

Sara
Avatar of Nobuo Miwa
Nobuo Miwa

ASKER

Thank you for comment.

I want to know foreground application's PID. If Edge was on the top window, I want to know its process information.

I use GetForegroundWindow(), because I don't know how to know forground PID from enumlated list.
if you want to get the association of processes to their windows, you have to enumerate all top level windows by calling EnumWindows. for each window (handle) you get you would call GetWindowThreadProcessID which retrieves the process id for this (top level) window. if you also have a list of all processes as shown in the sample code, you could find out which window belongs to which process. it is not really straight forward but not very difficult.  

to get the process id of the foreground window you could try to call GetForegroundWindow and if it returns a non-zero HWND, call GetWindowThreadProcessID. this may or may not retrieve the process id of Edge or another browser if these browsers were used by automation or if a web control (.net assembly) was used.

if you would tell which scenario you want to handle, we probably could give more and better solution.

Sara
ASKER CERTIFIED SOLUTION
Avatar of Nobuo Miwa
Nobuo Miwa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to get the process id of the foreground window you could try to call GetForegroundWindow and if it returns a non-zero HWND, call GetWindowThreadProcessID.
what is the difference of your solution to that?

Sara
I got an answer