-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWtsUtils.cpp
More file actions
174 lines (142 loc) · 4.23 KB
/
WtsUtils.cpp
File metadata and controls
174 lines (142 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "Base.h"
#include "WtsUtils.h"
#include <ws2tcpip.h>
NS_BEGIN
//#################################################################################################
void WtsInit(void)
{
HANDLE hEvent = OpenEventW(SYNCHRONIZE, FALSE, L"Global\\TermSrvReadyEvent");
if(hEvent)
{
WaitForSingleObject(hEvent, INFINITE);
CloseHandle(hEvent);
}
}
//#################################################################################################
bool WtsGetCurrentSessionId(uint32_t &nSessionId)
{
bool bResult = false;
nSessionId = 0;
PWSTR pBuffer;
DWORD dwSize;
if(WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSSessionId, &pBuffer, &dwSize))
{
Assert(dwSize == sizeof(uint32_t));
nSessionId = *(uint32_t*)pBuffer;
WTSFreeMemory(pBuffer);
bResult = true;
}
return bResult;
}
//#################################################################################################
bool WtsGetDomainName(CStrW &strDomain, const uint32_t nSessionId)
{
bool bResult = false;
strDomain.Empty();
PWSTR sz;
DWORD dwSize;
if(WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, nSessionId, WTSDomainName, &sz, &dwSize))
{
strDomain = sz;
WTSFreeMemory(sz);
bResult = true;
}
return bResult;
}
//#################################################################################################
bool WtsGetUserName(CStrW &strUser, const uint32_t nSessionId)
{
bool bResult = false;
strUser.Empty();
PWSTR sz;
DWORD dwSize;
if(WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, nSessionId, WTSUserName, &sz, &dwSize))
{
strUser = sz;
WTSFreeMemory(sz);
bResult = true;
}
return bResult;
}
//#################################################################################################
bool WtsGetLogonTime(FILETIME &ftLogon, const uint32_t nSessionId)
{
bool bResult = false;
ftLogon = FILETIME_NULL;
PWSTR pBuffer;
DWORD dwSize;
if(WTSQuerySessionInformationW(nullptr, nSessionId, WTSSessionInfo, &pBuffer, &dwSize))
{
PWTSINFO pwtsi = (PWTSINFO)pBuffer;
ftLogon.dwHighDateTime = pwtsi->LogonTime.HighPart;
ftLogon.dwLowDateTime = pwtsi->LogonTime.LowPart;
WTSFreeMemory(pBuffer);
bResult = true;
}
return bResult;
}
//#################################################################################################
bool WtsIsRemoteLogon(bool &bRemote, const uint32_t nSessionId)
{
bool bResult = false;
bRemote = false;
PWSTR pBuffer;
DWORD dwSize;
if(WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, nSessionId, WTSIsRemoteSession, &pBuffer, &dwSize))
{
bRemote = std::any_of((PCBYTE)pBuffer, (PCBYTE)pBuffer + dwSize, [](const BYTE n){return (n != 0);});
WTSFreeMemory(pBuffer);
bResult = true;
}
return bResult;
}
//#################################################################################################
bool WtsGetRemoteName(CStrW &strRemote, const uint32_t nSessionId)
{
bool bResult = false;
strRemote.Empty();
PWSTR sz;
DWORD dwSize;
if(WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, nSessionId, WTSClientName, &sz, &dwSize))
{
strRemote = sz;
WTSFreeMemory(sz);
bResult = true;
}
return bResult;
}
//#################################################################################################
bool WtsGetRemoteAddress(CStrW &strAddress, const uint32_t nSessionId)
{
bool bResult = false;
strAddress.Empty();
PWSTR pBuffer;
DWORD dwSize;
if(WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, nSessionId, WTSClientAddress, &pBuffer, &dwSize))
{
PWTS_CLIENT_ADDRESS pWCA = (PWTS_CLIENT_ADDRESS)pBuffer;
if(pWCA->AddressFamily == AF_INET)
{
size_t nLength = 16;
auto szIP = std::make_unique<char[]>(nLength);
if(inet_ntop(AF_INET, pWCA->Address + 2, szIP.get(), nLength))
{
strAddress = szIP.get();
bResult = true;
}
}
else if(pWCA->AddressFamily == AF_INET6)
{
size_t nLength = 46;
auto szIP = std::make_unique<char[]>(nLength);
if(inet_ntop(AF_INET6, pWCA->Address, szIP.get(), nLength))
{
strAddress = szIP.get();
bResult = true;
}
}
WTSFreeMemory(pBuffer);
}
return bResult;
}
NS_END