Skip to content

Commit

Permalink
this worked
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Jun 8, 2022
1 parent f117fb8 commit e970f7c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/inc/HostSignals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft::Console
struct HostSignalSetForegroundData
{
uint32_t sizeInBytes;
uint32_t processId;
uint32_t processId; // THIS NEEDS TO BE A PID, NOT A HANDLE
bool isForeground;
};

Expand All @@ -30,4 +30,4 @@ namespace Microsoft::Console
uint32_t eventType;
uint32_t ctrlFlags;
};
};
};
4 changes: 4 additions & 0 deletions src/interactivity/base/HostSignalInputThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ T HostSignalInputThread::_ReceiveTypedPacket()
{
auto msg = _ReceiveTypedPacket<HostSignalSetForegroundData>();

// if (wil::unique_handle hClient{ OpenProcess(PROCESS_QUERY_INFORMATION, false, msg.processId) })
// {
// LOG_IF_NTSTATUS_FAILED(ServiceLocator::LocateConsoleControl()->SetForeground(hClient.get(), msg.isForeground));
// }
LOG_IF_NTSTATUS_FAILED(ServiceLocator::LocateConsoleControl()->SetForeground(ULongToHandle(msg.processId), msg.isForeground));

break;
Expand Down
29 changes: 29 additions & 0 deletions src/server/ProcessHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include "../host/globals.h"
#include "../host/telemetry.hpp"

#include "../interactivity/inc/ServiceLocator.hpp"

using namespace Microsoft::Console::Interactivity;
// Routine Description:
// - Constructs an instance of the ConsoleProcessHandle Class
// - NOTE: Can throw if allocation fails or if there is a console policy we do not understand.
Expand All @@ -34,6 +37,32 @@ ConsoleProcessHandle::ConsoleProcessHandle(const DWORD dwProcessId,
{
Telemetry::Instance().LogProcessConnected(_hProcess.get());
}

if (const auto& conhost{ ServiceLocator::LocateGlobals().handoffInboxConsoleHandle })
{
LOG_IF_WIN32_BOOL_FALSE(DuplicateHandle(GetCurrentProcess(),
_hProcess.get(),
conhost.get(),
_hProcessInConhost.put(),
0 /*dwDesiredAccess, ignored*/,
false,
DUPLICATE_SAME_ACCESS));
}
}

ConsoleProcessHandle::~ConsoleProcessHandle()
{
const auto& conhost{ ServiceLocator::LocateGlobals().handoffInboxConsoleHandle };
if (_hProcessInConhost && conhost)
{
LOG_IF_WIN32_BOOL_FALSE(DuplicateHandle(conhost.get(),
_hProcessInConhost.get(),
nullptr /* hTargetProcessHandle */,
nullptr /* lpTargetHandle, ignored */,
0 /*dwDesiredAccess, ignored*/,
false,
DUPLICATE_CLOSE_SOURCE));
}
}

// Routine Description:
Expand Down
3 changes: 2 additions & 1 deletion src/server/ProcessHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ConsoleProcessHandle
ConsoleProcessHandle(const DWORD dwProcessId,
const DWORD dwThreadId,
const ULONG ulProcessGroupId);
~ConsoleProcessHandle() = default;
~ConsoleProcessHandle();
ConsoleProcessHandle(const ConsoleProcessHandle&) = delete;
ConsoleProcessHandle(ConsoleProcessHandle&&) = delete;
ConsoleProcessHandle& operator=(const ConsoleProcessHandle&) & = delete;
Expand All @@ -59,6 +59,7 @@ class ConsoleProcessHandle
ULONG _ulTerminateCount;
ULONG const _ulProcessGroupId;
wil::unique_handle const _hProcess;
wil::unique_handle _hProcessInConhost;

mutable ULONG64 _processCreationTime;

Expand Down
4 changes: 2 additions & 2 deletions src/server/ProcessList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,9 @@ void ConsoleProcessList::ModifyConsoleProcessFocus(const bool fForeground)
{
const auto pProcessHandle = *it;

if (pProcessHandle->_hProcess != nullptr)
if (const auto& handleToUse = pProcessHandle->_hProcessInConhost ? pProcessHandle->_hProcessInConhost : pProcessHandle->_hProcess)
{
_ModifyProcessForegroundRights(pProcessHandle->_hProcess.get(), fForeground);
_ModifyProcessForegroundRights(handleToUse.get(), fForeground);
}

it = std::next(it);
Expand Down

0 comments on commit e970f7c

Please sign in to comment.