Skip to content
This repository has been archived by the owner on Jul 26, 2023. It is now read-only.

Commit

Permalink
Add pseudo console APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Jul 2, 2020
1 parent 4a25649 commit 798afc4
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Kernel32/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
PInvoke.Kernel32.CreatePseudoConsoleFlags
PInvoke.Kernel32.CreatePseudoConsoleFlags.None = 0 -> PInvoke.Kernel32.CreatePseudoConsoleFlags
PInvoke.Kernel32.CreatePseudoConsoleFlags.PSEUDOCONSOLE_INHERIT_CURSOR = 1 -> PInvoke.Kernel32.CreatePseudoConsoleFlags
PInvoke.Kernel32.ErrorModes
PInvoke.Kernel32.ErrorModes.SEM_DEFAULT = 0 -> PInvoke.Kernel32.ErrorModes
PInvoke.Kernel32.ErrorModes.SEM_FAILCRITICALERRORS = 1 -> PInvoke.Kernel32.ErrorModes
Expand Down Expand Up @@ -75,6 +78,9 @@ PInvoke.Kernel32.STARTUPINFO.lpReserved_IntPtr.set -> void
PInvoke.Kernel32.STARTUPINFO.lpTitle -> char*
PInvoke.Kernel32.STARTUPINFO.lpTitle_IntPtr.get -> System.IntPtr
PInvoke.Kernel32.STARTUPINFO.lpTitle_IntPtr.set -> void
PInvoke.Kernel32.SafePseudoConsoleHandle
PInvoke.Kernel32.SafePseudoConsoleHandle.SafePseudoConsoleHandle() -> void
PInvoke.Kernel32.SafePseudoConsoleHandle.SafePseudoConsoleHandle(System.IntPtr preexistingHandle, bool ownsHandle = true) -> void
PInvoke.Kernel32.VER_CONDITION
PInvoke.Kernel32.VER_CONDITION.VER_AND = 6 -> PInvoke.Kernel32.VER_CONDITION
PInvoke.Kernel32.VER_CONDITION.VER_EQUAL = 1 -> PInvoke.Kernel32.VER_CONDITION
Expand All @@ -92,6 +98,8 @@ PInvoke.Kernel32.VER_MASK.VER_PRODUCT_TYPE = 128 -> PInvoke.Kernel32.VER_MASK
PInvoke.Kernel32.VER_MASK.VER_SERVICEPACKMAJOR = 32 -> PInvoke.Kernel32.VER_MASK
PInvoke.Kernel32.VER_MASK.VER_SERVICEPACKMINOR = 16 -> PInvoke.Kernel32.VER_MASK
PInvoke.Kernel32.VER_MASK.VER_SUITENAME = 64 -> PInvoke.Kernel32.VER_MASK
override PInvoke.Kernel32.SafePseudoConsoleHandle.IsInvalid.get -> bool
override PInvoke.Kernel32.SafePseudoConsoleHandle.ReleaseHandle() -> bool
static PInvoke.Kernel32.CompareFileTime(PInvoke.Kernel32.FILETIME lpFileTime1, PInvoke.Kernel32.FILETIME lpFileTime2) -> int
static PInvoke.Kernel32.CompareFileTime(System.IntPtr lpFileTime1, System.IntPtr lpFileTime2) -> int
static PInvoke.Kernel32.FILETIME.explicit operator System.DateTime(PInvoke.Kernel32.FILETIME fileTime) -> System.DateTime
Expand All @@ -113,18 +121,21 @@ static PInvoke.Kernel32.VerifyVersionInfo(System.IntPtr lpVersionInformation, PI
static PInvoke.Kernel32.VerifyVersionInfo(ref PInvoke.Kernel32.OSVERSIONINFOEX lpVersionInformation, PInvoke.Kernel32.VER_MASK dwTypeMask, long dwlConditionMask) -> PInvoke.NTSTATUS
static PInvoke.Kernel32.WriteProcessMemory(System.IntPtr hProcess, System.IntPtr lpBaseAddress, System.IntPtr lpBuffer, System.UIntPtr nSize, out System.UIntPtr lpNumberOfBytesWritten) -> bool
static extern PInvoke.Kernel32.CompareFileTime(PInvoke.Kernel32.FILETIME* lpFileTime1, PInvoke.Kernel32.FILETIME* lpFileTime2) -> int
static extern PInvoke.Kernel32.CreatePseudoConsole(PInvoke.COORD size, PInvoke.Kernel32.SafeObjectHandle hInput, PInvoke.Kernel32.SafeObjectHandle hOutput, PInvoke.Kernel32.CreatePseudoConsoleFlags dwFlags, out PInvoke.Kernel32.SafePseudoConsoleHandle phPC) -> PInvoke.HResult
static extern PInvoke.Kernel32.FileTimeToSystemTime(PInvoke.Kernel32.FILETIME* lpFileTime, PInvoke.Kernel32.SYSTEMTIME* lpSystemTime) -> bool
static extern PInvoke.Kernel32.GetHandleInformation(System.Runtime.InteropServices.SafeHandle hObject, PInvoke.Kernel32.HandleFlags* lpdwFlags) -> bool
static extern PInvoke.Kernel32.GetProcessId(System.IntPtr Process) -> int
static extern PInvoke.Kernel32.GetProcessTimes(PInvoke.Kernel32.SafeObjectHandle handle, out PInvoke.Kernel32.FILETIME creation, out PInvoke.Kernel32.FILETIME exit, out PInvoke.Kernel32.FILETIME kernel, out PInvoke.Kernel32.FILETIME user) -> bool
static extern PInvoke.Kernel32.GetStartupInfo(PInvoke.Kernel32.STARTUPINFO* lpStartupInfo) -> void
static extern PInvoke.Kernel32.GetVolumeInformation(string lpRootPathName, char* lpVolumeNameBuffer, int nVolumeNameSize, out uint lpVolumeSerialNumber, out int lpMaximumComponentLength, out PInvoke.Kernel32.FileSystemFlags lpFileSystemFlags, char* lpFileSystemNameBuffer, int nFileSystemNameSize) -> bool
static extern PInvoke.Kernel32.ReadProcessMemory(System.IntPtr hProcess, void* lpBaseAddress, void* lpBuffer, System.UIntPtr nSize, out System.UIntPtr lpNumberOfBytesRead) -> bool
static extern PInvoke.Kernel32.ResizePseudoConsole(PInvoke.Kernel32.SafePseudoConsoleHandle hPC, PInvoke.COORD size) -> PInvoke.HResult
static extern PInvoke.Kernel32.SetErrorMode(PInvoke.Kernel32.ErrorModes uMode) -> PInvoke.Kernel32.ErrorModes
static extern PInvoke.Kernel32.SetFilePointerEx(PInvoke.Kernel32.SafeObjectHandle hFile, long liDistanceToMove, out long lpNewFilePointer, System.IO.SeekOrigin dwMoveMethod) -> bool
static extern PInvoke.Kernel32.SetHandleInformation(System.Runtime.InteropServices.SafeHandle hObject, PInvoke.Kernel32.HandleFlags dwMask, PInvoke.Kernel32.HandleFlags dwFlags) -> bool
static extern PInvoke.Kernel32.SetLastError(uint dwErrCode) -> void
static extern PInvoke.Kernel32.SystemTimeToFileTime(PInvoke.Kernel32.SYSTEMTIME* lpSystemTime, PInvoke.Kernel32.FILETIME* lpFileTime) -> bool
static extern PInvoke.Kernel32.VerSetConditionMask(long ConditionMask, PInvoke.Kernel32.VER_MASK TypeMask, PInvoke.Kernel32.VER_CONDITION Condition) -> long
static extern PInvoke.Kernel32.VerifyVersionInfo(PInvoke.Kernel32.OSVERSIONINFOEX* lpVersionInformation, PInvoke.Kernel32.VER_MASK dwTypeMask, long dwlConditionMask) -> PInvoke.NTSTATUS
static extern PInvoke.Kernel32.WriteProcessMemory(System.IntPtr hProcess, void* lpBaseAddress, void* lpBuffer, System.UIntPtr nSize, out System.UIntPtr lpNumberOfBytesWritten) -> bool
static extern PInvoke.Kernel32.WriteProcessMemory(System.IntPtr hProcess, void* lpBaseAddress, void* lpBuffer, System.UIntPtr nSize, out System.UIntPtr lpNumberOfBytesWritten) -> bool
static readonly PInvoke.Kernel32.SafePseudoConsoleHandle.Invalid -> PInvoke.Kernel32.SafePseudoConsoleHandle
30 changes: 30 additions & 0 deletions src/Kernel32/storebanned/Kernel32+CreatePseudoConsoleFlags.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright © .NET Foundation and Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace PInvoke
{
using System;
using System.Diagnostics;

/// <content>
/// Contains the <see cref="CreatePseudoConsoleFlags"/> nested type.
/// </content>
public partial class Kernel32
{
/// <summary>
/// Flags for the <see cref="CreatePseudoConsole(COORD, SafeObjectHandle, SafeObjectHandle, CreatePseudoConsoleFlags, out SafePseudoConsoleHandle)"/> method.
/// </summary>
public enum CreatePseudoConsoleFlags
{
/// <summary>
/// Perform a standard pseudoconsole creation.
/// </summary>
None = 0,

/// <summary>
/// The created pseudoconsole session will attempt to inherit the cursor position of the parent console.
/// </summary>
PSEUDOCONSOLE_INHERIT_CURSOR = 1,
}
}
}
57 changes: 57 additions & 0 deletions src/Kernel32/storebanned/Kernel32+SafePseudoConsoleHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright © .NET Foundation and Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace PInvoke
{
using System;
using System.Runtime.InteropServices;

/// <content>
/// Contains the <see cref="SafePseudoConsoleHandle"/> nested type.
/// </content>
public static partial class Kernel32
{
/// <summary>
/// Represents a Win32 handle that can be closed with <see cref="ClosePseudoConsole"/>.
/// </summary>
public class SafePseudoConsoleHandle : SafeHandle
{
/// <summary>
/// An invalid handle that may be used in place of <see cref="INVALID_HANDLE_VALUE"/>.
/// </summary>
public static readonly SafePseudoConsoleHandle Invalid = new SafePseudoConsoleHandle();

/// <summary>
/// Initializes a new instance of the <see cref="SafePseudoConsoleHandle"/> class.
/// </summary>
public SafePseudoConsoleHandle()
: base(INVALID_HANDLE_VALUE, true)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="SafePseudoConsoleHandle"/> class.
/// </summary>
/// <param name="preexistingHandle">An object that represents the pre-existing handle to use.</param>
/// <param name="ownsHandle">
/// <see langword="true" /> to have the native handle released when this safe handle is disposed or finalized;
/// <see langword="false" /> otherwise.
/// </param>
public SafePseudoConsoleHandle(IntPtr preexistingHandle, bool ownsHandle = true)
: base(IntPtr.Zero, ownsHandle)
{
this.SetHandle(preexistingHandle);
}

/// <inheritdoc />
public override bool IsInvalid => this.handle == INVALID_HANDLE_VALUE;

/// <inheritdoc />
protected override bool ReleaseHandle()
{
ClosePseudoConsole(this.handle);
return true;
}
}
}
}
39 changes: 39 additions & 0 deletions src/Kernel32/storebanned/Kernel32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3026,5 +3026,44 @@ public static partial class Kernel32
[DllImport(api_ms_win_core_handle_l1_1_0, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern unsafe bool GetHandleInformation(SafeHandle hObject, [Friendly(FriendlyFlags.Out)] HandleFlags* lpdwFlags);

/// <summary>
/// Creates a new pseudoconsole object for the calling process.
/// </summary>
/// <param name="size">The dimensions of the window/buffer in count of characters that will be used on initial creation of the pseudoconsole. This can be adjusted later with <see cref="ResizePseudoConsole"/>.</param>
/// <param name="hInput">An open handle to a stream of data that represents user input to the device. This is currently restricted to <see href="https://docs.microsoft.com/en-us/windows/desktop/Sync/synchronization-and-overlapped-input-and-output">synchronous</see> I/O.</param>
/// <param name="hOutput">An open handle to a stream of data that represents application output from the device. This is currently restricted to <see href="https://docs.microsoft.com/en-us/windows/desktop/Sync/synchronization-and-overlapped-input-and-output">synchronous</see> I/O.</param>
/// <param name="dwFlags">The value can be one of the values of the enum.</param>
/// <param name="phPC">Pointer to a location that will receive a handle to the new pseudoconsole device.</param>
/// <returns>If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
[DllImport(nameof(Kernel32), CallingConvention = CallingConvention.Winapi)]
public static extern HResult CreatePseudoConsole(
COORD size,
SafeObjectHandle hInput,
SafeObjectHandle hOutput,
CreatePseudoConsoleFlags dwFlags,
out SafePseudoConsoleHandle phPC);

/// <summary>
/// Resizes the internal buffers for a pseudoconsole to the given size.
/// </summary>
/// <param name="hPC">A handle to an active psuedoconsole as opened by <see cref="CreatePseudoConsole"/>.</param>
/// <param name="size">The dimensions of the window/buffer in count of characters that will be used for the internal buffer of this pseudoconsole.</param>
/// <returns>If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.</returns>
/// <remarks>
/// This function can resize the internal buffers in the pseudoconsole session to match the window/buffer size being used for display on the terminal end.
/// This ensures that attached Command-Line Interface (CUI) applications using the Console Functions to communicate will have the correct dimensions returned in their calls.
/// </remarks>
[DllImport(nameof(Kernel32), CallingConvention = CallingConvention.Winapi)]
public static extern HResult ResizePseudoConsole(
SafePseudoConsoleHandle hPC,
COORD size);

/// <summary>
/// Closes a pseudoconsole from the given handle.
/// </summary>
/// <param name="hPC">A handle to an active psuedoconsole as opened by <see cref="CreatePseudoConsole"/>.</param>
[DllImport(nameof(Kernel32), CallingConvention = CallingConvention.Winapi)]
private static extern void ClosePseudoConsole(IntPtr hPC);
}
}

0 comments on commit 798afc4

Please sign in to comment.