9

I've been trying to use IVirtualDesktopManager::MoveWindowToDesktop to move windows between virtual desktops.

Using it on my own windows works great but unfortunately it fails with E_ACCESSDENIED when I try to use it on the window of another process.

Sadly "Launch as admin" is not good enough for it. Am I missing something that I don't know about COM interfaces or is it just badly designed ?

Nopt
  • 93
  • 5
  • which process do you try to move? – magicandre1981 Sep 19 '15 at 05:00
  • "Not designed the way you want it" != "badly designed" :-). From the documentation, it sounds like this is only for moving your own windows around (to avoid poor user experience); it doesn't sound like a general-purpose API for managing virtual desktops. Did you provide feedback on the MSDN topic? – Peter Torr - MSFT Sep 19 '15 at 05:16
  • @magicandre1981 anything really, explorer, skype, firefox – Nopt Sep 19 '15 at 09:19
  • @PeterTorr-MSFT You're right it's more like "what's_the_point_then"-designed ^^' Btw, if moving windows around desktops is poor user experience, why ShowWindow(SW_HIDE) works on any window ? It seems way worse to me ! Nope I didn't, but that's a good idea, thanks :-) – Nopt Sep 19 '15 at 09:29
  • Did you find a solution to this? thanks – wily May 29 '17 at 02:59

1 Answers1

10

You can move a window not owned by the executing process, but it requires the use of additional, undocumented COM objects.

  1. Query ImmersiveShell for instances of IApplicationViewCollection and IVirtualDesktopManagerInternal.
  2. Get the hwnd of the window you want to move.
  3. It sounds like you already know the target desktop ID, but you can also use IVirtualDesktopManagerInternal methods GetAdjacentDesktop and GetDesktops to find more.
  4. Call IApplicationViewCollection::GetViewForHwnd to get the view.
  5. Call IVirtualDesktopManagerInternal::MoveViewToDesktop to move the view.

Code references:

  • VirtualDesktopAccessor wraps the relevant objects in C++ and builds a native DLL you can use.
  • VirtualDesktop wraps them in C# and provides a .NET DLL and GUI.
  • zVirtualDesktop documents different versions of the interfaces.
  • This forum topic shows how to work with the IVirtualDesktopManagerInternal to select a virtual desktop by index in AutoHotKey. I'm working on a version that also wraps IApplicationViewCollection to support moving any given window as described above.
Shawn Hoover
  • 726
  • 8
  • 9