Click here to Skip to main content
15,885,032 members
Articles / Programming Languages / C++

Enumerating Logon Sessions

Rate me:
Please Sign up or sign in to vote.
4.92/5 (32 votes)
21 Jun 2004CPOL23 min read 217.1K   4.6K   80  
An article on enumerating logon sessions, specifically interactive logon sessions on NT based operating systems.
#ifndef __LOGONSESSIONDATA_H__
#define __LOGONSESSIONDATA_H__

/*
Copyright 2004 by Stefan Kuhr
*/


#include <ntsecapi.h>
#include <wtsapi32.h>

// The following constant may be defined by including NtStatus.h.

#ifndef STATUS_SUCCESS
#define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
#endif

#define HAVE_RECENT_PLATFORMSDK 1

#if !HAVE_RECENT_PLATFORMSDK
typedef struct _SECURITY_LOGON_SESSION_DATA {
    ULONG               Size ;
    LUID                LogonId ;
    LSA_UNICODE_STRING  UserName ;
    LSA_UNICODE_STRING  LogonDomain ;
    LSA_UNICODE_STRING  AuthenticationPackage ;
    ULONG               LogonType ;
    ULONG               Session ;
    PSID                Sid ;
    LARGE_INTEGER       LogonTime ;

    //
    // new for whistler:
    //

    LSA_UNICODE_STRING  LogonServer ;
    LSA_UNICODE_STRING  DnsDomainName ;
    LSA_UNICODE_STRING  Upn ;
} SECURITY_LOGON_SESSION_DATA, * PSECURITY_LOGON_SESSION_DATA ;
#endif

class CLogonSessionData
{
    /// class member variables:
    LPWSTR m_szUserName;
    LPWSTR m_szLogonDomain;
    LPWSTR m_szAuthenticationPackage;;
    ULONG m_ulLogonType;
    ULONG m_ulSession;
    LPWSTR m_szUserSID;
    LARGE_INTEGER m_liLogonTime;
    DWORD m_dwFlags;
    LUID m_Luid;

    /// operators and member functions
    void SetFlags(DWORD dw){m_dwFlags|=dw;}
    BOOL Initialize(const PSECURITY_LOGON_SESSION_DATA);
    BOOL Initialize(HANDLE hToken, PLUID pLogonId, PWTS_PROCESS_INFO pWtsInfo);

protected:
    /// 
    /// copy ctor and assignement operator made protected: If you really
    /// insist on tampering with CLogonSessionData objects, then derive
    /// your own class from it and do whatever you want!
    /// 

    CLogonSessionData(const CLogonSessionData &);
    CLogonSessionData & operator = (const CLogonSessionData &me);

public:
    CLogonSessionData(void);    
    ~CLogonSessionData(void);    
    DWORD GetFlags(void)const{return m_dwFlags;}

    LPCWSTR GetUserName(void)const {return m_szUserName;}
    LPCWSTR GetLogonDomain(void)const {return m_szLogonDomain;}
    LPCWSTR GetAuthenticationPackage(void)const {return m_szAuthenticationPackage;}
    LPCWSTR GetUserSID(void)const {return m_szUserSID;}
    ULONG GetLogonType(void)const {return m_ulLogonType;}
    ULONG GetTSSession(void) const {return m_ulSession;}
    LARGE_INTEGER GetLogonTime(void)const{return m_liLogonTime;}
    LUID GetLuid(void)const{return m_Luid;}
    enum Flags
    {
        VOID_STATE  = 0,          
        INITIALIZED = 1,
        STALE       = 2
    };

    /// 
    /// We make EnumLogonSessions/EnumNT4StyleInteractiveSessions a "friend"  
    /// function in order to be able to make SetFlags and Initialize private. 
    /// This way, ctors and dtor are the only  non-const public member 
    /// functions which is a very desirable effect.
    /// 

    friend BOOL EnumLogonSessions(CLogonSessionData *pLogonSessionData, PULONG lpulSessions);
    friend BOOL EnumNT4StyleInteractiveSessions(CLogonSessionData *pLogonSessionData, PULONG lpulSessions);
};



#endif /// __LOGONSESSIONDATA_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
Stefan has been writing programs in C/C++ since 1991, and for Windows since 1993. He holds a German engineer's degree Dipl.-Ing. (FH) in "Microelectronics/Technical Computer Science" from the Aalen (Germany) University of Applied Sciences and an MSc in "Software Technology" from the Stuttgart (Germany) University of Applied Sciences. Currently, he is employed by a software company in the south-west of Germany that specializes in PC life-cycle products and software deployment technology. In his spare time, Stefan likes to go swimming and enjoys listening to jazz music from the fifties. And yes, he has a Weblog at http://mcblogs.craalse.de/sku (German only).

Comments and Discussions