~ubuntu-branches/debian/lenny/italc/lenny

« back to all changes in this revision

Viewing changes to ica/win32/src/TsSessions.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Winnertz
  • Date: 2008-06-17 13:46:54 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20080617134654-2y5m7ki93r5c1ysf
Tags: upstream-1.0.9~rc3
ImportĀ upstreamĀ versionĀ 1.0.9~rc3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 
2
 * Copyright (C) 2007 Constantin Kaplinsky.  All Rights Reserved.
 
3
 * 
 
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.
 
8
 * 
 
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.
 
13
 * 
 
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,
 
17
 * USA.
 
18
 */
 
19
 
 
20
#include "TsSessions.h"
 
21
#include "DynamicFn.h"
 
22
#include <tchar.h>
 
23
 
 
24
#ifdef ERROR_CTX_WINSTATION_BUSY
 
25
#define RFB_HAVE_WINSTATION_CONNECT
 
26
#else
 
27
#pragma message("  NOTE: Not building WinStationConnect support.")
 
28
#endif
 
29
 
 
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");
 
39
 
 
40
 
 
41
ProcessSessionId::ProcessSessionId(DWORD processId) {
 
42
  id = 0;
 
43
  if (!_ProcessIdToSessionId.isValid())
 
44
    return;
 
45
  if ((int)processId == -1)
 
46
    processId = GetCurrentProcessId();
 
47
  if (!(*_ProcessIdToSessionId)(GetCurrentProcessId(), &id))
 
48
    vnclog.Print(LL_INTERR, VNCLOG("ProcessIdToSessionId failed (error %d)"), GetLastError());
 
49
}
 
50
 
 
51
ProcessSessionId mySessionId;
 
52
 
 
53
ConsoleSessionId::ConsoleSessionId() {
 
54
  if (_WTSGetActiveConsoleSessionId.isValid())
 
55
    id = (*_WTSGetActiveConsoleSessionId)();
 
56
  else
 
57
    id = 0;
 
58
}
 
59
 
 
60
bool inConsoleSession() {
 
61
  ConsoleSessionId console;
 
62
  return console.id == mySessionId.id;
 
63
}
 
64
 
 
65
void setConsoleSession(DWORD sessionId) {
 
66
#ifdef RFB_HAVE_WINSTATION_CONNECT
 
67
  if (!_WinStationConnect.isValid()) {
 
68
    vnclog.Print(LL_INTERR, VNCLOG("WinSta APIs missing"));
 
69
    return;
 
70
  }
 
71
  if ((int) sessionId == -1)
 
72
    sessionId = mySessionId.id;
 
73
 
 
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());
 
79
    return;
 
80
  }
 
81
 
 
82
  // Lock the newly connected session, for security
 
83
  if (_LockWorkStation.isValid())
 
84
    (*_LockWorkStation)();
 
85
#else
 
86
  vnclog.Print(LL_INTERR, VNCLOG("setConsoleSession not implemented"));
 
87
#endif
 
88
}