 I want to share with you a life hack, which I have been using daily for several years now. It works flawlessly, saves time. It so happened that my wife and I had different accounts on the same home computer. This is convenient: each has its own desktop, its own wallpaper, preferences, application settings, cookies in the browser. I can not even imagine now how to work under one account. But (without this “but” there would be no article), there is one small problem. Switch users. As is usually done: Start -> some button, depending on the system -> change user. A user selection screen appears. We poke into the necessary user. Yes, there is a combination of keys Win + L. After which again you need to poke a user change and an icon. Total at least 3 actions. Windows 8 made a noticeable improvement in this regard. Click the Win + user icon and click on another in the list. But this is without taking into account that there is a password on accounting. This is where substantial delays begin. Entering a password every time you switch will bother you very quickly. And I had to set a password for my account, because I needed remote access. Yes, it was possible to make another account for remote access, but my life hack was already ready for that moment, and it worked fine regardless of whether there are passwords on the account or not.
 I want to share with you a life hack, which I have been using daily for several years now. It works flawlessly, saves time. It so happened that my wife and I had different accounts on the same home computer. This is convenient: each has its own desktop, its own wallpaper, preferences, application settings, cookies in the browser. I can not even imagine now how to work under one account. But (without this “but” there would be no article), there is one small problem. Switch users. As is usually done: Start -> some button, depending on the system -> change user. A user selection screen appears. We poke into the necessary user. Yes, there is a combination of keys Win + L. After which again you need to poke a user change and an icon. Total at least 3 actions. Windows 8 made a noticeable improvement in this regard. Click the Win + user icon and click on another in the list. But this is without taking into account that there is a password on accounting. This is where substantial delays begin. Entering a password every time you switch will bother you very quickly. And I had to set a password for my account, because I needed remote access. Yes, it was possible to make another account for remote access, but my life hack was already ready for that moment, and it worked fine regardless of whether there are passwords on the account or not.private void SwitchUser() { IntPtr buffer = IntPtr.Zero; int count = 0; //   ,     if (WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, ref buffer, ref count)) { WTS_SESSION_INFO[] sessionInfo = new WTS_SESSION_INFO[count]; //   : //       for (int index = 0; index < count; index++) sessionInfo[index] = (WTS_SESSION_INFO)Marshal.PtrToStructure((IntPtr)((int)buffer + (Marshal.SizeOf(new WTS_SESSION_INFO()) * index)), typeof(WTS_SESSION_INFO)); int activeSessId = -1; int targetSessId = -1; //  Id ,    // 0 ,   "Services" for (int i = 1; i < count; i++) { if (sessionInfo[i].State == WTS_CONNECTSTATE_CLASS.WTSDisconnected) targetSessId = sessionInfo[i].SessionId; else if (sessionInfo[i].State == WTS_CONNECTSTATE_CLASS.WTSActive) activeSessId = sessionInfo[i].SessionId; } if ((activeSessId > 0) && (targetSessId > 0)) { //    ,    . WTSConnectSession(Convert.ToUInt64(targetSessId), Convert.ToUInt64(activeSessId), "", false); } else { //   .   (    ) WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE, activeSessId, false); } } //    WTSFreeMemory(buffer); }  internal class SUApplicationContext: ApplicationContext { private Hotkey hk; private Form form; private const int SWITCH_USER_COMMAND = 193; internal SUApplicationContext() { //   ,     //    form = new Form(); //    Win+A hk = new Hotkey(Keys.A, false, false, false, true); //    hk.Pressed += delegate { SendSwitchCommand(); }; //  ,   if (hk.GetCanRegister(form)) hk.Register(form); //     Application.ApplicationExit += Application_ApplicationExit; } private void SendSwitchCommand() { //    ServiceController sc = new ServiceController("Sus"); try { //    sc.ExecuteCommand(SWITCH_USER_COMMAND); } catch (Exception ex) { MessageBox.Show(ex.Message); } } void Application_ApplicationExit(object sender, EventArgs e) { //     if (hk.Registered) hk.Unregister(); } } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new SUApplicationContext()); }  protected override void OnCustomCommand(int command) { base.OnCustomCommand(command); if (command == SWITCH_USER_COMMAND) { SwitchUser(); } } Source: https://habr.com/ru/post/178105/
All Articles