1
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2
* Copyright (C) 2007 Constantin Kaplinsky. All Rights Reserved.
4
* This is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This software is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this software; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20
#include "TsSessions.h"
21
#include "DynamicFn.h"
24
#ifdef ERROR_CTX_WINSTATION_BUSY
25
#define RFB_HAVE_WINSTATION_CONNECT
27
#pragma message(" NOTE: Not building WinStationConnect support.")
30
// Windows XP (and later) functions used to handle session Ids
31
typedef BOOLEAN (WINAPI *_WinStationConnect_proto) (HANDLE,ULONG,ULONG,PCWSTR,ULONG);
32
DynamicFn<_WinStationConnect_proto> _WinStationConnect(_T("winsta.dll"), "WinStationConnectW");
33
typedef DWORD (WINAPI *_WTSGetActiveConsoleSessionId_proto) ();
34
DynamicFn<_WTSGetActiveConsoleSessionId_proto> _WTSGetActiveConsoleSessionId(_T("kernel32.dll"), "WTSGetActiveConsoleSessionId");
35
typedef BOOL (WINAPI *_ProcessIdToSessionId_proto) (DWORD, DWORD*);
36
DynamicFn<_ProcessIdToSessionId_proto> _ProcessIdToSessionId(_T("kernel32.dll"), "ProcessIdToSessionId");
37
typedef BOOL (WINAPI *_LockWorkStation_proto)();
38
DynamicFn<_LockWorkStation_proto> _LockWorkStation(_T("user32.dll"), "LockWorkStation");
41
ProcessSessionId::ProcessSessionId(DWORD processId) {
43
if (!_ProcessIdToSessionId.isValid())
45
if ((int)processId == -1)
46
processId = GetCurrentProcessId();
47
if (!(*_ProcessIdToSessionId)(GetCurrentProcessId(), &id))
48
vnclog.Print(LL_INTERR, VNCLOG("ProcessIdToSessionId failed (error %d)"), GetLastError());
51
ProcessSessionId mySessionId;
53
ConsoleSessionId::ConsoleSessionId() {
54
if (_WTSGetActiveConsoleSessionId.isValid())
55
id = (*_WTSGetActiveConsoleSessionId)();
60
bool inConsoleSession() {
61
ConsoleSessionId console;
62
return console.id == mySessionId.id;
65
void setConsoleSession(DWORD sessionId) {
66
#ifdef RFB_HAVE_WINSTATION_CONNECT
67
if (!_WinStationConnect.isValid()) {
68
vnclog.Print(LL_INTERR, VNCLOG("WinSta APIs missing"));
71
if ((int) sessionId == -1)
72
sessionId = mySessionId.id;
74
// Try to reconnect our session to the console
75
ConsoleSessionId console;
76
vnclog.Print(LL_INTINFO, VNCLOG("Console session is %d"), console.id);
77
if (!(*_WinStationConnect)(0, sessionId, console.id, L"", 0)) {
78
vnclog.Print(LL_INTERR, VNCLOG("Unable to connect session to Console (error %d)"), GetLastError());
82
// Lock the newly connected session, for security
83
if (_LockWorkStation.isValid())
84
(*_LockWorkStation)();
86
vnclog.Print(LL_INTERR, VNCLOG("setConsoleSession not implemented"));