DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
ActiveUserSession.cpp
Go to the documentation of this file.
1
#include "
pch.h
"
2
#include "
System/Diagnostics/ActiveUserSession.h
"
3
#include "
System/UnauthorizedAccessException.h
"
4
#include "
System/Utils/StringConvert.h
"
5
6
#if defined(_WIN32)
7
#include <windows.h>
8
#include <wtsapi32.h>
9
#pragma comment(lib, "wtsapi32.lib")
10
#endif
11
12
namespace
DotNetDupe
{
13
namespace
System
{
14
namespace
Diagnostics
{
15
16
ActiveUserSession::ActiveUserSession
() {}
17
ActiveUserSession::~ActiveUserSession
() {}
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
56
Collections::Generic::List<UserSessionInfo>
ActiveUserSession::GetAllSessions
() {
58
Collections::Generic::List<UserSessionInfo>
lstSessions;
59
#if defined(_WIN32)
60
EnumerateWin32Sessions(lstSessions);
61
#endif
62
return
lstSessions;
63
}
64
65
Collections::Generic::List<UserSessionInfo>
ActiveUserSession::GetActiveSessions
() {
67
auto
lstAll =
GetAllSessions
();
68
Collections::Generic::List<UserSessionInfo>
lstActive;
69
for
(
int
i = 0; i < lstAll.GetCount(); i++) {
70
if
(lstAll[i].bIsActive) lstActive.
Add
(lstAll[i]);
71
}
72
return
lstActive;
73
}
74
75
Collections::Generic::List<UserSessionInfo>
ActiveUserSession::GetExpiredSessions
() {
77
auto
lstAll =
GetAllSessions
();
78
Collections::Generic::List<UserSessionInfo>
lstExpired;
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
}
ActiveUserSession.h
StringConvert.h
Utility routines for high-performance UTF-8, UTF-16, and wide-character string conversions.
UnauthorizedAccessException.h
The exception that is thrown when the operating system denies access because of an I/O error or a spe...
DotNetDupe::System::Collections::Generic::List
Represents a strongly typed list of objects accessible by index.
Definition
List.h:29
DotNetDupe::System::Collections::Generic::List::Add
void Add(const T &item)
Adds an object to the end of the List.
Definition
List.h:138
DotNetDupe::System::Diagnostics::ActiveUserSession::GetActiveSessions
static Collections::Generic::List< UserSessionInfo > GetActiveSessions()
Enumerates all sessions currently in the active interactive state.
Definition
ActiveUserSession.cpp:65
DotNetDupe::System::Diagnostics::ActiveUserSession::ActiveUserSession
ActiveUserSession()
Initializes a new instance of ActiveUserSession.
Definition
ActiveUserSession.cpp:16
DotNetDupe::System::Diagnostics::ActiveUserSession::GetExpiredSessions
static Collections::Generic::List< UserSessionInfo > GetExpiredSessions()
Enumerates all disconnected or inactive user sessions.
Definition
ActiveUserSession.cpp:75
DotNetDupe::System::Diagnostics::ActiveUserSession::GetAllSessions
static Collections::Generic::List< UserSessionInfo > GetAllSessions()
Enumerates all user sessions regardless of active state.
Definition
ActiveUserSession.cpp:56
DotNetDupe::System::Diagnostics::ActiveUserSession::~ActiveUserSession
virtual ~ActiveUserSession()
Virtual destructor for polymorphic cleanup.
Definition
ActiveUserSession.cpp:17
DotNetDupe::System::String
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition
String.h:74
DotNetDupe::System::String::String
String()
Initializes a new instance of the String class to an empty string.
Definition
String.cpp:64
DotNetDupe::System::UnauthorizedAccessException::UnauthorizedAccessException
UnauthorizedAccessException()
Initializes a new instance of the UnauthorizedAccessException class with a default message.
Definition
Exception.cpp:52
DotNetDupe::System::Diagnostics
Definition
ActiveUserSession.h:11
DotNetDupe::System::Diagnostics::PopulateSessionInfo
static void PopulateSessionInfo(const WTS_SESSION_INFOW &wtsInfo, UserSessionInfo &session)
Definition
ActiveUserSession.cpp:20
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe::System::Diagnostics::UserSessionInfo
Encapsulates details about an operating system interactive user session.
Definition
ActiveUserSession.h:15
DotNetDupe::System::Diagnostics::UserSessionInfo::uSessionId
unsigned long uSessionId
Terminal Services session identifier.
Definition
ActiveUserSession.h:16
DotNetDupe::System::Diagnostics::UserSessionInfo::sLogoutTimestamp
String sLogoutTimestamp
Formatted timestamp of session disconnection or active status.
Definition
ActiveUserSession.h:20
DotNetDupe::System::Diagnostics::UserSessionInfo::sPrivilege
String sPrivilege
User privilege level or role description.
Definition
ActiveUserSession.h:18
DotNetDupe::System::Diagnostics::UserSessionInfo::sUsername
String sUsername
Account name of the logged-on user.
Definition
ActiveUserSession.h:17
DotNetDupe::System::Diagnostics::UserSessionInfo::bIsActive
bool bIsActive
True if session is actively receiving user input.
Definition
ActiveUserSession.h:21
DotNetDupe::System::Diagnostics::UserSessionInfo::sLoginTimestamp
String sLoginTimestamp
Formatted timestamp of when session was established.
Definition
ActiveUserSession.h:19
DotNetDupe
ActiveUserSession.cpp
Generated by
1.18.0