Files
Dec2017-Script-Dump/mscorlib/Microsoft/Win32/Win32RegistryApi.cs
T
2026-06-04 11:42:34 +02:00

540 lines
17 KiB
C#

using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
namespace Microsoft.Win32
{
// Token: 0x0200007C RID: 124
internal class Win32RegistryApi : IRegistryApi
{
// Token: 0x06000765 RID: 1893
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegCreateKey(IntPtr keyBase, string keyName, out IntPtr keyHandle);
// Token: 0x06000766 RID: 1894
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegCloseKey(IntPtr keyHandle);
// Token: 0x06000767 RID: 1895
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegConnectRegistry(string machineName, IntPtr hKey, out IntPtr keyHandle);
// Token: 0x06000768 RID: 1896
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegFlushKey(IntPtr keyHandle);
// Token: 0x06000769 RID: 1897
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegOpenKeyEx(IntPtr keyBase, string keyName, IntPtr reserved, int access, out IntPtr keyHandle);
// Token: 0x0600076A RID: 1898
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegDeleteKey(IntPtr keyHandle, string valueName);
// Token: 0x0600076B RID: 1899
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegDeleteValue(IntPtr keyHandle, string valueName);
// Token: 0x0600076C RID: 1900
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegEnumKey(IntPtr keyBase, int index, StringBuilder nameBuffer, int bufferLength);
// Token: 0x0600076D RID: 1901
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegEnumValue(IntPtr keyBase, int index, StringBuilder nameBuffer, ref int nameLength, IntPtr reserved, ref RegistryValueKind type, IntPtr data, IntPtr dataLength);
// Token: 0x0600076E RID: 1902
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegSetValueEx(IntPtr keyBase, string valueName, IntPtr reserved, RegistryValueKind type, string data, int rawDataLength);
// Token: 0x0600076F RID: 1903
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegSetValueEx(IntPtr keyBase, string valueName, IntPtr reserved, RegistryValueKind type, byte[] rawData, int rawDataLength);
// Token: 0x06000770 RID: 1904
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegSetValueEx(IntPtr keyBase, string valueName, IntPtr reserved, RegistryValueKind type, ref int data, int rawDataLength);
// Token: 0x06000771 RID: 1905
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegQueryValueEx(IntPtr keyBase, string valueName, IntPtr reserved, ref RegistryValueKind type, IntPtr zero, ref int dataSize);
// Token: 0x06000772 RID: 1906
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegQueryValueEx(IntPtr keyBase, string valueName, IntPtr reserved, ref RegistryValueKind type, [Out] byte[] data, ref int dataSize);
// Token: 0x06000773 RID: 1907
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
private static extern int RegQueryValueEx(IntPtr keyBase, string valueName, IntPtr reserved, ref RegistryValueKind type, ref int data, ref int dataSize);
// Token: 0x06000774 RID: 1908 RVA: 0x00017410 File Offset: 0x00015610
private static IntPtr GetHandle(RegistryKey key)
{
return (IntPtr)key.Handle;
}
// Token: 0x06000775 RID: 1909 RVA: 0x00017420 File Offset: 0x00015620
private static bool IsHandleValid(RegistryKey key)
{
return key.Handle != null;
}
// Token: 0x06000776 RID: 1910 RVA: 0x00017430 File Offset: 0x00015630
public object GetValue(RegistryKey rkey, string name, object defaultValue, RegistryValueOptions options)
{
RegistryValueKind registryValueKind = RegistryValueKind.Unknown;
int num = 0;
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
int num2 = Win32RegistryApi.RegQueryValueEx(handle, name, IntPtr.Zero, ref registryValueKind, IntPtr.Zero, ref num);
if (num2 == 2 || num2 == 1018)
{
return defaultValue;
}
if (num2 != 234 && num2 != 0)
{
this.GenerateException(num2);
}
object obj;
if (registryValueKind == RegistryValueKind.String)
{
byte[] array;
num2 = this.GetBinaryValue(rkey, name, registryValueKind, out array, num);
obj = RegistryKey.DecodeString(array);
}
else if (registryValueKind == RegistryValueKind.ExpandString)
{
byte[] array2;
num2 = this.GetBinaryValue(rkey, name, registryValueKind, out array2, num);
obj = RegistryKey.DecodeString(array2);
if ((options & RegistryValueOptions.DoNotExpandEnvironmentNames) == RegistryValueOptions.None)
{
obj = Environment.ExpandEnvironmentVariables((string)obj);
}
}
else if (registryValueKind == RegistryValueKind.DWord)
{
int num3 = 0;
num2 = Win32RegistryApi.RegQueryValueEx(handle, name, IntPtr.Zero, ref registryValueKind, ref num3, ref num);
obj = num3;
}
else if (registryValueKind == RegistryValueKind.Binary)
{
byte[] array3;
num2 = this.GetBinaryValue(rkey, name, registryValueKind, out array3, num);
obj = array3;
}
else
{
if (registryValueKind != RegistryValueKind.MultiString)
{
throw new SystemException();
}
obj = null;
byte[] array4;
num2 = this.GetBinaryValue(rkey, name, registryValueKind, out array4, num);
if (num2 == 0)
{
obj = RegistryKey.DecodeString(array4).Split(new char[1]);
}
}
if (num2 != 0)
{
this.GenerateException(num2);
}
return obj;
}
// Token: 0x06000777 RID: 1911 RVA: 0x00017580 File Offset: 0x00015780
public void SetValue(RegistryKey rkey, string name, object value, RegistryValueKind valueKind)
{
Type type = value.GetType();
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
int num2;
if (valueKind == RegistryValueKind.DWord && type == typeof(int))
{
int num = (int)value;
num2 = Win32RegistryApi.RegSetValueEx(handle, name, IntPtr.Zero, RegistryValueKind.DWord, ref num, 4);
}
else if (valueKind == RegistryValueKind.Binary && type == typeof(byte[]))
{
byte[] array = (byte[])value;
num2 = Win32RegistryApi.RegSetValueEx(handle, name, IntPtr.Zero, RegistryValueKind.Binary, array, array.Length);
}
else if (valueKind == RegistryValueKind.MultiString && type == typeof(string[]))
{
string[] array2 = (string[])value;
StringBuilder stringBuilder = new StringBuilder();
foreach (string text in array2)
{
stringBuilder.Append(text);
stringBuilder.Append('\0');
}
stringBuilder.Append('\0');
byte[] bytes = Encoding.Unicode.GetBytes(stringBuilder.ToString());
num2 = Win32RegistryApi.RegSetValueEx(handle, name, IntPtr.Zero, RegistryValueKind.MultiString, bytes, bytes.Length);
}
else if ((valueKind == RegistryValueKind.String || valueKind == RegistryValueKind.ExpandString) && type == typeof(string))
{
string text2 = string.Format("{0}{1}", value, '\0');
num2 = Win32RegistryApi.RegSetValueEx(handle, name, IntPtr.Zero, valueKind, text2, text2.Length * this.NativeBytesPerCharacter);
}
else
{
if (type.IsArray)
{
throw new ArgumentException("Only string and byte arrays can written as registry values");
}
throw new ArgumentException("Type does not match the valueKind");
}
if (num2 != 0)
{
this.GenerateException(num2);
}
}
// Token: 0x06000778 RID: 1912 RVA: 0x00017724 File Offset: 0x00015924
public void SetValue(RegistryKey rkey, string name, object value)
{
Type type = value.GetType();
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
int num2;
if (type == typeof(int))
{
int num = (int)value;
num2 = Win32RegistryApi.RegSetValueEx(handle, name, IntPtr.Zero, RegistryValueKind.DWord, ref num, 4);
}
else if (type == typeof(byte[]))
{
byte[] array = (byte[])value;
num2 = Win32RegistryApi.RegSetValueEx(handle, name, IntPtr.Zero, RegistryValueKind.Binary, array, array.Length);
}
else if (type == typeof(string[]))
{
string[] array2 = (string[])value;
StringBuilder stringBuilder = new StringBuilder();
foreach (string text in array2)
{
stringBuilder.Append(text);
stringBuilder.Append('\0');
}
stringBuilder.Append('\0');
byte[] bytes = Encoding.Unicode.GetBytes(stringBuilder.ToString());
num2 = Win32RegistryApi.RegSetValueEx(handle, name, IntPtr.Zero, RegistryValueKind.MultiString, bytes, bytes.Length);
}
else
{
if (type.IsArray)
{
throw new ArgumentException("Only string and byte arrays can written as registry values");
}
string text2 = string.Format("{0}{1}", value, '\0');
num2 = Win32RegistryApi.RegSetValueEx(handle, name, IntPtr.Zero, RegistryValueKind.String, text2, text2.Length * this.NativeBytesPerCharacter);
}
if (num2 == 1018)
{
throw RegistryKey.CreateMarkedForDeletionException();
}
if (num2 != 0)
{
this.GenerateException(num2);
}
}
// Token: 0x06000779 RID: 1913 RVA: 0x00017890 File Offset: 0x00015A90
private int GetBinaryValue(RegistryKey rkey, string name, RegistryValueKind type, out byte[] data, int size)
{
byte[] array = new byte[size];
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
int num = Win32RegistryApi.RegQueryValueEx(handle, name, IntPtr.Zero, ref type, array, ref size);
data = array;
return num;
}
// Token: 0x0600077A RID: 1914 RVA: 0x000178C4 File Offset: 0x00015AC4
public int SubKeyCount(RegistryKey rkey)
{
StringBuilder stringBuilder = new StringBuilder(1024);
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
int num = 0;
for (;;)
{
int num2 = Win32RegistryApi.RegEnumKey(handle, num, stringBuilder, stringBuilder.Capacity);
if (num2 == 1018)
{
break;
}
if (num2 != 0)
{
if (num2 == 259)
{
return num;
}
this.GenerateException(num2);
}
num++;
}
throw RegistryKey.CreateMarkedForDeletionException();
}
// Token: 0x0600077B RID: 1915 RVA: 0x00017938 File Offset: 0x00015B38
public int ValueCount(RegistryKey rkey)
{
StringBuilder stringBuilder = new StringBuilder(1024);
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
int num = 0;
for (;;)
{
RegistryValueKind registryValueKind = RegistryValueKind.Unknown;
int capacity = stringBuilder.Capacity;
int num2 = Win32RegistryApi.RegEnumValue(handle, num, stringBuilder, ref capacity, IntPtr.Zero, ref registryValueKind, IntPtr.Zero, IntPtr.Zero);
if (num2 == 1018)
{
break;
}
if (num2 != 0 && num2 != 234)
{
if (num2 == 259)
{
return num;
}
this.GenerateException(num2);
}
num++;
}
throw RegistryKey.CreateMarkedForDeletionException();
}
// Token: 0x0600077C RID: 1916 RVA: 0x000179D0 File Offset: 0x00015BD0
public RegistryKey OpenRemoteBaseKey(RegistryHive hKey, string machineName)
{
IntPtr intPtr = new IntPtr((int)hKey);
IntPtr intPtr2;
int num = Win32RegistryApi.RegConnectRegistry(machineName, intPtr, out intPtr2);
if (num != 0)
{
this.GenerateException(num);
}
return new RegistryKey(hKey, intPtr2, true);
}
// Token: 0x0600077D RID: 1917 RVA: 0x00017A04 File Offset: 0x00015C04
public RegistryKey OpenSubKey(RegistryKey rkey, string keyName, bool writable)
{
int num = 131097;
if (writable)
{
num |= 131078;
}
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
IntPtr intPtr;
int num2 = Win32RegistryApi.RegOpenKeyEx(handle, keyName, IntPtr.Zero, num, out intPtr);
if (num2 == 2 || num2 == 1018)
{
return null;
}
if (num2 != 0)
{
this.GenerateException(num2);
}
return new RegistryKey(intPtr, Win32RegistryApi.CombineName(rkey, keyName), writable);
}
// Token: 0x0600077E RID: 1918 RVA: 0x00017A70 File Offset: 0x00015C70
public void Flush(RegistryKey rkey)
{
if (!Win32RegistryApi.IsHandleValid(rkey))
{
return;
}
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
Win32RegistryApi.RegFlushKey(handle);
}
// Token: 0x0600077F RID: 1919 RVA: 0x00017A98 File Offset: 0x00015C98
public void Close(RegistryKey rkey)
{
if (!Win32RegistryApi.IsHandleValid(rkey))
{
return;
}
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
Win32RegistryApi.RegCloseKey(handle);
}
// Token: 0x06000780 RID: 1920 RVA: 0x00017AC0 File Offset: 0x00015CC0
public RegistryKey CreateSubKey(RegistryKey rkey, string keyName)
{
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
IntPtr intPtr;
int num = Win32RegistryApi.RegCreateKey(handle, keyName, out intPtr);
if (num == 1018)
{
throw RegistryKey.CreateMarkedForDeletionException();
}
if (num != 0)
{
this.GenerateException(num);
}
return new RegistryKey(intPtr, Win32RegistryApi.CombineName(rkey, keyName), true);
}
// Token: 0x06000781 RID: 1921 RVA: 0x00017B10 File Offset: 0x00015D10
public void DeleteKey(RegistryKey rkey, string keyName, bool shouldThrowWhenKeyMissing)
{
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
int num = Win32RegistryApi.RegDeleteKey(handle, keyName);
if (num != 2)
{
if (num != 0)
{
this.GenerateException(num);
}
return;
}
if (shouldThrowWhenKeyMissing)
{
throw new ArgumentException("key " + keyName);
}
}
// Token: 0x06000782 RID: 1922 RVA: 0x00017B58 File Offset: 0x00015D58
public void DeleteValue(RegistryKey rkey, string value, bool shouldThrowWhenKeyMissing)
{
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
int num = Win32RegistryApi.RegDeleteValue(handle, value);
if (num == 1018)
{
return;
}
if (num != 2)
{
if (num != 0)
{
this.GenerateException(num);
}
return;
}
if (shouldThrowWhenKeyMissing)
{
throw new ArgumentException("value " + value);
}
}
// Token: 0x06000783 RID: 1923 RVA: 0x00017BAC File Offset: 0x00015DAC
public string[] GetSubKeyNames(RegistryKey rkey)
{
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
StringBuilder stringBuilder = new StringBuilder(1024);
ArrayList arrayList = new ArrayList();
int num = 0;
for (;;)
{
int num2 = Win32RegistryApi.RegEnumKey(handle, num, stringBuilder, stringBuilder.Capacity);
if (num2 == 0)
{
arrayList.Add(stringBuilder.ToString());
stringBuilder.Length = 0;
}
else
{
if (num2 == 259)
{
break;
}
this.GenerateException(num2);
}
num++;
}
return (string[])arrayList.ToArray(typeof(string));
}
// Token: 0x06000784 RID: 1924 RVA: 0x00017C40 File Offset: 0x00015E40
public string[] GetValueNames(RegistryKey rkey)
{
IntPtr handle = Win32RegistryApi.GetHandle(rkey);
ArrayList arrayList = new ArrayList();
int num = 0;
for (;;)
{
StringBuilder stringBuilder = new StringBuilder(1024);
int capacity = stringBuilder.Capacity;
RegistryValueKind registryValueKind = RegistryValueKind.Unknown;
int num2 = Win32RegistryApi.RegEnumValue(handle, num, stringBuilder, ref capacity, IntPtr.Zero, ref registryValueKind, IntPtr.Zero, IntPtr.Zero);
if (num2 == 0 || num2 == 234)
{
arrayList.Add(stringBuilder.ToString());
}
else
{
if (num2 == 259)
{
break;
}
if (num2 == 1018)
{
goto Block_3;
}
this.GenerateException(num2);
}
num++;
}
return (string[])arrayList.ToArray(typeof(string));
Block_3:
throw RegistryKey.CreateMarkedForDeletionException();
}
// Token: 0x06000785 RID: 1925 RVA: 0x00017D04 File Offset: 0x00015F04
private void GenerateException(int errorCode)
{
switch (errorCode)
{
case 2:
break;
default:
if (errorCode == 53)
{
throw new IOException("The network path was not found.");
}
if (errorCode != 87)
{
throw new SystemException();
}
break;
case 5:
throw new SecurityException();
}
throw new ArgumentException();
}
// Token: 0x06000786 RID: 1926 RVA: 0x00017D5C File Offset: 0x00015F5C
public string ToString(RegistryKey rkey)
{
return rkey.Name;
}
// Token: 0x06000787 RID: 1927 RVA: 0x00017D64 File Offset: 0x00015F64
internal static string CombineName(RegistryKey rkey, string localName)
{
return rkey.Name + "\\" + localName;
}
// Token: 0x04000109 RID: 265
private const int OpenRegKeyRead = 131097;
// Token: 0x0400010A RID: 266
private const int OpenRegKeyWrite = 131078;
// Token: 0x0400010B RID: 267
private const int Int32ByteSize = 4;
// Token: 0x0400010C RID: 268
private const int BufferMaxLength = 1024;
// Token: 0x0400010D RID: 269
private readonly int NativeBytesPerCharacter = Marshal.SystemDefaultCharSize;
}
}