Пример #1
0
        public static void SetValueEx(object key, string valueName, int reserved, int type, object value)
        {
            HKEYType          rootKey = GetRootKey(key);
            RegistryValueKind regKind = (RegistryValueKind)type;

            if (regKind == RegistryValueKind.MultiString)
            {
                int      size     = ((List)value)._size;
                string[] strArray = new string[size];
                Array.Copy(((List)value)._data, strArray, size);
                rootKey.key.SetValue(valueName, strArray, regKind);
            }
            else if (regKind == RegistryValueKind.Binary)
            {
                byte[] byteArr = null;
                if (value is string)
                {
                    string        strValue = value as string;
                    ASCIIEncoding encoding = new ASCIIEncoding();
                    byteArr = encoding.GetBytes(strValue);
                }
                rootKey.key.SetValue(valueName, byteArr, regKind);
            }
            else
            {
                rootKey.key.SetValue(valueName, value, regKind);
            }
        }
Пример #2
0
        public static PythonTuple QueryInfoKey(object key)
        {
            HKEYType rootKey = null;

            //The key can also be a handle. If it is, then retrieve it from the cache.
            if (key is int)
            {
                if (HKeyHandleCache.cache.ContainsKey((int)key))
                {
                    if (HKeyHandleCache.cache[(int)key].IsAlive)
                    {
                        rootKey = HKeyHandleCache.cache[(int)key].Target as HKEYType;
                    }
                }
            }
            else
            {
                rootKey = GetRootKey(key);
            }

            if (rootKey == null)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, "key has been closed");
            }

            try {
                var nativeRootKey = rootKey.GetKey();
                return(PythonTuple.MakeTuple(nativeRootKey.SubKeyCount, nativeRootKey.ValueCount, 0));
            } catch (ObjectDisposedException e) {
                throw new ExternalException(e.Message);
            }
        }
Пример #3
0
        public static void DeleteKey(object key, string sub_key)
        {
            HKEYType rootKey = GetRootKey(key);

            if (key is BigInteger && string.IsNullOrEmpty(sub_key))
            {
                throw new InvalidCastException("DeleteKey() argument 2 must be string, not None");
            }

            // the .NET APIs don't work with a key name of length 256, use a PInvoke instead
            // unlike in CreateKey(), `subKeyName.Length` on deletion can be greater than 256 if combined with parent key name.
            if (sub_key.Length >= 256)
            {
                int result = RegDeleteKey(rootKey.GetKey().Handle, sub_key);
                if (result != ERROR_SUCCESS)
                {
                    throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, result);
                }
                return;
            }

            try {
                rootKey.GetKey().DeleteSubKey(sub_key);
            } catch (ArgumentException e) {
                throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, e.HResult);
            }
        }
Пример #4
0
        public static HKEYType CreateKeyEx(object key, string sub_key, int reserved = 0, int access = KEY_ALL_ACCESS)
        {
            HKEYType rootKey = GetRootKey(key);

            //if key is a system key and no subkey is specified return that.
            if (key is BigInteger && string.IsNullOrEmpty(sub_key))
            {
                return(rootKey);
            }

            SafeRegistryHandle handle;
            int disposition;

            int result = RegCreateKeyEx(
                rootKey.GetKey().Handle,
                sub_key,
                0,
                null,
                RegistryOptions.None,
                (RegistryRights)access,
                IntPtr.Zero,
                out handle,
                out disposition
                );

            if (result != ERROR_SUCCESS)
            {
                throw PythonExceptions.CreateThrowable(error, result, FormatError(result));
            }

            return(new HKEYType(RegistryKey.FromHandle(handle)));
        }
Пример #5
0
        public static PythonTuple EnumValue(object key, int index)
        {
            HKEYType rootKey = GetRootKey(key);

            if (index >= rootKey.key.ValueCount)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.WindowsError, PythonExceptions._WindowsError.ERROR_BAD_COMMAND, "No more data is available");
            }

            string valueName = rootKey.key.GetValueNames()[index];
            int    valueKind = MapRegistryValueKind(rootKey.key.GetValueKind(valueName));

            object value = rootKey.key.GetValue(valueName);

            //Handle some special cases of registry values.
            if (valueKind == REG_MULTI_SZ)
            {
                value = new List(value);
            }
            else if (valueKind == REG_BINARY)
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                value = encoding.GetString((byte[])value);
            }
            // REG_EXPAND_SZ expands any environment variable present in the registry key.
            // CPython does the wrong thing and returns the unexpanded value. Should we put in a
            // hack to return the wrong value that CPython does?

            return(PythonTuple.MakeTuple(valueName, value, valueKind));
        }
Пример #6
0
        public static PythonTuple QueryInfoKey(object key)
        {
            HKEYType rootKey = null;

            //The key can also be a handle. If it is, then retrieve it from the cache.
            if (key is int)
            {
                if (HKeyHandleCache.cache.ContainsKey((int)key))
                {
                    if (HKeyHandleCache.cache[(int)key].IsAlive)
                    {
                        rootKey = HKeyHandleCache.cache[(int)key].Target as HKEYType;
                    }
                }
            }
            else
            {
                rootKey = GetRootKey(key);
            }

            try {
                return(PythonTuple.MakeTuple(rootKey.key.SubKeyCount, rootKey.key.ValueCount, 0));
            } catch (ObjectDisposedException e) {
                throw new ExternalException(e.Message);
            }
        }
Пример #7
0
        public static PythonTuple QueryInfoKey(object key)
        {
            HKEYType rootKey = null;

            //The key can also be a handle. If it is, then retrieve it from the cache.
            if (key is int)
            {
                if (HKEYType.cache.TryGetValue((int)key, out var value))
                {
                    value.TryGetTarget(out rootKey);
                }
            }
            else
            {
                rootKey = GetRootKey(key);
            }

            if (rootKey == null)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.EnvironmentError, "key has been closed");
            }

            try {
                var nativeRootKey = rootKey.GetKey();
                return(PythonTuple.MakeTuple(nativeRootKey.SubKeyCount, nativeRootKey.ValueCount, 0));
            } catch (ObjectDisposedException e) {
                throw new ExternalException(e.Message);
            }
        }
Пример #8
0
        public static void SetValueEx(object key, string value_name, object reserved, int type, object value)
        {
            HKEYType          rootKey = GetRootKey(key);
            RegistryValueKind regKind = (RegistryValueKind)type;

            // null is a valid value but RegistryKey.SetValue doesn't like it so PInvoke to set it
            if (value == null)
            {
                RegSetValueEx(rootKey.GetKey().Handle, value_name, 0, type, null, 0);
                return;
            }

            if (regKind == RegistryValueKind.MultiString)
            {
                int      size     = ((PythonList)value)._size;
                string[] strArray = new string[size];
                Array.Copy(((PythonList)value)._data, strArray, size);
                rootKey.GetKey().SetValue(value_name, strArray, regKind);
            }
            else if (regKind == RegistryValueKind.Binary)
            {
                byte[] byteArr = null;
                if (value is string)
                {
                    string        strValue = value as string;
                    ASCIIEncoding encoding = new ASCIIEncoding();
                    byteArr = encoding.GetBytes(strValue);
                }
                else if (value is Bytes ibytes)
                {
                    byteArr = ibytes.UnsafeByteArray;
                }
                rootKey.GetKey().SetValue(value_name, byteArr, regKind);
            }
            else if (regKind == RegistryValueKind.DWord)
            {
                // DWORDs are uint but the .NET API requires int
                if (value is BigInteger)
                {
                    var val = (uint)(BigInteger)value;
                    value = unchecked ((int)val);
                }
                rootKey.GetKey().SetValue(value_name, value, regKind);
            }
            else if (regKind == RegistryValueKind.QWord)
            {
                // QWORDs are ulong but the .NET API requires long
                if (value is BigInteger)
                {
                    var val = (ulong)(BigInteger)value;
                    value = unchecked ((long)val);
                }
                rootKey.GetKey().SetValue(value_name, value, regKind);
            }
            else
            {
                rootKey.GetKey().SetValue(value_name, value, regKind);
            }
        }
Пример #9
0
        public static HKEYType OpenKey(object key, string sub_key, int reserved = 0, int access = KEY_READ)
        {
            HKEYType    rootKey = GetRootKey(key);
            RegistryKey newKey  = null;

            // I'm assuming that the masks that CPy uses are the same as the Win32 API one mentioned here-
            // http://msdn2.microsoft.com/en-us/library/ms724878(VS.85).aspx

            // KEY_WRITE is a combination of KEY_SET_VALUE and KEY_CREATE_SUB_KEY. We'll open with write access
            // if any of this is set.
            // KEY_READ is a combination of KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS and KEY_NOTIFY. We'll open
            // with read access for all of these.

            var nativeRootKey = rootKey.GetKey();

            try {
                if ((access & KEY_SET_VALUE) == KEY_SET_VALUE ||
                    (access & KEY_CREATE_SUB_KEY) == KEY_CREATE_SUB_KEY)
                {
                    if (reserved != 0)
                    {
                        newKey = nativeRootKey.OpenSubKey(sub_key, RegistryKeyPermissionCheck.Default, (RegistryRights)reserved);
                    }
                    else
                    {
                        newKey = nativeRootKey.OpenSubKey(sub_key, true);
                    }
                }
                else if ((access & KEY_QUERY_VALUE) == KEY_QUERY_VALUE ||
                         (access & KEY_ENUMERATE_SUB_KEYS) == KEY_ENUMERATE_SUB_KEYS ||
                         (access & KEY_NOTIFY) == KEY_NOTIFY)
                {
                    if (reserved != 0)
                    {
                        newKey = nativeRootKey.OpenSubKey(sub_key, RegistryKeyPermissionCheck.ReadSubTree, (RegistryRights)reserved);
                    }
                    else
                    {
                        newKey = nativeRootKey.OpenSubKey(sub_key, false);
                    }
                }
                else
                {
                    throw new Win32Exception("Unexpected mode");
                }
            } catch (SecurityException) {
                throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, PythonExceptions._OSError.ERROR_ACCESS_DENIED, "Access is denied", null, PythonExceptions._OSError.ERROR_ACCESS_DENIED);
            }


            if (newKey == null)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, PythonExceptions._OSError.ERROR_FILE_NOT_FOUND, "The system cannot find the file specified", null, PythonExceptions._OSError.ERROR_FILE_NOT_FOUND);
            }

            return(new HKEYType(newKey));
        }
Пример #10
0
        public static string EnumKey(object key, int index)
        {
            HKEYType rootKey = GetRootKey(key);

            if (index >= rootKey.key.SubKeyCount)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.WindowsError, PythonExceptions._WindowsError.ERROR_BAD_COMMAND, "No more data is available");
            }
            return(rootKey.key.GetSubKeyNames()[index]);
        }
Пример #11
0
        public static PythonTuple QueryValueEx(object key, string valueName)
        {
            HKEYType rootKey = GetRootKey(key);

            int    valueKind;
            object value;

            QueryValueExImpl(rootKey.GetKey(), valueName, out valueKind, out value);

            return(PythonTuple.MakeTuple(value, valueKind));
        }
Пример #12
0
        public static PythonTuple QueryValueEx(object key, string value_name)
        {
            HKEYType rootKey = GetRootKey(key);

            // it looks like rootKey.GetKey().Handle fails on HKEY_PERFORMANCE_DATA so manually create the handle instead
            var handle = rootKey.hkey == HKEY_PERFORMANCE_DATA ? new SafeRegistryHandle(new IntPtr(unchecked ((int)0x80000004)), true) : rootKey.GetKey().Handle;

            int    valueKind;
            object value;

            QueryValueExImpl(handle, value_name, out valueKind, out value);

            return(PythonTuple.MakeTuple(value, valueKind));
        }
Пример #13
0
        public static HKEYType CreateKey(object key, string subKeyName)
        {
            HKEYType rootKey = GetRootKey(key);

            //if key is a system key and no subkey is specified return that.
            if (key is BigInteger && string.IsNullOrEmpty(subKeyName))
            {
                return(rootKey);
            }

            HKEYType subKey = new HKEYType(rootKey.key.CreateSubKey(subKeyName));

            return(subKey);
        }
Пример #14
0
        public static string EnumKey(object key, int index)
        {
            HKEYType rootKey = GetRootKey(key);

            int           len  = 257; // maximum key name length is 256
            StringBuilder name = new StringBuilder(len);
            int           ret  = RegEnumKeyEx(rootKey.GetKey().Handle, index, name, ref len, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            if (ret != ERROR_SUCCESS)
            {
                Debug.Assert(ret == ERROR_NO_MORE_ITEMS);
                throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, PythonExceptions._OSError.ERROR_BAD_COMMAND, "No more data is available", null, PythonExceptions._OSError.ERROR_BAD_COMMAND);
            }
            return(name.ToString());
        }
Пример #15
0
        public static void DeleteKey(object key, string subKeyName)
        {
            HKEYType rootKey = GetRootKey(key);

            if (key is BigInteger && string.IsNullOrEmpty(subKeyName))
            {
                throw new InvalidCastException("DeleteKey() argument 2 must be string, not None");
            }

            try {
                rootKey.key.DeleteSubKey(subKeyName);
            } catch (ArgumentException e) {
                throw new ExternalException(e.Message);
            }
        }
Пример #16
0
        public static void EnableReflectionKey(object key)
        {
            HKEYType rootKey = GetRootKey(key);

            if (!Environment.Is64BitOperatingSystem)
            {
                throw new NotImplementedException("not implemented on this platform");
            }

            int dwRet = RegEnableReflectionKey(rootKey.GetKey().Handle);

            if (dwRet != ERROR_SUCCESS)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, dwRet);
            }
        }
Пример #17
0
        public static void DeleteKeyEx(object key, string sub_key, int access = KEY_WOW64_64KEY, int reserved = 0)
        {
            HKEYType rootKey = GetRootKey(key);

            if (key is BigInteger && string.IsNullOrEmpty(sub_key))
            {
                throw new InvalidCastException("DeleteKeyEx() argument 2 must be string, not None");
            }

            int result = RegDeleteKeyEx(rootKey.GetKey().Handle, sub_key, access, reserved);

            if (result != ERROR_SUCCESS)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, result);
            }
        }
Пример #18
0
        public static PythonTuple EnumValue(object key, int index)
        {
            HKEYType rootKey = GetRootKey(key);

            if (index >= rootKey.GetKey().ValueCount)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.WindowsError, PythonExceptions._WindowsError.ERROR_BAD_COMMAND, "No more data is available");
            }

            var    nativeRootKey = rootKey.GetKey();
            string valueName     = nativeRootKey.GetValueNames()[index];

            int    valueKind;
            object value;

            QueryValueExImpl(nativeRootKey, valueName, out valueKind, out value);
            return(PythonTuple.MakeTuple(valueName, value, valueKind));
        }
Пример #19
0
        private static HKEYType GetRootKey(object key)
        {
            HKEYType rootKey;

            rootKey = key as HKEYType;
            if (rootKey == null)
            {
                if (key is BigInteger)
                {
                    rootKey = new HKEYType(RegistryKey.OpenBaseKey(MapSystemKey((BigInteger)key), RegistryView.Default), (BigInteger)key);
                }
                else
                {
                    throw new InvalidCastException("The object is not a PyHKEY object");
                }
            }
            return(rootKey);
        }
Пример #20
0
        public static PythonTuple QueryValueEx(object key, string valueName)
        {
            HKEYType rootKey   = GetRootKey(key);
            object   value     = rootKey.key.GetValue(valueName);
            int      valueKind = MapRegistryValueKind(rootKey.key.GetValueKind(valueName));

            if (valueKind == REG_MULTI_SZ)
            {
                value = new List(value);
            }
            if (valueKind == REG_BINARY)
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                value = encoding.GetString((byte[])value);
            }

            return(PythonTuple.MakeTuple(value, valueKind));
        }
Пример #21
0
        public static bool QueryReflectionKey(object key)
        {
            HKEYType rootKey = GetRootKey(key);
            bool     isDisabled;

            if (!Environment.Is64BitOperatingSystem)
            {
                throw new NotImplementedException("not implemented on this platform");
            }

            int dwRet = RegQueryReflectionKey(rootKey.GetKey().Handle, out isDisabled);

            if (dwRet != ERROR_SUCCESS)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.WindowsError, dwRet);
            }
            return(isDisabled);
        }
Пример #22
0
        public static HKEYType CreateKey(object key, string sub_key)
        {
            // the .NET APIs don't work with a key name of length 256, use CreateKeyEx which PInvokes instead
            if (sub_key.Length == 256)
            {
                return(CreateKeyEx(key, sub_key, 0, KEY_ALL_ACCESS));
            }

            HKEYType rootKey = GetRootKey(key);

            //if key is a system key and no subkey is specified return that.
            if (key is BigInteger && string.IsNullOrEmpty(sub_key))
            {
                return(rootKey);
            }

            HKEYType subKey = new HKEYType(rootKey.GetKey().CreateSubKey(sub_key));

            return(subKey);
        }
Пример #23
0
        public static PythonTuple EnumValue(object key, int index)
        {
            HKEYType rootKey = GetRootKey(key);

            if (index >= rootKey.GetKey().ValueCount)
            {
                throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, PythonExceptions._OSError.ERROR_BAD_COMMAND, "No more data is available", null, PythonExceptions._OSError.ERROR_BAD_COMMAND);
            }

            var    nativeRootKey = rootKey.GetKey();
            string valueName     = nativeRootKey.GetValueNames()[index];

            // it looks like nativeRootKey.Handle fails on HKEY_PERFORMANCE_DATA so manually create the handle instead
            var handle = rootKey.hkey == HKEY_PERFORMANCE_DATA ? new SafeRegistryHandle(new IntPtr(unchecked ((int)0x80000004)), true) : nativeRootKey.Handle;

            int    valueKind;
            object value;

            QueryValueExImpl(handle, valueName, out valueKind, out value);
            return(PythonTuple.MakeTuple(valueName, value, valueKind));
        }
Пример #24
0
        public static void DeleteKey(object key, string subKeyName)
        {
            HKEYType rootKey = GetRootKey(key);

            if (key is BigInteger && string.IsNullOrEmpty(subKeyName))
            {
                throw new InvalidCastException("DeleteKey() argument 2 must be string, not None");
            }

            // the .NET APIs don't work with a key name of length 256, use a PInvoke instead
            if (subKeyName.Length == 256)
            {
                RegDeleteKey(rootKey.GetKey().Handle, subKeyName);
                return;
            }

            try {
                rootKey.GetKey().DeleteSubKey(subKeyName);
            } catch (ArgumentException e) {
                throw new ExternalException(e.Message);
            }
        }
Пример #25
0
 public static void CloseKey(HKEYType key)
 {
     key.Close();
 }
Пример #26
0
        public static void SetValue(object key, string subKeyName, int type, string value)
        {
            HKEYType pyKey = CreateKey(key, subKeyName);

            pyKey.key.SetValue(null, value);
        }
Пример #27
0
        public static HKEYType CreateKey(object key, string subKeyName) {
            HKEYType rootKey = GetRootKey(key);

            //if key is a system key and no subkey is specified return that.
            if (key is BigInteger && string.IsNullOrEmpty(subKeyName))
                return rootKey;
            
            HKEYType subKey = new HKEYType(rootKey.GetKey().CreateSubKey(subKeyName));
            return subKey;
        }
Пример #28
0
 private static HKEYType GetRootKey(object key) {
     HKEYType rootKey;
     rootKey = key as HKEYType;
     if (rootKey == null) {
         if (key is BigInteger) {
             rootKey = new HKEYType(RegistryKey.OpenRemoteBaseKey(MapSystemKey((BigInteger)key), string.Empty));
         } else {
             throw new InvalidCastException("The object is not a PyHKEY object");
         }
     }
     return rootKey;
 }
Пример #29
0
        public static void SetValue(object key, string sub_key, int type, string value)
        {
            HKEYType pyKey = CreateKey(key, sub_key);

            pyKey.GetKey().SetValue(null, value);
        }
Пример #30
0
        public static HKEYType CreateKey(object key, string subKeyName) {
            HKEYType rootKey = GetRootKey(key);

            //if key is a system key and no subkey is specified return that.
            BigInteger bi = key as BigInteger;
            if (!Object.ReferenceEquals(bi, null) && string.IsNullOrEmpty(subKeyName))
                return rootKey;

            HKEYType subKey = new HKEYType(rootKey.key.CreateSubKey(subKeyName));
            return subKey;
        }
Пример #31
0
        public static object QueryValue(object key, string sub_key)
        {
            HKEYType pyKey = OpenKey(key, sub_key);

            return(pyKey.GetKey().GetValue(null));
        }
Пример #32
0
 private static HKEYType GetRootKey(object key) {
     HKEYType rootKey;
     rootKey = key as HKEYType;
     if (rootKey == null) {
         BigInteger bi = key as BigInteger;
         if (!Object.ReferenceEquals(bi, null)) {
             rootKey = new HKEYType(RegistryKey.OpenRemoteBaseKey(MapSystemKey(bi), string.Empty));
         } else {
             throw new InvalidCastException("The object is not a PyHKEY object");
         }
     }
     return rootKey;
 }
Пример #33
0
 public static void CloseKey(HKEYType key) {
     key.Close();
 }
Пример #34
0
        public static void FlushKey(object key)
        {
            HKEYType rootKey = GetRootKey(key);

            rootKey.GetKey().Flush();
        }
Пример #35
0
        public static void DeleteValue(object key, string value)
        {
            HKEYType rootKey = GetRootKey(key);

            rootKey.GetKey().DeleteValue(value, true);
        }