Avoid calling static members from 'Microsoft.Win32.Registry' on UNIX, especially the code that would run in TypeInitializer.

This is because with the latest .NET Core packages (1.1.0 preview), calling static members from 'Microsoft.Win32.Registry' will raise 'PlatformNotSupportedException'.
This commit is contained in:
Dongbo Wang 2016-10-12 16:55:41 -07:00 committed by Mike Richmond
parent a1aac43f1b
commit d0c60e049d
2 changed files with 46 additions and 37 deletions

View file

@ -200,8 +200,12 @@ namespace System.Management.Automation
return ecFromTLS.TypeTable;
}
#if !UNIX
private static string s_pshome = null;
/// <summary>
/// Get the application base path of the shell from registry
/// </summary>
internal static string GetApplicationBaseFromRegistry(string shellId)
{
bool wantPsHome = (object)shellId == (object)DefaultPowerShellShellID;
@ -226,6 +230,7 @@ namespace System.Management.Automation
return null;
}
#endif
/// <summary>
/// Gets the application base for current monad version
@ -616,13 +621,18 @@ namespace System.Management.Automation
return GetRegistryConfigurationPrefix() + "\\" + shellID;
}
// Retrieves group policy settings based on the preference order provided:
// Dictionary<string, object> settings = GetGroupPolicySetting("Transcription", Registry.LocalMachine, Registry.CurrentUser);
// Calling static members of 'Registry' on UNIX will raise 'PlatformNotSupportedException'
#if UNIX
internal static RegistryKey[] RegLocalMachine = null;
internal static RegistryKey[] RegCurrentUser = null;
internal static RegistryKey[] RegLocalMachineThenCurrentUser = null;
internal static RegistryKey[] RegCurrentUserThenLocalMachine = null;
#else
internal static RegistryKey[] RegLocalMachine = new[] { Registry.LocalMachine };
internal static RegistryKey[] RegCurrentUser = new[] { Registry.CurrentUser };
internal static RegistryKey[] RegLocalMachineThenCurrentUser = new[] { Registry.LocalMachine, Registry.CurrentUser };
internal static RegistryKey[] RegCurrentUserThenLocalMachine = new[] { Registry.CurrentUser, Registry.LocalMachine };
#endif
internal static Dictionary<string, object> GetGroupPolicySetting(string settingName, RegistryKey[] preferenceOrder)
{

View file

@ -247,6 +247,7 @@ namespace System.Management.Automation.Runspaces
}
}
#if !CORECLR
/// <summary>
/// Stack Reserve setting for pipeline threads
/// </summary>
@ -263,6 +264,38 @@ namespace System.Management.Automation.Runspaces
}
}
internal static int ReadRegistryInt(string policyValueName, int defaultValue)
{
RegistryKey key;
try
{
key = Registry.LocalMachine.OpenSubKey(Utils.GetRegistryConfigurationPrefix());
}
catch (System.Security.SecurityException)
{
return defaultValue;
}
if (null == key)
return defaultValue;
object temp;
try
{
temp = key.GetValue(policyValueName);
}
catch (System.Security.SecurityException)
{
return defaultValue;
}
if (!(temp is int))
{
return defaultValue;
}
int i = (int)temp;
return i;
}
#endif
///<summary>
/// Helper method for asynchronous invoke
///<returns>Unhandled FlowControl exception if InvocationSettings.ExposeFlowControlExceptions is true.</returns>
@ -531,40 +564,6 @@ namespace System.Management.Automation.Runspaces
return flowControlException;
}
// NTRAID#Windows Out Of Band Releases-915506-2005/09/09
// Removed HandleUnexpectedExceptions infrastructure
internal static int ReadRegistryInt(string policyValueName, int defaultValue)
{
RegistryKey key;
try
{
key = Registry.LocalMachine.OpenSubKey(Utils.GetRegistryConfigurationPrefix());
}
catch (System.Security.SecurityException)
{
return defaultValue;
}
if (null == key)
return defaultValue;
object temp;
try
{
temp = key.GetValue(policyValueName);
}
catch (System.Security.SecurityException)
{
return defaultValue;
}
if (!(temp is int))
{
return defaultValue;
}
int i = (int)temp;
return i;
}
// NTRAID#Windows Out Of Band Releases-915506-2005/09/09
// Removed HandleUnexpectedExceptions infrastructure
/// <summary>