locked
Determine the current Active session of user in Windows Server 2008 RRS feed

  • Question

  • Hi,

    I have an requirement of invoking a batch file through terminal services.
    I have used CreateProcessAsUSer API to invoke an batch from service.

    This method used to work fine in WindowsXP. To make it work for Windows Vista and Windows 7
    operating system as sessionid is different for applications and services I was forced to add below code to get the token associated with the sessionid related to user.

    DWORD dwSessionid = GetActiveSessionId();

    //get Active UI session
    bRet = WTSQueryUserToken (dwSessionid, &hTokenthis);

    if (!LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&luid))
       {
           printf("Lookup Privilege value Error: %u\n",GetLastError());
       }
       tp.PrivilegeCount =1;
       tp.Privileges[0].Luid =luid;
       tp.Privileges[0].Attributes =SE_PRIVILEGE_ENABLED;

        bRet = false;
        bRet =  DuplicateTokenEx(hTokenthis, MAXIMUM_ALLOWED,NULL, SecurityIdentification, TokenPrimary, &htoken);

        //Adjust Token privilege
        SetTokenInformation(hTokenthis,TokenSessionId,(void*)dwSessionid,sizeof(DWORD));

        if (!AdjustTokenPrivileges(hTokenthis,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),(PTOKEN_PRIVILEGES)NULL,NULL))
       {
           int abc =GetLastError();
           printf("Adjust Privilege value Error: %u\n",GetLastError());     
       }

       if (GetLastError()== ERROR_NOT_ALL_ASSIGNED)
       {
         printf("Token does not have the provilege\n");
       }

       LPVOID pEnv =NULL;

       if(CreateEnvironmentBlock(&pEnv,hUserTokenDup,TRUE))
       {
           dwCreationFlags|=CREATE_UNICODE_ENVIRONMENT;
       }
       else
          pEnv=NULL;

       -----------------------------------   
       Call CreateProcessAsUser   
       -----------------------------------



    //Definition of GetActiveSessionId
    DWORD GetActiveSessionId()
    {
        PHANDLE currentToken = 0;
        PHANDLE primaryToken = 0;

        int dwSessionId = 0;
        PHANDLE hUserToken = 0;
        PHANDLE hTokenDup = 0;

        PWTS_SESSION_INFO pSessionInfo = 0;
        DWORD dwCount = 0;

        WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &dwCount);

        int dataSize = sizeof(WTS_SESSION_INFO);

        for (DWORD i = 0; i < dwCount; ++i)
        {
            WTS_SESSION_INFO si = pSessionInfo[i];
            if (WTSActive == si.State)
            {
                dwSessionId = si.SessionId;
                break;
            }
        }

        return dwSessionId;
    }

    After adding the above code, my code works for XP, Vista and Windows 7. BUT, Windows Server 2008
    can have more than one active sessions at a time and it also allows the same user to login from machines. For ex: Server 2008 allows the user "Administrator" to login to in x machine and with the same user "Administrator" server 2008 allows to login from y machine. Therefore there can be more than one active sessions from same/different users.

    Is there any way to determine the current Active session of user?

    Wednesday, May 25, 2011 5:01 AM

All replies

  • On client systems, you can only have a single Active Session.  This isn't the case for a Server system so it is not clear where you need to run the batch file.  You would need to determine which session you want to run the batch file on.

    thanks

    Frank K [MSFT]

    Follow us on Twitter, www.twitter.com/WindowsSDK

    Wednesday, January 7, 2015 5:59 AM