添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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/ HWND s 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 HWND s 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.

Exactly the details what I was looking for. I was interested to know on how they are related to each other. Thanks dude! Quest Feb 10, 2012 at 7:59

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.