This source file includes following definitions.
- LookupTerminalId
- LookupSessionId
#include "remoting/host/win/wts_terminal_monitor.h"
#include <windows.h>
#include <wtsapi32.h>
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
namespace remoting {
const uint32 kInvalidSessionId = 0xffffffffu;
const char* WtsTerminalMonitor::kConsole = "console";
WtsTerminalMonitor::~WtsTerminalMonitor() {
}
bool WtsTerminalMonitor::LookupTerminalId(uint32 session_id,
std::string* terminal_id) {
if (session_id == WTSGetActiveConsoleSessionId()) {
*terminal_id = kConsole;
return true;
}
DWORD bytes;
wchar_t* working_directory;
if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
session_id,
WTSWorkingDirectory,
&working_directory,
&bytes)) {
return false;
}
bool result = base::WideToUTF8(working_directory,
(bytes / sizeof(wchar_t)) - 1,
terminal_id);
WTSFreeMemory(working_directory);
return result;
}
uint32 WtsTerminalMonitor::LookupSessionId(const std::string& terminal_id) {
if (terminal_id == kConsole)
return WTSGetActiveConsoleSessionId();
WTS_SESSION_INFO* session_info;
DWORD session_info_count;
if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &session_info,
&session_info_count)) {
LOG_GETLASTERROR(ERROR) << "Failed to enumerate all sessions";
return kInvalidSessionId;
}
for (DWORD i = 0; i < session_info_count; ++i) {
uint32 session_id = session_info[i].SessionId;
std::string id;
if (LookupTerminalId(session_id, &id) && terminal_id == id) {
WTSFreeMemory(session_info);
return session_id;
}
}
WTSFreeMemory(session_info);
return kInvalidSessionId;
}
WtsTerminalMonitor::WtsTerminalMonitor() {
}
}