Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the GetDesktopWindow and OpenInputDesktop APIs in Windows?

Tags:

windows

winapi

What is the usage difference between the GetDesktopWindow and OpenInputDesktop APIs in Windows?

like image 863
Quest Avatar asked Feb 10 '12 03:02

Quest


2 Answers

As to what they do:

GetDesktopWindow() returns the 'root' HWND of whichever desktop the calling thread is currently associated with; it's perhaps better thought of as "Get root HWND". All the other windows/HWNDs on the desktop are somehow descendants of this. Top-level windows are direct children. (Message-only windows are a special case, they don't show up on the HWND tree, but still belong to a desktop.) Note that this is not the same as the window that is at the background with all the files and icons on it, that's perhaps properly called the "Shell Desktop" window, and you can get that using GetShellWindow() - it just happens to be a special type of top-level window.

OpenInputDesktop() returns the HDESK of whichever desktop currently is active and receiving user input. Each desktop has its own tree of HWNDs rooted at the root window, or 'desktop' window.

As to how these are related, once you have a HDESK, you can use SetThreadDesktop to set that desktop as the thread's default desktop; then calling GetDesktopWindow will return the root HWND for that desktop. (Note that you have to have permission to use that HDESK in the first place, which is usually not the case if the input desktop is the locked desktop, for example.) You can also use GetThreadDesktop() to get the HDESK for the current thread.

You might use GetDesktopWindow() if you wanted to traverse the HWND tree for the desktop that your application is on - Spy++-type apps might use this to get the root window and traverse from there using GetWindow() or similar, perhaps. But most apps are happy keeping to themselves so don't need to be aware of what other windows are out there. Perhaps one common use is for checking if an arbitrary window is top-level: use GetAncestor(hwnd, GA_PARENT), and check if the return value matches GetDesktopWindow().

OpenInputDesktop() is perhaps even more rarely used; most apps just sit on the desktop they are started on and stay there. Perhaps if you wrote a desktop switching utility that created multiple desktops that the user could switch between, then that app or some other app could use this to make sure it was on the current one before displaying UI there, but that's really not a common scenario at all. It might have been possible at one stage to write something like a magnifier or screen reader or other app with UI that would want to "follow the user" as they switch desktops, but that doesn't work with the locked desktop which is secure - so these types of apps have to use another way to work with that case instead.

like image 197
BrendanMcK Avatar answered Oct 22 '22 08:10

BrendanMcK


They return completely different values, so they are not interchangeable. You can tell just by looking at their function signatures in the documentation:

GetDesktopWindow returns an HWND, which is a handle to a window:

HWND WINAPI GetDesktopWindow(void);

while OpenInputDesktop returns an HDESK, which is a handle to a desktop:

HDESK WINAPI OpenInputDesktop(
  __in  DWORD dwFlags,
  __in  BOOL fInherit,
  __in  ACCESS_MASK dwDesiredAccess
);

Therefore, which one you use would obviously depend on which type of value you needed. All of the Desktop functions require parameters of type HDESK. All window manipulation functions are going to require handles to a window (HWND).

All of that said, I would be remiss if I didn't caution you against abusing the desktop window, as returned by the GetDesktopWindow function. The desktop window is very special.

like image 21
Cody Gray Avatar answered Oct 22 '22 06:10

Cody Gray