DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
ActiveUserSession.cpp
Go to the documentation of this file.
1#include "pch.h"
5
6#if defined(_WIN32)
7#include <windows.h>
8#include <wtsapi32.h>
9#pragma comment(lib, "wtsapi32.lib")
10#endif
11
12namespace DotNetDupe {
13 namespace System {
14 namespace Diagnostics {
15
18
19#if defined(_WIN32)
20 static void PopulateSessionInfo(const WTS_SESSION_INFOW& wtsInfo, UserSessionInfo& session) {
22 LPWSTR pBuffer = NULL; DWORD dwBytes = 0;
23 String sUsername = "SYSTEM";
24 if (::WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, wtsInfo.SessionId, WTSUserName, &pBuffer, &dwBytes) && pBuffer && dwBytes > 2) {
25 sUsername = String(pBuffer);
26 ::WTSFreeMemory(pBuffer);
27 }
28
30 session.uSessionId = wtsInfo.SessionId;
31 session.sUsername = sUsername;
32 session.bIsActive = (wtsInfo.State == WTSActive);
33 session.sPrivilege = session.bIsActive ? "Administrator (Active Terminal)" : "Standard User";
34 session.sLoginTimestamp = "Active Session";
35 session.sLogoutTimestamp = session.bIsActive ? "Active Session" : "Session Disconnected";
36 }
37
38 void ActiveUserSession::EnumerateWin32Sessions(Collections::Generic::List<UserSessionInfo>& lstSessions) {
40 WTS_SESSION_INFOW* pSessionInfo = NULL;
41 DWORD dwSessionCount = 0;
42
43 if (::WTSEnumerateSessionsW(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &dwSessionCount) && pSessionInfo) {
44 for (DWORD i = 0; i < dwSessionCount; ++i) {
45 UserSessionInfo session;
46 PopulateSessionInfo(pSessionInfo[i], session);
47 lstSessions.Add(session);
48 }
49 ::WTSFreeMemory(pSessionInfo);
50 } else if (::GetLastError() == ERROR_ACCESS_DENIED) {
51 throw UnauthorizedAccessException("Access denied querying active user sessions. Administrator privileges required.");
52 }
53 }
54#endif
55
59#if defined(_WIN32)
60 EnumerateWin32Sessions(lstSessions);
61#endif
62 return lstSessions;
63 }
64
67 auto lstAll = GetAllSessions();
69 for (int i = 0; i < lstAll.GetCount(); i++) {
70 if (lstAll[i].bIsActive) lstActive.Add(lstAll[i]);
71 }
72 return lstActive;
73 }
74
77 auto lstAll = GetAllSessions();
79 for (int i = 0; i < lstAll.GetCount(); i++) {
80 if (!lstAll[i].bIsActive) lstExpired.Add(lstAll[i]);
81 }
82 return lstExpired;
83 }
84
85 }
86 }
87}
Utility routines for high-performance UTF-8, UTF-16, and wide-character string conversions.
The exception that is thrown when the operating system denies access because of an I/O error or a spe...
Represents a strongly typed list of objects accessible by index.
Definition List.h:29
void Add(const T &item)
Adds an object to the end of the List.
Definition List.h:138
static Collections::Generic::List< UserSessionInfo > GetActiveSessions()
Enumerates all sessions currently in the active interactive state.
ActiveUserSession()
Initializes a new instance of ActiveUserSession.
static Collections::Generic::List< UserSessionInfo > GetExpiredSessions()
Enumerates all disconnected or inactive user sessions.
static Collections::Generic::List< UserSessionInfo > GetAllSessions()
Enumerates all user sessions regardless of active state.
virtual ~ActiveUserSession()
Virtual destructor for polymorphic cleanup.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
UnauthorizedAccessException()
Initializes a new instance of the UnauthorizedAccessException class with a default message.
Definition Exception.cpp:52
static void PopulateSessionInfo(const WTS_SESSION_INFOW &wtsInfo, UserSessionInfo &session)
Encapsulates details about an operating system interactive user session.
unsigned long uSessionId
Terminal Services session identifier.
String sLogoutTimestamp
Formatted timestamp of session disconnection or active status.
String sPrivilege
User privilege level or role description.
String sUsername
Account name of the logged-on user.
bool bIsActive
True if session is actively receiving user input.
String sLoginTimestamp
Formatted timestamp of when session was established.