blob: 6d16adbffb76b49c5eeaae791d270f8aa22234c7 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <rfb_win32/TsSessions.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000020#include <rfb/LogWriter.h>
21#include <rdr/Exception.h>
22#include <tchar.h>
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010023#include <wtsapi32.h>
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000024
25static rfb::LogWriter vlog("TsSessions");
26
27namespace rfb {
28namespace win32 {
29
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000030 ProcessSessionId::ProcessSessionId(DWORD processId) {
31 id = 0;
Pierre Ossman5c23b9e2015-03-03 16:26:03 +010032 if (processId == (DWORD)-1)
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000033 processId = GetCurrentProcessId();
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010034 if (!ProcessIdToSessionId(GetCurrentProcessId(), &id))
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000035 throw rdr::SystemException("ProcessIdToSessionId", GetLastError());
36 }
37
38 ProcessSessionId mySessionId;
39
40 ConsoleSessionId::ConsoleSessionId() {
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010041 id = WTSGetActiveConsoleSessionId();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000042 }
43
44 bool inConsoleSession() {
45 ConsoleSessionId console;
46 return console.id == mySessionId.id;
47 }
48
49 void setConsoleSession(DWORD sessionId) {
Pierre Ossman5c23b9e2015-03-03 16:26:03 +010050 if (sessionId == (DWORD)-1)
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000051 sessionId = mySessionId.id;
52
53 // Try to reconnect our session to the console
54 ConsoleSessionId console;
Pierre Ossmanfb450fb2015-03-03 16:34:56 +010055 vlog.info("Console session is %lu", console.id);
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010056 if (!WTSConnectSession(sessionId, console.id, (PTSTR)_T(""), 0))
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000057 throw rdr::SystemException("Unable to connect session to Console", GetLastError());
58
59 // Lock the newly connected session, for security
Pierre Ossmanfc08bee2016-01-12 12:32:15 +010060 LockWorkStation();
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000061 }
62
63};
64};