public void ChangeDesktop5sec(string WinStationName, string DesktopName) { IntPtr SavedDefaultDesktop = GetThreadDesktop(GetCurrentThreadId()); IntPtr hwinstaCurrent = GetProcessWindowStation(); IntPtr hWinSta = WindowStation.OpenWindowStation(WinStationName, true, 0x37); WindowStation.SetProcessWindowStation(hWinSta); IntPtr OpenDesk = WindowStation.OpenDesktop(DesktopName, 0, false, (uint)DESKTOP_ACCESS.GENERIC_ALL); bool ret_switch = WindowStation.SwitchDesktop(OpenDesk); if (ret_switch == true) { if (checkBox3.Checked == false) { System.Threading.Thread.Sleep(5000); WindowStation.SetProcessWindowStation(hwinstaCurrent); WindowStation.SwitchDesktop(SavedDefaultDesktop); WindowStation.SetThreadDesktop(SavedDefaultDesktop); } } else { WindowStation.SetProcessWindowStation(hwinstaCurrent); MessageBox.Show("Switching to the selected desktop has been rejected.", "Faild to switch the desktop", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void enumwinstation() { IList <string> list = new List <string>(); GCHandle gch = GCHandle.Alloc(list); EnumWindowStationsDelegate childProc = new EnumWindowStationsDelegate(EnumWindowStationsCallback); EnumWindowStations(childProc, GCHandle.ToIntPtr(gch)); foreach (string ws in list) { List <string> desktops = WindowStation.EnumerateDesktops(ws); foreach (string desktop in desktops) { try { AddToList2(ws, desktop); } catch (Exception) { AddToList2("-", "-"); } } } }