File size: 4,715 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System;
using System.ComponentModel;
using System.Globalization;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Tracing;
using System.Runtime.InteropServices;
using System.Threading;
namespace Microsoft.PowerShell
{
/// <summary>
/// Defines an entry point from unmanaged code to PowerShell.
/// </summary>
public sealed class UnmanagedPSEntry
{
/// <summary>
/// Starts PowerShell.
/// </summary>
/// <param name="consoleFilePath">
/// Deprecated: Console file used to create a runspace configuration to start PowerShell
/// </param>
/// <param name="args">
/// Command line arguments to the PowerShell
/// </param>
/// <param name="argc">
/// Length of the passed in argument array.
/// </param>
[Obsolete("Callers should now use UnmanagedPSEntry.Start(string[], int)", error: true)]
public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 2)] string[] args, int argc)
{
return Start(args, argc);
}
/// <summary>
/// Starts PowerShell.
/// </summary>
/// <param name="args">
/// Command line arguments to PowerShell
/// </param>
/// <param name="argc">
/// Length of the passed in argument array.
/// </param>
public static int Start([MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 1)] string[] args, int argc)
{
ArgumentNullException.ThrowIfNull(args);
#if DEBUG
if (args.Length > 0 && !string.IsNullOrEmpty(args[0]) && args[0]!.Equals("-isswait", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Attach the debugger to continue...");
while (!System.Diagnostics.Debugger.IsAttached)
{
Thread.Sleep(100);
}
System.Diagnostics.Debugger.Break();
}
#endif
// Warm up some components concurrently on background threads.
EarlyStartup.Init();
// Windows Vista and later support non-traditional UI fallback ie., a
// user on an Arabic machine can choose either French or English(US) as
// UI fallback language.
// CLR does not support this (non-traditional) fallback mechanism.
// The currentUICulture returned NativeCultureResolver supports this non
// traditional fallback on Vista. So it is important to set currentUICulture
// in the beginning before we do anything.
Thread.CurrentThread.CurrentUICulture = NativeCultureResolver.UICulture;
Thread.CurrentThread.CurrentCulture = NativeCultureResolver.Culture;
ConsoleHost.ParseCommandLine(args);
// NOTE: On Unix, logging depends on a command line parsing
// and must be just after ConsoleHost.ParseCommandLine(args)
// to allow overriding logging options.
PSEtwLog.LogConsoleStartup();
int exitCode = 0;
try
{
string banner = string.Format(
CultureInfo.InvariantCulture,
ManagedEntranceStrings.ShellBannerPowerShell,
PSVersionInfo.GitCommitId);
ConsoleHost.DefaultInitialSessionState = InitialSessionState.CreateDefault2();
exitCode = ConsoleHost.Start(
bannerText: banner,
helpText: ManagedEntranceStrings.UsageHelp,
issProvidedExternally: false);
}
catch (HostException e)
{
if (e.InnerException is Win32Exception win32e)
{
// These exceptions are caused by killing conhost.exe
// 1236, network connection aborted by local system
// 0x6, invalid console handle
if (win32e.NativeErrorCode == 0x6 || win32e.NativeErrorCode == 1236)
{
return exitCode;
}
}
System.Environment.FailFast(e.Message, e);
}
catch (Exception e)
{
System.Environment.FailFast(e.Message, e);
}
return exitCode;
}
}
}
|