title: SwitchDesktop works momentarily but switches back after a moment
body: <p>I've got some code to create a new desktop and launch a process into that desktop.</p>
  
  <p>One a few select Windows XP machines, when this code runs, I can see it switch to the new desktop and start the process, but almost immediately, the desktop switches back to the normal desktop.</p>
  
  <p>This code works fine on about 98% of machines, and I can't seem to isolate any reason for this not working on the others.</p>
  
  <p>Should <code>SwitchDesktop</code> be reliable?  Can I hook calls to <code>SwitchDesktop</code> that might be called from another application?</p>
  
  <p>My code:</p>
  
  <pre><code>int DLL_EXP_IMP WINAPI Process_Desktop(char *szDesktopName, char *szPath)
  {
      HDESK   hOriginalThread;
      HDESK   hOriginalInput;
      HDESK   hNewDesktop;
      int procSuccess;
      // Save original ...
      hOriginalThread = GetThreadDesktop(GetCurrentThreadId());
      hOriginalInput = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP);
  
      // Create a new Desktop and switch to it
      hNewDesktop = CreateDesktop(szDesktopName, NULL, NULL, DF_ALLOWOTHERACCOUNTHOOK, GENERIC_ALL, NULL);
      SetThreadDesktop(hNewDesktop);
      SwitchDesktop(hNewDesktop);
  
      // This call blocks until the process exits, and is confirmed to work on the affected machines
      procSuccess = StartProcess(szDesktopName, szPath);
  
      // Restore original ...
      SwitchDesktop(hOriginalInput);
      SetThreadDesktop(hOriginalThread);
  
      // Close the Desktop
      CloseDesktop(hNewDesktop);
  
      if (procSuccess != 0)
      {
          return procSuccess;
      }
      else
      {
          return 0;
      }
  }
  </code></pre>