-
Notifications
You must be signed in to change notification settings - Fork 1
/
SessionUtil.cpp
200 lines (165 loc) · 4.48 KB
/
SessionUtil.cpp
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// SessionUtil.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
void printError(TCHAR *messageFormat)
{
LPWSTR pErrorMsg;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), 0, (LPWSTR)&pErrorMsg, 0, NULL);
wprintf(messageFormat, pErrorMsg);
LocalFree(pErrorMsg);
}
void showBalloonTip(const TCHAR* msg)
{
static bool nidset = false;
static NOTIFYICONDATA nid = {};
if (nidset == false)
{
nid.cbSize = sizeof(nid);
nid.hIcon = LoadIcon(NULL, IDI_INFORMATION);
GUID tipGuid = {0x30375b04, 0x977b, 0x410f, { 0x9e, 0xb7, 0x46, 0xed, 0xb5, 0xf7, 0xf4, 0xe2 }};
nid.guidItem = tipGuid;
nid.uFlags = NIF_INFO | NIF_ICON | NIF_GUID;
nidset = true;
}
wcsncpy_s(nid.szInfo, msg, ARRAYSIZE(nid.szInfo));
Shell_NotifyIcon(nidset ? NIM_MODIFY : NIM_ADD, &nid);
//Shell_NotifyIcon(NIM_DELETE, &nid) ? S_OK : E_FAIL;
}
int findSession(TCHAR *userToFind)
{
int sessionId = -1;
WTS_SESSION_INFO *sessions;
DWORD count;
if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &sessions, &count))
{
printError(L"WTSEnumerateSessions failed: %s\n");
return -1;
}
for (DWORD i = 0; i < count; i++)
{
WCHAR *sessionUser;
DWORD bytes;
if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, sessions[i].SessionId, WTSUserName, &sessionUser, &bytes))
{
printError(L"WTSQuerySessionInformation error: %s\n");
continue;
}
#if DEBUG
wprintf(L" * Session %d: \n user: %s\n state: %d\n name: %s\n",
sessions[i].SessionId,
sessionUser,
sessions[i].State,
sessions[i].pWinStationName);
#endif
if (sessionUser != NULL && _wcsicmp(userToFind, sessionUser) == 0)
sessionId = sessions[i].SessionId;
WTSFreeMemory(sessionUser);
if (sessionId != -1)
break;
}
WTSFreeMemory(sessions);
return sessionId;
}
int switchUser(TCHAR *username, TCHAR *password)
{
int sessionId = findSession(username);
if (password == NULL)
password = L"";
if (sessionId == -1)
{
std::wstring msg;
msg.append(L"Session for ")
.append(username)
.append(L" not found");
wprintf(msg.c_str());
showBalloonTip(msg.c_str());
return 2;
}
if(!WTSConnectSession(sessionId, WTSGetActiveConsoleSessionId(), password, true))
{
wprintf(L"SessionId: %d\n", sessionId);
printError(L"WTSConnectSession error: %s\n");
return 2;
}
return 0;
}
int logoffUser(TCHAR *userToLogoff)
{
wprintf(L"Logging off %s\n", userToLogoff);
int sessionId = findSession(userToLogoff);
if (sessionId != -1)
{
wprintf(L" session %d\n", sessionId);
if (!WTSLogoffSession(WTS_CURRENT_SERVER_HANDLE, sessionId, TRUE))
{
printError(L"WTSLogoffSession failed: %s");
return 3;
}
else
wprintf(L" ...done.\n");
}
else
{
std::wstring msg;
msg.append(L"Session for ")
.append(userToLogoff)
.append(L" not found");
wprintf(msg.c_str());
showBalloonTip(msg.c_str());
return 2;
}
return 0;
}
void printUsage()
{
wprintf(
L"Usage:\n\
sessionutil switch\n\
sessionutil switch <username> <password>\n\
\n\
sessionutil logoff\n\
sessionutil logoff <username> [<username2> ...]\n\
\n\
sessionutil sleep\n\
"
);
}
int _tmain(int argc, TCHAR* argv[])
{
if (argc == 1)
{
printUsage();
return 1;
}
if (_wcsicmp(argv[1], L"SWITCH") == 0)
{
if (argc > 2)
return switchUser(argv[2], argv[3]);
else // go to Switch User screen
WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, false);
}
else if (_wcsicmp(argv[1], L"LOGOFF") == 0)
{
if (argc > 2)
{
for (int i = 2; i < argc; i++)
logoffUser(argv[i]);
}
else
{
WTSLogoffSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, true);
}
}
else if (_wcsicmp(argv[1], L"SLEEP") == 0)
{
SetSuspendState(/*bHibernate*/ FALSE,
/*bForce*/ FALSE,
/*bWakeupEventsDisabled*/ FALSE);
// Go to Switch User screen
WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, false);
}
else
{
printUsage();
}
}