Skip to content

Commit

Permalink
Merge pull request #2351 from manne/feature/nullable_microsoft_win32_…
Browse files Browse the repository at this point in the history
…registry

Nullable annotate for Microsoft.Win32.Registry
  • Loading branch information
eiriktsarpalis authored Feb 18, 2020
2 parents a01fd73 + 72c9708 commit 446c224
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public static partial class Registry
public static readonly Microsoft.Win32.RegistryKey LocalMachine;
public static readonly Microsoft.Win32.RegistryKey PerformanceData;
public static readonly Microsoft.Win32.RegistryKey Users;
public static object GetValue(string keyName, string valueName, object defaultValue) { throw null; }
public static void SetValue(string keyName, string valueName, object value) { }
public static void SetValue(string keyName, string valueName, object value, Microsoft.Win32.RegistryValueKind valueKind) { }
public static object? GetValue(string keyName, string? valueName, object? defaultValue) { throw null; }
public static void SetValue(string keyName, string? valueName, object value) { }
public static void SetValue(string keyName, string? valueName, object value, Microsoft.Win32.RegistryValueKind valueKind) { }
}
public enum RegistryHive
{
Expand All @@ -40,8 +40,8 @@ public void Close() { }
public Microsoft.Win32.RegistryKey CreateSubKey(string subkey) { throw null; }
public Microsoft.Win32.RegistryKey CreateSubKey(string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck) { throw null; }
public Microsoft.Win32.RegistryKey CreateSubKey(string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, Microsoft.Win32.RegistryOptions registryOptions) { throw null; }
public Microsoft.Win32.RegistryKey CreateSubKey(string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, Microsoft.Win32.RegistryOptions registryOptions, System.Security.AccessControl.RegistrySecurity registrySecurity) { throw null; }
public Microsoft.Win32.RegistryKey CreateSubKey(string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistrySecurity registrySecurity) { throw null; }
public Microsoft.Win32.RegistryKey CreateSubKey(string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, Microsoft.Win32.RegistryOptions registryOptions, System.Security.AccessControl.RegistrySecurity? registrySecurity) { throw null; }
public Microsoft.Win32.RegistryKey CreateSubKey(string subkey, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistrySecurity? registrySecurity) { throw null; }
public Microsoft.Win32.RegistryKey CreateSubKey(string subkey, bool writable) { throw null; }
public Microsoft.Win32.RegistryKey CreateSubKey(string subkey, bool writable, Microsoft.Win32.RegistryOptions options) { throw null; }
public void DeleteSubKey(string subkey) { }
Expand All @@ -57,22 +57,22 @@ public void Flush() { }
public System.Security.AccessControl.RegistrySecurity GetAccessControl() { throw null; }
public System.Security.AccessControl.RegistrySecurity GetAccessControl(System.Security.AccessControl.AccessControlSections includeSections) { throw null; }
public string[] GetSubKeyNames() { throw null; }
public object GetValue(string name) { throw null; }
public object GetValue(string name, object defaultValue) { throw null; }
public object GetValue(string name, object defaultValue, Microsoft.Win32.RegistryValueOptions options) { throw null; }
public Microsoft.Win32.RegistryValueKind GetValueKind(string name) { throw null; }
public object? GetValue(string? name) { throw null; }
public object? GetValue(string? name, object? defaultValue) { throw null; }
public object? GetValue(string? name, object? defaultValue, Microsoft.Win32.RegistryValueOptions options) { throw null; }
public Microsoft.Win32.RegistryValueKind GetValueKind(string? name) { throw null; }
public string[] GetValueNames() { throw null; }
public static Microsoft.Win32.RegistryKey OpenBaseKey(Microsoft.Win32.RegistryHive hKey, Microsoft.Win32.RegistryView view) { throw null; }
public static Microsoft.Win32.RegistryKey OpenRemoteBaseKey(Microsoft.Win32.RegistryHive hKey, string machineName) { throw null; }
public static Microsoft.Win32.RegistryKey OpenRemoteBaseKey(Microsoft.Win32.RegistryHive hKey, string machineName, Microsoft.Win32.RegistryView view) { throw null; }
public Microsoft.Win32.RegistryKey OpenSubKey(string name) { throw null; }
public Microsoft.Win32.RegistryKey OpenSubKey(string name, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck) { throw null; }
public Microsoft.Win32.RegistryKey OpenSubKey(string name, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistryRights rights) { throw null; }
public Microsoft.Win32.RegistryKey OpenSubKey(string name, bool writable) { throw null; }
public Microsoft.Win32.RegistryKey OpenSubKey(string name, System.Security.AccessControl.RegistryRights rights) { throw null; }
public Microsoft.Win32.RegistryKey? OpenSubKey(string name) { throw null; }
public Microsoft.Win32.RegistryKey? OpenSubKey(string name, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck) { throw null; }
public Microsoft.Win32.RegistryKey? OpenSubKey(string name, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistryRights rights) { throw null; }
public Microsoft.Win32.RegistryKey? OpenSubKey(string name, bool writable) { throw null; }
public Microsoft.Win32.RegistryKey? OpenSubKey(string name, System.Security.AccessControl.RegistryRights rights) { throw null; }
public void SetAccessControl(System.Security.AccessControl.RegistrySecurity registrySecurity) { }
public void SetValue(string name, object value) { }
public void SetValue(string name, object value, Microsoft.Win32.RegistryValueKind valueKind) { }
public void SetValue(string? name, object value) { }
public void SetValue(string? name, object value, Microsoft.Win32.RegistryValueKind valueKind) { }
public override string ToString() { throw null; }
}
public enum RegistryKeyPermissionCheck
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<IsPartialFacadeAssembly Condition="'$(TargetsNetFx)' == 'true'">true</IsPartialFacadeAssembly>
<TargetFrameworks>netstandard2.0;$(NetFrameworkCurrent);net461</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="Microsoft.Win32.Registry.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<TargetFrameworks>$(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix;$(NetFrameworkCurrent)-Windows_NT;netstandard2.0-Windows_NT;netstandard2.0-Unix;netstandard2.0;net461-Windows_NT</TargetFrameworks>
<ExcludeCurrentNetCoreAppFromPackage>true</ExcludeCurrentNetCoreAppFromPackage>
<ExcludeCurrentFullFrameworkFromPackage>true</ExcludeCurrentFullFrameworkFromPackage>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup Condition="'$(TargetsNetFx)' != 'true' and '$(TargetsAnyOS)' != 'true'">
<Compile Include="$(CommonPath)Interop\Windows\Advapi32\Interop.RegistryConstants.cs">
Expand Down Expand Up @@ -107,4 +108,4 @@
<Reference Include="System.Security.AccessControl" />
<Reference Include="System.Security.Principal.Windows" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subK
int length = i != -1 ? i : keyName.Length;

// Determine the potential base key from the length.
RegistryKey baseKey = null;
RegistryKey? baseKey = null;
switch (length)
{
case 10: baseKey = Users; break; // HKEY_USERS
Expand All @@ -68,28 +68,26 @@ private static RegistryKey GetBaseKeyFromKeyName(string keyName, out string subK
throw new ArgumentException(SR.Format(SR.Arg_RegInvalidKeyName, nameof(keyName)), nameof(keyName));
}

public static object GetValue(string keyName, string valueName, object defaultValue)
public static object? GetValue(string keyName, string? valueName, object? defaultValue)
{
string subKeyName;
RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out string subKeyName);

using (RegistryKey key = basekey.OpenSubKey(subKeyName))
using (RegistryKey? key = basekey.OpenSubKey(subKeyName))
{
return key?.GetValue(valueName, defaultValue);
}
}

public static void SetValue(string keyName, string valueName, object value)
public static void SetValue(string keyName, string? valueName, object value)
{
SetValue(keyName, valueName, value, RegistryValueKind.Unknown);
}

public static void SetValue(string keyName, string valueName, object value, RegistryValueKind valueKind)
public static void SetValue(string keyName, string? valueName, object value, RegistryValueKind valueKind)
{
string subKeyName;
RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out subKeyName);
RegistryKey basekey = GetBaseKeyFromKeyName(keyName, out string subKeyName);

using (RegistryKey key = basekey.CreateSubKey(subKeyName))
using (RegistryKey? key = basekey.CreateSubKey(subKeyName))
{
Debug.Assert(key != null, "An exception should be thrown if failed!");
key.SetValue(valueName, value, valueKind);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ private string[] GetValueNamesCore(int values)
throw new PlatformNotSupportedException(SR.PlatformNotSupported_Registry);
}

private object InternalGetValueCore(string name, object defaultValue, bool doNotExpand)
private object InternalGetValueCore(string? name, object? defaultValue, bool doNotExpand)
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_Registry);
}

private RegistryValueKind GetValueKindCore(string name)
private RegistryValueKind GetValueKindCore(string? name)
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_Registry);
}

private void SetValueCore(string name, object value, RegistryValueKind valueKind)
private void SetValueCore(string? name, object value, RegistryValueKind valueKind)
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_Registry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Diagnostics;
using System.IO;
using System.Security;
using System.Security.AccessControl;

/*
Note on transaction support:
Expand Down Expand Up @@ -85,20 +84,18 @@ private void FlushCore()

private unsafe RegistryKey CreateSubKeyInternalCore(string subkey, RegistryKeyPermissionCheck permissionCheck, RegistryOptions registryOptions)
{
Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = default(Interop.Kernel32.SECURITY_ATTRIBUTES);
int disposition = 0;
Interop.Kernel32.SECURITY_ATTRIBUTES secAttrs = default;

// By default, the new key will be writable.
SafeRegistryHandle result = null;
int ret = Interop.Advapi32.RegCreateKeyEx(_hkey,
subkey,
0,
null,
(int)registryOptions /* specifies if the key is volatile */,
GetRegistryKeyAccess(permissionCheck != RegistryKeyPermissionCheck.ReadSubTree) | (int)_regView,
ref secAttrs,
out result,
out disposition);
out SafeRegistryHandle result,
out int _);

if (ret == 0 && !result.IsInvalid)
{
Expand Down Expand Up @@ -220,8 +217,7 @@ private static RegistryKey OpenRemoteBaseKeyCore(RegistryHive hKey, string machi
}

// connect to the specified remote registry
SafeRegistryHandle foreignHKey = null;
int ret = Interop.Advapi32.RegConnectRegistry(machineName, new SafeRegistryHandle(new IntPtr((int)hKey), false), out foreignHKey);
int ret = Interop.Advapi32.RegConnectRegistry(machineName, new SafeRegistryHandle(new IntPtr((int)hKey), false), out SafeRegistryHandle foreignHKey);

if (ret == Interop.Errors.ERROR_DLL_INIT_FAILED)
{
Expand All @@ -246,10 +242,9 @@ private static RegistryKey OpenRemoteBaseKeyCore(RegistryHive hKey, string machi
return key;
}

private RegistryKey InternalOpenSubKeyCore(string name, RegistryKeyPermissionCheck permissionCheck, int rights)
private RegistryKey? InternalOpenSubKeyCore(string name, RegistryKeyPermissionCheck permissionCheck, int rights)
{
SafeRegistryHandle result = null;
int ret = Interop.Advapi32.RegOpenKeyEx(_hkey, name, 0, (rights | (int)_regView), out result);
int ret = Interop.Advapi32.RegOpenKeyEx(_hkey, name, 0, (rights | (int)_regView), out SafeRegistryHandle result);
if (ret == 0 && !result.IsInvalid)
{
RegistryKey key = new RegistryKey(result, (permissionCheck == RegistryKeyPermissionCheck.ReadWriteSubTree), false, _remoteKey, false, _regView);
Expand All @@ -269,10 +264,9 @@ private RegistryKey InternalOpenSubKeyCore(string name, RegistryKeyPermissionChe
return null;
}

private RegistryKey InternalOpenSubKeyCore(string name, bool writable)
private RegistryKey? InternalOpenSubKeyCore(string name, bool writable)
{
SafeRegistryHandle result = null;
int ret = Interop.Advapi32.RegOpenKeyEx(_hkey, name, 0, (GetRegistryKeyAccess(writable) | (int)_regView), out result);
int ret = Interop.Advapi32.RegOpenKeyEx(_hkey, name, 0, (GetRegistryKeyAccess(writable) | (int)_regView), out SafeRegistryHandle result);
if (ret == 0 && !result.IsInvalid)
{
RegistryKey key = new RegistryKey(result, writable, false, _remoteKey, false, _regView);
Expand All @@ -292,10 +286,9 @@ private RegistryKey InternalOpenSubKeyCore(string name, bool writable)
return null;
}

internal RegistryKey InternalOpenSubKeyWithoutSecurityChecksCore(string name, bool writable)
internal RegistryKey? InternalOpenSubKeyWithoutSecurityChecksCore(string name, bool writable)
{
SafeRegistryHandle result = null;
int ret = Interop.Advapi32.RegOpenKeyEx(_hkey, name, 0, (GetRegistryKeyAccess(writable) | (int)_regView), out result);
int ret = Interop.Advapi32.RegOpenKeyEx(_hkey, name, 0, (GetRegistryKeyAccess(writable) | (int)_regView), out SafeRegistryHandle result);
if (ret == 0 && !result.IsInvalid)
{
RegistryKey key = new RegistryKey(result, writable, false, _remoteKey, false, _regView);
Expand Down Expand Up @@ -340,12 +333,11 @@ private SafeRegistryHandle SystemKeyHandle
}

// open the base key so that RegistryKey.Handle will return a valid handle
SafeRegistryHandle result;
ret = Interop.Advapi32.RegOpenKeyEx(baseKey,
null,
0,
GetRegistryKeyAccess(IsWritable()) | (int)_regView,
out result);
out SafeRegistryHandle result);

if (ret == 0 && !result.IsInvalid)
{
Expand Down Expand Up @@ -463,7 +455,7 @@ private unsafe string[] GetValueNamesCore(int values)
// add up quickly- we'll try to keep the memory pressure low and grow the buffer
// only if needed.

char[] name = ArrayPool<char>.Shared.Rent(100);
char[]? name = ArrayPool<char>.Shared.Rent(100);

try
{
Expand Down Expand Up @@ -528,13 +520,13 @@ private unsafe string[] GetValueNamesCore(int values)
return names.ToArray();
}

private object InternalGetValueCore(string name, object defaultValue, bool doNotExpand)
private object? InternalGetValueCore(string? name, object? defaultValue, bool doNotExpand)
{
object data = defaultValue;
object? data = defaultValue;
int type = 0;
int datasize = 0;

int ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, (byte[])null, ref datasize);
int ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, (byte[]?)null, ref datasize);

if (ret != 0)
{
Expand Down Expand Up @@ -734,7 +726,7 @@ private object InternalGetValueCore(string name, object defaultValue, bool doNot
nextNull++;
}

string toAdd = null;
string? toAdd = null;
if (nextNull < len)
{
Debug.Assert(blob[nextNull] == (char)0, "blob[nextNull] should be 0");
Expand Down Expand Up @@ -780,11 +772,11 @@ private object InternalGetValueCore(string name, object defaultValue, bool doNot
return data;
}

private RegistryValueKind GetValueKindCore(string name)
private RegistryValueKind GetValueKindCore(string? name)
{
int type = 0;
int datasize = 0;
int ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, (byte[])null, ref datasize);
int ret = Interop.Advapi32.RegQueryValueEx(_hkey, name, null, ref type, (byte[]?)null, ref datasize);
if (ret != 0)
{
Win32Error(ret, null);
Expand All @@ -796,7 +788,7 @@ private RegistryValueKind GetValueKindCore(string name)
(RegistryValueKind)type;
}

private unsafe void SetValueCore(string name, object value, RegistryValueKind valueKind)
private unsafe void SetValueCore(string? name, object value, RegistryValueKind valueKind)
{
int ret = 0;
try
Expand All @@ -806,7 +798,7 @@ private unsafe void SetValueCore(string name, object value, RegistryValueKind va
case RegistryValueKind.ExpandString:
case RegistryValueKind.String:
{
string data = value.ToString();
string data = value.ToString()!;
ret = Interop.Advapi32.RegSetValueEx(_hkey,
name,
0,
Expand Down Expand Up @@ -921,7 +913,7 @@ private unsafe void SetValueCore(string name, object value, RegistryValueKind va
/// error, and depending on the error, insert a string into the message
/// gotten from the ResourceManager.
/// </summary>
private void Win32Error(int errorCode, string str)
private void Win32Error(int errorCode, string? str)
{
switch (errorCode)
{
Expand All @@ -944,7 +936,7 @@ private void Win32Error(int errorCode, string str)
if (!IsPerfDataKey())
{
_hkey.SetHandleAsInvalid();
_hkey = null;
_hkey = null!;
}
goto default;

Expand All @@ -956,7 +948,7 @@ private void Win32Error(int errorCode, string str)
}
}

private static void Win32ErrorStatic(int errorCode, string str) =>
private static void Win32ErrorStatic(int errorCode, string? str) =>
throw errorCode switch
{
Interop.Errors.ERROR_ACCESS_DENIED => str != null ?
Expand Down
Loading

0 comments on commit 446c224

Please sign in to comment.