A little backstory

We had this problem at work where we had to bring a window attached to a process to the foreground. I thought this was not possible because the process has access to the window and not anything else.

Recently, When I was debugging an issue I was using a tool called Process Explorer from Microsoft. That tool had an option to bring the window to the front. This reminded me of the problem that we had earlier and realised that I missed an obvious point. The window belongs to a process but the windowing system belongs to the operating system.

A bit of googling

With a little bit of googling I found the headerfile winuser.h in Windows SDK. This headerfile had function declarations that interact with windows. Microsoft has documented them very well and you can find them here

Hello winuser.h(user32.dll)

I first started going through the list of functions available to find a function that can take a process ID and return all the windows handles that are attached to that process. But the API doesn’t provide any such function. Instead I found a function that enumerates all the available windows.

// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumwindows
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAMS lParams) {
	return FALSE;
}
int main() {
	EnumWindows(EnumWindowsProc, NULL);
}