XP-related programming for fast switching user functions

Source: Internet
Author: User

I don't know if you noticed, Windows XP has added a new feature to quickly switch users. It allows you to switch between users without logging off, and each user has its own separate profile and desktop. That is, when you switch to another user (that is, when you log in with another account), all of the programs that the previous user ran are still running and don't quit. This has brought new problems to our development. For example, if your application provides functionality that occurs when multiple users run concurrently, you must add code to the application to detect the situation and respond accordingly.

1. Detect if a user is running the application

We usually use mutexes to implement only one instance, and using CreateMutex () to generate a mutex is using the default method, that is, the scope of the mutex is not global and can only work in one session, The presence of multiple sessions under a switching user does not work, and therefore does not detect instances that are already running. The solution is simple, when using CreateMutex (), add the "Global" keyword before the name of the mutex.

m_hmutexapprunning = CreateMutex (NULL, FALSE, "Global\\esxpswitchuserapp");

2, the monitoring user is now in the switch user sometimes, we need to detect users to do the user to switch the operation of this behavior, so that our program can make corresponding processing. For example, we need to free some resources when the active user session is running, and regain the resource when it enters the active session state again. Fortunately, Microsoft provides a way for our programs to receive session notification messages. We can register to receive wm_wtssession_change messages by calling the WTSRegisterSessionNotification function.

The main messages are:

Wts_console_connect

Wts_console_disconnect

Wts_remote_connect

Wts_remote_disconnect

Wts_session_logon

Wts_session_logoff

Wts_session_lock

Wts_session_unlock

These message types are included in the wparam, and lparam contains the SessionID of the changed session.

switch(message)
{
  case WM_WTSSESSION_CHANGE:
  {
switch(wParam)
   {
   case WTS_CONSOLE_CONNECT:
   MessageBox("WTS_CONSOLE_CONNECT", "Esmile", MB_OK );
   break;
   case WTS_CONSOLE_DISCONNECT:
   MessageBox("WTS_CONSOLE_DISCONNECT",
    "Esmile", MB_OK );
   break;
   case WTS_SESSION_LOCK:
   MessageBox("WTS_SESSION_LOCK",
    "Esmile", MB_OK );
   break;
   case WTS_SESSION_UNLOCK:
   MessageBox("WTS_SESSION_UNLOCK",
    "Esmile", MB_OK );
   break;
   case WTS_SESSION_LOGOFF:
   MessageBox("WTS_SESSION_LOGOFF",
    "Esmile", MB_OK );
   break;
   case WTS_SESSION_LOGON:
   MessageBox("WTS_SESSION_LOGON",
    "Esmile", MB_OK );
   break;
   default:
   break;
   }
}
  break;
default:
  break;
}

In this way, we can deal with the news we are interested in, so as to achieve our goal.

3. Attention Matters

Each call to the WTSRegisterSessionNotification should match a call to the wtsunregistersessionnotification,

That is, to release this notification registration information when the program exits.

WTSRegisterSessionNotification and wtsunregistersessionnotification require header file Wtsapi32.h as well as

Wtsapi32.lib. Add the following #define statement before the statement that contains the Windows.h

#define _WIN32_WINNT 0x0501

The source code included with the article is compiled under Win2000pro, and the program can only run under Window XP because non-XP systems do not have WTSAPI32.dll.

Any question wants to discuss together with everybody!

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.