Replace DllImport with LibraryImport - 1 (#18603)

This commit is contained in:
Ilya
2022-11-30 10:00:12 -08:00
committed by GitHub
parent 52ea419662
commit 3dc95ced87
27 changed files with 413 additions and 333 deletions
@@ -2078,66 +2078,13 @@ namespace System.Management.Automation
/// Static class that allows you to show and hide the console window
/// associated with this process.
/// </summary>
internal static partial class ConsoleVisibility
internal static class ConsoleVisibility
{
/// <summary>
/// If set to true, then native commands will always be run redirected...
/// </summary>
public static bool AlwaysCaptureApplicationIO { get; set; }
[LibraryImport("Kernel32.dll")]
internal static partial IntPtr GetConsoleWindow();
internal const int SW_HIDE = 0;
internal const int SW_SHOWNORMAL = 1;
internal const int SW_NORMAL = 1;
internal const int SW_SHOWMINIMIZED = 2;
internal const int SW_SHOWMAXIMIZED = 3;
internal const int SW_MAXIMIZE = 3;
internal const int SW_SHOWNOACTIVATE = 4;
internal const int SW_SHOW = 5;
internal const int SW_MINIMIZE = 6;
internal const int SW_SHOWMINNOACTIVE = 7;
internal const int SW_SHOWNA = 8;
internal const int SW_RESTORE = 9;
internal const int SW_SHOWDEFAULT = 10;
internal const int SW_FORCEMINIMIZE = 11;
internal const int SW_MAX = 11;
/// <summary>
/// Code to control the display properties of the a window...
/// </summary>
/// <param name="hWnd">The window to show...</param>
/// <param name="nCmdShow">The command to do.</param>
/// <returns>True if it was successful.</returns>
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool ShowWindow(IntPtr hWnd, int nCmdShow);
/// <summary>
/// Code to allocate a console...
/// </summary>
/// <returns>True if a console was created...</returns>
[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool AllocConsole();
/// <summary>
/// Called to save the foreground window before allocating a hidden console window.
/// </summary>
/// <returns>A handle to the foreground window.</returns>
[LibraryImport("user32.dll")]
private static partial IntPtr GetForegroundWindow();
/// <summary>
/// Called to restore the foreground window after allocating a hidden console window.
/// </summary>
/// <param name="hWnd">A handle to the window that should be activated and brought to the foreground.</param>
/// <returns>True if the window was brought to the foreground.</returns>
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// If no console window is attached to this process, then allocate one,
/// hide it and return true. If there's already a console window attached, then
@@ -2146,77 +2093,44 @@ namespace System.Management.Automation
/// <returns></returns>
internal static bool AllocateHiddenConsole()
{
#if UNIX
return false;
#else
// See if there is already a console attached.
IntPtr hwnd = ConsoleVisibility.GetConsoleWindow();
if (hwnd != IntPtr.Zero)
IntPtr hwnd = Interop.Windows.GetConsoleWindow();
if (hwnd != nint.Zero)
{
return false;
}
// save the foreground window since allocating a console window might remove focus from it
IntPtr savedForeground = ConsoleVisibility.GetForegroundWindow();
IntPtr savedForeground = Interop.Windows.GetForegroundWindow();
// Since there is no console window, allocate and then hide it...
// Suppress the PreFAST warning about not using Marshal.GetLastWin32Error() to
// get the error code.
#pragma warning disable 56523
ConsoleVisibility.AllocConsole();
hwnd = ConsoleVisibility.GetConsoleWindow();
Interop.Windows.AllocConsole();
hwnd = Interop.Windows.GetConsoleWindow();
bool returnValue;
if (hwnd == IntPtr.Zero)
if (hwnd == nint.Zero)
{
returnValue = false;
}
else
{
returnValue = true;
ConsoleVisibility.ShowWindow(hwnd, ConsoleVisibility.SW_HIDE);
Interop.Windows.ShowWindow(hwnd, Interop.Windows.SW_HIDE);
AlwaysCaptureApplicationIO = true;
}
if (savedForeground != IntPtr.Zero && ConsoleVisibility.GetForegroundWindow() != savedForeground)
if (savedForeground != nint.Zero && Interop.Windows.GetForegroundWindow() != savedForeground)
{
ConsoleVisibility.SetForegroundWindow(savedForeground);
Interop.Windows.SetForegroundWindow(savedForeground);
}
return returnValue;
}
/// <summary>
/// If there is a console attached, then make it visible
/// and allow interactive console applications to be run.
/// </summary>
public static void Show()
{
IntPtr hwnd = GetConsoleWindow();
if (hwnd != IntPtr.Zero)
{
ShowWindow(hwnd, SW_SHOW);
AlwaysCaptureApplicationIO = false;
}
else
{
throw PSTraceSource.NewInvalidOperationException();
}
}
/// <summary>
/// If there is a console attached, then hide it and always capture
/// output from the child process.
/// </summary>
public static void Hide()
{
IntPtr hwnd = GetConsoleWindow();
if (hwnd != IntPtr.Zero)
{
ShowWindow(hwnd, SW_HIDE);
AlwaysCaptureApplicationIO = true;
}
else
{
throw PSTraceSource.NewInvalidOperationException();
}
#endif
}
}