1 // Copyright (c) 2011-2016 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 #include "BackgroundDesktop.h"
25 #include "DebugClient.h"
26 #include "StringUtil.h"
27 #include "WinptyException.h"
31 static std::wstring getObjectName(HANDLE object) {
33 DWORD lengthNeeded = 0;
34 GetUserObjectInformationW(object, UOI_NAME,
37 ASSERT(lengthNeeded % sizeof(wchar_t) == 0);
38 std::unique_ptr<wchar_t[]> tmp(
39 new wchar_t[lengthNeeded / sizeof(wchar_t)]);
40 success = GetUserObjectInformationW(object, UOI_NAME,
41 tmp.get(), lengthNeeded,
44 throwWindowsError(L"GetUserObjectInformationW failed");
46 return std::wstring(tmp.get());
49 static std::wstring getDesktopName(HWINSTA winsta, HDESK desk) {
50 return getObjectName(winsta) + L"\\" + getObjectName(desk);
53 } // anonymous namespace
55 // Get a non-interactive window station for the agent.
56 // TODO: review security w.r.t. windowstation and desktop.
57 BackgroundDesktop::BackgroundDesktop() {
59 m_originalStation = GetProcessWindowStation();
60 if (m_originalStation == nullptr) {
62 L"BackgroundDesktop ctor: "
63 L"GetProcessWindowStation returned NULL");
66 CreateWindowStationW(nullptr, 0, WINSTA_ALL_ACCESS, nullptr);
67 if (m_newStation == nullptr) {
69 L"BackgroundDesktop ctor: CreateWindowStationW returned NULL");
71 if (!SetProcessWindowStation(m_newStation)) {
73 L"BackgroundDesktop ctor: SetProcessWindowStation failed");
75 m_newDesktop = CreateDesktopW(
76 L"Default", nullptr, nullptr, 0, GENERIC_ALL, nullptr);
77 if (m_newDesktop == nullptr) {
79 L"BackgroundDesktop ctor: CreateDesktopW failed");
81 m_newDesktopName = getDesktopName(m_newStation, m_newDesktop);
82 TRACE("Created background desktop: %s",
83 utf8FromWide(m_newDesktopName).c_str());
90 void BackgroundDesktop::dispose() WINPTY_NOEXCEPT {
91 if (m_originalStation != nullptr) {
92 SetProcessWindowStation(m_originalStation);
93 m_originalStation = nullptr;
95 if (m_newDesktop != nullptr) {
96 CloseDesktop(m_newDesktop);
97 m_newDesktop = nullptr;
99 if (m_newStation != nullptr) {
100 CloseWindowStation(m_newStation);
101 m_newStation = nullptr;
105 std::wstring getCurrentDesktopName() {
106 // MSDN says that the handles returned by GetProcessWindowStation and
107 // GetThreadDesktop do not need to be passed to CloseWindowStation and
108 // CloseDesktop, respectively.
109 const HWINSTA winsta = GetProcessWindowStation();
110 if (winsta == nullptr) {
112 L"getCurrentDesktopName: "
113 L"GetProcessWindowStation returned NULL");
115 const HDESK desk = GetThreadDesktop(GetCurrentThreadId());
116 if (desk == nullptr) {
118 L"getCurrentDesktopName: "
119 L"GetThreadDesktop returned NULL");
121 return getDesktopName(winsta, desk);