Search code examples
c++windowswinapiusersession

WTS_CONNECTSTATE_CLASS enumeration class


I have to determine what state the user session is in. i.e if the the user is logged on and active or if the user is not logged on. To do this im using the function WTSEnumerateSessions and then I am checking the WTS_SESSION_INFOwhich is return from this function which has a state member.

According to the online documentation.

WTSActive A user is logged on to the WinStation

WTSDisconnected The WinStation is active but the client is disconnected.

The problem is that im currently logged onto my system but the state returned for my current session is WTSDisconnected im expecting the state to be WTSActive is there any reason as to why this is happening?

Code below

PWTS_SESSION_INFO pSessionInfo(nullptr);
    DWORD countOfSessions(0);
    DWORD sessionIndex(0);

    if (WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE,     // local machine
        0,                              // reserved, must be zero
        1,                              // Version, must be one
        &pSessionInfo,
        &countOfSessions) == FALSE)
    {

        return false;
    }

    if (countOfSessions == 0 || !pSessionInfo)
    {
        return false;
    }

    const DWORD currentSession = GetRealSessionID(CurrentSession); //This case we are in session 0
    for (; sessionIndex < countOfSessions; ++sessionIndex)
    {
        WTS_SESSION_INFO& sessionInfo = pSessionInfo[sessionIndex];
        if (currentSession == sessionInfo.SessionId)
        {
            if (sessionInfo.State == WTSActive)
            {
                return true;
            }
        }
    }


    return false;

Thanks


Solution

  • This doesn't make much sense. The most likely explanation is that currentSession is being assigned a value of 0, which is the session ID for the non-interactive isolated session in which services run.