Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* 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 Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 20 | #include <rfb/LogWriter.h> |
| 21 | #include <rdr/Exception.h> |
| 22 | #include <tchar.h> |
Pierre Ossman | fc08bee | 2016-01-12 12:32:15 +0100 | [diff] [blame^] | 23 | #include <wtsapi32.h> |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 24 | |
| 25 | static rfb::LogWriter vlog("TsSessions"); |
| 26 | |
| 27 | namespace rfb { |
| 28 | namespace win32 { |
| 29 | |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 30 | ProcessSessionId::ProcessSessionId(DWORD processId) { |
| 31 | id = 0; |
Pierre Ossman | 5c23b9e | 2015-03-03 16:26:03 +0100 | [diff] [blame] | 32 | if (processId == (DWORD)-1) |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 33 | processId = GetCurrentProcessId(); |
Pierre Ossman | fc08bee | 2016-01-12 12:32:15 +0100 | [diff] [blame^] | 34 | if (!ProcessIdToSessionId(GetCurrentProcessId(), &id)) |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 35 | throw rdr::SystemException("ProcessIdToSessionId", GetLastError()); |
| 36 | } |
| 37 | |
| 38 | ProcessSessionId mySessionId; |
| 39 | |
| 40 | ConsoleSessionId::ConsoleSessionId() { |
Pierre Ossman | fc08bee | 2016-01-12 12:32:15 +0100 | [diff] [blame^] | 41 | id = WTSGetActiveConsoleSessionId(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | bool inConsoleSession() { |
| 45 | ConsoleSessionId console; |
| 46 | return console.id == mySessionId.id; |
| 47 | } |
| 48 | |
| 49 | void setConsoleSession(DWORD sessionId) { |
Pierre Ossman | 5c23b9e | 2015-03-03 16:26:03 +0100 | [diff] [blame] | 50 | if (sessionId == (DWORD)-1) |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 51 | sessionId = mySessionId.id; |
| 52 | |
| 53 | // Try to reconnect our session to the console |
| 54 | ConsoleSessionId console; |
Pierre Ossman | fb450fb | 2015-03-03 16:34:56 +0100 | [diff] [blame] | 55 | vlog.info("Console session is %lu", console.id); |
Pierre Ossman | fc08bee | 2016-01-12 12:32:15 +0100 | [diff] [blame^] | 56 | if (!WTSConnectSession(sessionId, console.id, (PTSTR)_T(""), 0)) |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 57 | throw rdr::SystemException("Unable to connect session to Console", GetLastError()); |
| 58 | |
| 59 | // Lock the newly connected session, for security |
Pierre Ossman | fc08bee | 2016-01-12 12:32:15 +0100 | [diff] [blame^] | 60 | LockWorkStation(); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | }; |
| 64 | }; |