Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/vncdll/winvnc/TsSessions.cpp
Views: 11780
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.1* Copyright (C) 2007 Constantin Kaplinsky. All Rights Reserved.2*3* This is free software; you can redistribute it and/or modify4* it under the terms of the GNU General Public License as published by5* the Free Software Foundation; either version 2 of the License, or6* (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 of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*13* You should have received a copy of the GNU General Public License14* along with this software; if not, write to the Free Software15* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,16* USA.17*/1819#include "TsSessions.h"20#include "DynamicFn.h"21#include <tchar.h>2223#ifdef ERROR_CTX_WINSTATION_BUSY24#define RFB_HAVE_WINSTATION_CONNECT25#else26#pragma message(" NOTE: Not building WinStationConnect support.")27#endif2829// Windows XP (and later) functions used to handle session Ids30typedef BOOLEAN (WINAPI *_WinStationConnect_proto) (HANDLE,ULONG,ULONG,PCWSTR,ULONG);31DynamicFn<_WinStationConnect_proto> _WinStationConnect(_T("winsta.dll"), "WinStationConnectW");32typedef DWORD (WINAPI *_WTSGetActiveConsoleSessionId_proto) ();33DynamicFn<_WTSGetActiveConsoleSessionId_proto> _WTSGetActiveConsoleSessionId(_T("kernel32.dll"), "WTSGetActiveConsoleSessionId");34typedef BOOL (WINAPI *_ProcessIdToSessionId_proto) (DWORD, DWORD*);35DynamicFn<_ProcessIdToSessionId_proto> _ProcessIdToSessionId(_T("kernel32.dll"), "ProcessIdToSessionId");36typedef BOOL (WINAPI *_LockWorkStation_proto)();37DynamicFn<_LockWorkStation_proto> _LockWorkStation(_T("user32.dll"), "LockWorkStation");383940ProcessSessionId::ProcessSessionId(DWORD processId) {41id = 0;42if (!_ProcessIdToSessionId.isValid())43return;44if (processId == -1)45processId = GetCurrentProcessId();46(*_ProcessIdToSessionId)(GetCurrentProcessId(), &id);47}4849ProcessSessionId mySessionId;5051ConsoleSessionId::ConsoleSessionId() {52if (_WTSGetActiveConsoleSessionId.isValid())53id = (*_WTSGetActiveConsoleSessionId)();54else55id = 0;56}5758bool inConsoleSession() {59ConsoleSessionId console;60return console.id == mySessionId.id;61}6263void setConsoleSession(DWORD sessionId) {64#ifdef RFB_HAVE_WINSTATION_CONNECT65if (!_WinStationConnect.isValid()) {66return;67}68if (sessionId == -1)69sessionId = mySessionId.id;7071// Try to reconnect our session to the console72ConsoleSessionId console;73if (!(*_WinStationConnect)(0, sessionId, console.id, L"", 0)) {74return;75}7677// Lock the newly connected session, for security78if (_LockWorkStation.isValid())79(*_LockWorkStation)();80#else8182#endif83}848586