
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.
And the idea was this. Make it so that fast user switching happens in a single action. By pressing a hotkey. The search on the Internet (I remind you, it was 3 years ago) bore fruit, and similar solutions were found. But, free or buggy, or require the installation of some third-party software. A paid, high-quality one was found, and one worked very well, but, first, it was paid, and second, it contained extra functionality — by pressing the hotkey, the user did not immediately switch, but the window was displayed (similar to Alt + Tab) by users. It was decided to write your decision. The simplest, with a minimum of functionality: hotkey - switching.
Google issued:
- To switch sessions, use the functions wtsapi32.dll: WTSEnumerateSessions , WTSConnectSession , WTSDisconnectSession (Now, when I look at the description of these functions, it says that it works with remote work sessions, and frankly, I am a little perplexed, but it works locally, flawlessly) .
- For hotkeys, use the user32.dll functions: RegisterHotKey , UnregisterHotKey . It's simple.
Immediately make a reservation, and you can throw tomatoes at me, but I wrote this thing on c #, although on the pros, it would certainly be better, more native and so on, and so on ... But, then I just started learning c # and I needed experience, and when the decision was written, it was not necessary to rewrite it, although its transfer would not take more than one evening.
So, for starters, it was written a simple win32 application with a button, which, when clicked, would run something like this:
private void SwitchUser() { IntPtr buffer = IntPtr.Zero; int count = 0;
With two sessions, sessionInfo will have 3 elements: a session of services, a session of the 1st user, a session of the 2nd user. Accordingly, targetSessId and activeSessId are uniquely determined. For sessions over 2, switching will occur between the active and the last inactive.
')
But then I suffered a small setback. Some could already guess that this would not work. When WTSConnectSession is executed from the application, the active user is disconnected, but the inclusion of the second user is not. Those. in other words, one user’s application cannot initiate another user’s login. But the service can do it! Yes, it’s a pity, but without system service we’ll fail. Well, let's create a system service in which we drop this code. This is where C # and .Net come in handy, since writing a service on these technologies is
very ,
very simple . Now the following problem occurs: the service does not have a user interface, i.e. the user cannot directly affect the operation of the service, and the service cannot hear the actions of the user. Hang a hot key on the service can not.
So, here is our solution:
The user application listens to the user, and when a hotkey is detected, it sends a signal to the system service, which performs the switch.
Very little is left, but even here I can find something to show you. For example, what we need is a desktop application, without windows, but for it to accept hotkeys. This can be done as everyone else does: Hide the main application window and do not show it. But there is a better solution. Write your
ApplicationContext .
With blackFor example:
internal class SUApplicationContext: ApplicationContext { private Hotkey hk; private Form form; private const int SWITCH_USER_COMMAND = 193; internal SUApplicationContext() {
Here I use the
MovablePython.Hotkey interface found on the Internet over user32.dll functions RegisterHotKey, UnregisterHotKey.
And a couple of lines about the service itself.
protected override void OnCustomCommand(int command) { base.OnCustomCommand(command); if (command == SWITCH_USER_COMMAND) { SwitchUser(); } }
We override the OnCustomCommand event, and when we receive our command, we perform the function already known to us.
It remains to register and start the service, and put an application in the autoload for each user.
Everything. Now, after the first user has logged in after starting the computer and pressed Win + A, his session is disconnected, and the user selection window appears. The second user enters, presses Win + A - the first user session appears. Etc.
On github you can see the
source code . Or you can
download the entire project and compiled and ready-to-run executables.