Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving running application between virtual desktop

Tags:

c#

I have already gone through articles in stack overflow related to virtual desktops, below are the links related to virtual desktop but it doesn't resolve my issue.

"How to Switch a Process between Default Desktop and Winlogon Desktop?" How to switch a process between default desktop and Winlogon desktop?

"Moving applications between desktops in Windows" Moving applications between desktops in Windows

I have WPF application running on one desktop and I want to move that application to another desktop when I switch to that desktop. I already applied the code mentioned in of the articles as mentioned below.

{code}

Debug.Write("MoveTONewDesktop ........");
IntPtr hWinSta0 = OpenWindowStation("WinSta0", false, ACCESS_MASK.MAXIMUM_ALLOWED);
Debug.Write("Windows Station Pointer "+ hWinSta0.ToInt32());
if (null == hWinSta0) { }

hWinSta0 = SetProcessWindowStation(hWinSta0);
Debug.Write("SetProcessWindowStation " + hWinSta0.ToInt32());

IntPtr hDesk = OpenDesktop("ABCD", 0, false, ACCESS_MASK.MAXIMUM_ALLOWED);
Debug.Write("OpenDesktop " + hDesk.ToInt32());
if (null == hDesk) { }

bool result = SwitchDesktop(hDesk);
Debug.Write("SwitchDesktop " + result);

bool bSuccess = SetThreadDesktop(hDesk);
Debug.Write("SetThreadDesktop " + bSuccess);
if (!bSuccess)
{
    Debug.Write("Get Last WIn32 Error " + Marshal.GetLastWin32Error());
    System.Console.WriteLine(Marshal.GetLastWin32Error());
}

if (hDesk != null) { CloseDesktop(hDesk); }
if (hWinSta0 != null) { CloseWindowStation(hWinSta0); }

On debugging I see correct handle get printed and my desktop get switched. Also setThreadDesktop shows true value but my application remains in old desktop and doesn't move to new desktop. What could be th reason that my application doesnt move from one desktop to another. Am I missing something. Please help...

Thanks & Regards, Ashu

like image 816
user3062021 Avatar asked Dec 03 '13 15:12

user3062021


1 Answers

According to your description I assume that your application already had at least one window on the 'original' desktop where it was created. MSDN tells that:

The SetThreadDesktop function will fail if the calling thread has any windows or hooks on its current desktop (unless the hDesktop parameter is a handle to the current desktop).

But you wrote that SetThreadDesktop succeeded. The only explanation for this is that the thread which you applied SetThreadDesktop on didn't have any hooks or windows, so it wasn't the thread that had your already shown UI.

Unfortunately it's impossible to move windows from one desktop to another on Windows. The maximum you can do is creating the window on the specific desktop.

You may think that virtual desktop manager applications have a workaround. The fact is that most of these applications on Windows only mimic the multi-desktop behavior, but they simply show/hide the corresponding windows, taskbar, background, etc. on Winsta0\default desktop. One of the exceptions is SysInternals' Desktops application. But guess what, they have faced the same problem:

Desktops reliance on Windows desktop objects means that it cannot provide some of the functionality of other virtual desktop utilities, however. For example, Windows doesn't provide a way to move a window from one desktop object to another

like image 86
dbalogh Avatar answered Sep 20 '22 02:09

dbalogh