Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit dbc3557

Browse files
Merge pull request #10983 from danmosemsft/revert.jeremy
Revert "Strip out unused reg code. (#10741)"
2 parents dc5b99d + 368b350 commit dbc3557

File tree

11 files changed

+861
-139
lines changed

11 files changed

+861
-139
lines changed

src/mscorlib/System.Private.CoreLib.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@
8686
<PropertyGroup Condition="'$(OsEnvironment)' == 'Unix'">
8787
<DebugType>portable</DebugType>
8888
</PropertyGroup>
89+
8990
<PropertyGroup Condition="'$(TargetsOSX)' == 'true'">
9091
<DefineConstants>PLATFORM_OSX;$(DefineConstants)</DefineConstants>
9192
</PropertyGroup>
93+
9294
<!-- Assembly attributes -->
9395
<PropertyGroup>
9496
<AssemblyName>System.Private.CoreLib</AssemblyName>
@@ -625,6 +627,7 @@
625627
<Compile Condition="'$(FeatureWin32Registry)' == 'true'" Include="$(BclSourcesRoot)\Microsoft\Win32\Registry.cs" />
626628
<Compile Condition="'$(FeatureWin32Registry)' == 'true'" Include="$(BclSourcesRoot)\Microsoft\Win32\RegistryKey.cs" />
627629
<Compile Condition="'$(FeatureWin32Registry)' == 'true'" Include="$(BclSourcesRoot)\Microsoft\Win32\RegistryValueKind.cs" />
630+
<Compile Condition="'$(FeatureWin32Registry)' == 'true'" Include="$(BclSourcesRoot)\Microsoft\Win32\RegistryView.cs" />
628631
<Compile Condition="'$(FeatureClassicCominterop)' == 'true'" Include="$(BclSourcesRoot)\Microsoft\Win32\OAVariantLib.cs" />
629632
</ItemGroup>
630633
<ItemGroup>
@@ -757,4 +760,4 @@
757760
<Win32Resource Condition="'$(GenerateNativeVersionInfo)'=='true'">$(IntermediateOutputPath)\System.Private.CoreLib.res</Win32Resource>
758761
</PropertyGroup>
759762
<Import Project="GenerateCompilerResponseFile.targets" />
760-
</Project>
763+
</Project>

src/mscorlib/shared/System/Diagnostics/Tracing/EventProvider.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -555,21 +555,21 @@ private unsafe bool GetDataFromController(int etwSessionId,
555555
{
556556
#if (!ES_BUILD_PCL && !ES_BUILD_PN && !FEATURE_PAL)
557557
string regKey = @"\Microsoft\Windows\CurrentVersion\Winevt\Publishers\{" + m_providerId + "}";
558-
if (Marshal.SizeOf(typeof(IntPtr)) == 8)
559-
regKey = @"Software" + @"\Wow6432Node" + regKey;
558+
if (System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8)
559+
regKey = @"HKEY_LOCAL_MACHINE\Software" + @"\Wow6432Node" + regKey;
560560
else
561-
regKey = @"Software" + regKey;
561+
regKey = @"HKEY_LOCAL_MACHINE\Software" + regKey;
562562

563563
string valueName = "ControllerData_Session_" + etwSessionId.ToString(CultureInfo.InvariantCulture);
564564

565-
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regKey, writable: false))
566-
{
567-
data = key.GetValue(valueName) as byte[];
568-
}
569-
565+
// we need to assert this permission for partial trust scenarios
566+
#if !CORECLR
567+
(new RegistryPermission(RegistryPermissionAccess.Read, regKey)).Assert();
568+
#endif
569+
data = Microsoft.Win32.Registry.GetValue(regKey, valueName, null) as byte[];
570570
if (data != null)
571571
{
572-
// We only used the persisted data from the registry for updates.
572+
// We only used the persisted data from the registry for updates.
573573
command = ControllerCommand.Update;
574574
return true;
575575
}

src/mscorlib/src/Microsoft/Win32/Registry.cs

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,142 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
6+
using System;
7+
using System.Runtime.InteropServices;
8+
using System.Runtime.Versioning;
9+
510
namespace Microsoft.Win32
611
{
12+
/**
13+
* Registry encapsulation. Contains members representing all top level system
14+
* keys.
15+
*
16+
* @security(checkClassLinking=on)
17+
*/
18+
//This class contains only static members and does not need to be serializable.
719
internal static class Registry
820
{
9-
public static readonly RegistryKey CurrentUser = RegistryKey.CurrentUser;
10-
public static readonly RegistryKey LocalMachine = RegistryKey.LocalMachine;
21+
/**
22+
* Current User Key.
23+
*
24+
* This key should be used as the root for all user specific settings.
25+
*/
26+
public static readonly RegistryKey CurrentUser = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER);
27+
28+
/**
29+
* Local Machine Key.
30+
*
31+
* This key should be used as the root for all machine specific settings.
32+
*/
33+
public static readonly RegistryKey LocalMachine = RegistryKey.GetBaseKey(RegistryKey.HKEY_LOCAL_MACHINE);
34+
35+
/**
36+
* Classes Root Key.
37+
*
38+
* This is the root key of class information.
39+
*/
40+
public static readonly RegistryKey ClassesRoot = RegistryKey.GetBaseKey(RegistryKey.HKEY_CLASSES_ROOT);
41+
42+
/**
43+
* Users Root Key.
44+
*
45+
* This is the root of users.
46+
*/
47+
public static readonly RegistryKey Users = RegistryKey.GetBaseKey(RegistryKey.HKEY_USERS);
48+
49+
/**
50+
* Performance Root Key.
51+
*
52+
* This is where dynamic performance data is stored on NT.
53+
*/
54+
public static readonly RegistryKey PerformanceData = RegistryKey.GetBaseKey(RegistryKey.HKEY_PERFORMANCE_DATA);
55+
56+
/**
57+
* Current Config Root Key.
58+
*
59+
* This is where current configuration information is stored.
60+
*/
61+
public static readonly RegistryKey CurrentConfig = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_CONFIG);
62+
63+
//
64+
// Following function will parse a keyName and returns the basekey for it.
65+
// It will also store the subkey name in the out parameter.
66+
// If the keyName is not valid, we will throw ArgumentException.
67+
// The return value shouldn't be null.
68+
//
69+
private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subKeyName)
70+
{
71+
if (keyName == null)
72+
{
73+
throw new ArgumentNullException(nameof(keyName));
74+
}
75+
76+
string basekeyName;
77+
int i = keyName.IndexOf('\\');
78+
if (i != -1)
79+
{
80+
basekeyName = keyName.Substring(0, i).ToUpper(System.Globalization.CultureInfo.InvariantCulture);
81+
}
82+
else
83+
{
84+
basekeyName = keyName.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
85+
}
86+
RegistryKey basekey = null;
87+
88+
switch (basekeyName)
89+
{
90+
case "HKEY_CURRENT_USER":
91+
basekey = Registry.CurrentUser;
92+
break;
93+
case "HKEY_LOCAL_MACHINE":
94+
basekey = Registry.LocalMachine;
95+
break;
96+
case "HKEY_CLASSES_ROOT":
97+
basekey = Registry.ClassesRoot;
98+
break;
99+
case "HKEY_USERS":
100+
basekey = Registry.Users;
101+
break;
102+
case "HKEY_PERFORMANCE_DATA":
103+
basekey = Registry.PerformanceData;
104+
break;
105+
case "HKEY_CURRENT_CONFIG":
106+
basekey = Registry.CurrentConfig;
107+
break;
108+
default:
109+
throw new ArgumentException(SR.Format(SR.Arg_RegInvalidKeyName, nameof(keyName)));
110+
}
111+
if (i == -1 || i == keyName.Length)
112+
{
113+
subKeyName = string.Empty;
114+
}
115+
else
116+
{
117+
subKeyName = keyName.Substring(i + 1, keyName.Length - i - 1);
118+
}
119+
return basekey;
120+
}
121+
122+
public static object GetValue(string keyName, string valueName, object defaultValue)
123+
{
124+
string subKeyName;
125+
RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
126+
BCLDebug.Assert(basekey != null, "basekey can't be null.");
127+
RegistryKey key = basekey.OpenSubKey(subKeyName);
128+
if (key == null)
129+
{ // if the key doesn't exist, do nothing
130+
return null;
131+
}
132+
try
133+
{
134+
return key.GetValue(valueName, defaultValue);
135+
}
136+
finally
137+
{
138+
key.Close();
139+
}
140+
}
11141
}
12142
}
13143

0 commit comments

Comments
 (0)