Repository: microsoft/terminal
File: src/server/ProcessHandle.cpp (C++)

Code before:
    //   console.
    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()
{
    // Close out the handle in the origin conhost.
    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:
// - Creates a CD_CONNECTION_INFORMATION (packet) that communicates the
//   process, input and output handles to the driver as transformed by the
//   DeviceComm's handle exchanger.
// Arguments:
// - deviceComm: IDeviceComm implementation with which to exchange handles.
CD_CONNECTION_INFORMATION ConsoleProcessHandle::GetConnectionInformation(IDeviceComm* deviceComm) const
{
    CD_CONNECTION_INFORMATION result = { 0 };
    result.Process = deviceComm->PutHandle(this);
    result.Input = deviceComm->PutHandle(pInputHandle.get());
    result.Output = deviceComm->PutHandle(pOutputHandle.get());
    return result;
}

// Routine Description:
// - Retrieves the policies set on this particular process handle
// - This specifies restrictions that may apply to the calling console client application
const ConsoleProcessPolicy ConsoleProcessHandle::GetPolicy() const
{
    return _policy;
}

Review comment (suggestion):
HAAHAHAHAHA OH GOD


I read your description and was all like, "Are we about to cause a handle leak in conhost"

Here I see that my concerns were unfounded

Code after:
    return result;
}

// Routine Description:
// - Retrieves the policies set on this particular process handle
// - This specifies restrictions that may apply to the calling console client application
const ConsoleProcessPolicy ConsoleProcessHandle::GetPolicy() const
{
    return _policy;
}

// Routine Description:
// - Retrieves the policies set on this particular process handle
// - This specifies compatibility shims that we might need to make for certain applications.
const ConsoleShimPolicy ConsoleProcessHandle::GetShimPolicy() const
{
    return _shimPolicy;
}

// Routine Description:
// - Retrieves the raw process handle
const HANDLE ConsoleProcessHandle::GetRawHandle() const
{
    return _hProcess.get();
}

// Routine Description:
// - Retrieves the process creation time (currently used in telemetry traces)
// - The creation time is lazily populated on first call
const ULONG64 ConsoleProcessHandle::GetProcessCreationTime() const
{
    if (_processCreationTime == 0 && _hProcess != nullptr)
    {
        FILETIME ftCreationTime, ftDummyTime = { 0 };
        ULARGE_INTEGER creationTime = { 0 };

        if (::GetProcessTimes(_hProcess.get(),
                              &ftCreationTime,
                              &ftDummyTime,
                              &ftDummyTime,
                              &ftDummyTime))
        {
            creationTime.HighPart = ftCreationTime.dwHighDateTime;
            creationTime.LowPart = ftCreationTime.dwLowDateTime;
        }

        _processCreationTime = creationTime.QuadPart;
    }

    return _processCreationTime;
}