Ejemplo n.º 1
0
        /// <summary>
        /// RegInject: Sets a value to the REG_NONE type. Therefore only its name is required.
        /// </summary>
        /// <param name="name">The name of the value.</param>
        public void SetValueNone(string name)
        {
            IntPtr dataPtr = IntPtr.Zero;

            try
            {
                dataPtr = Marshal.AllocHGlobal(0);
                //Marshal.Copy(data, 0, dataPtr, data.Length);

                Win32Result result = OffregNative.SetValue(
                    _intPtr, name, RegValueType.REG_NONE, dataPtr, (uint)0);

                if (result != Win32Result.ERROR_SUCCESS)
                {
                    throw new Win32Exception((int)result);
                }
            }
            finally
            {
                if (dataPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(dataPtr);
                }
            }

            RefreshMetadata();
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Enumerates all subkeys, retrieving both their name and class at the same time.
        /// </summary>
        /// <returns>Names and classes of all the subkeys.</returns>
        public SubKeyContainer[] EnumerateSubKeys()
        {
            SubKeyContainer[] results = new SubKeyContainer[_metadata.SubKeysCount];

            for (uint item = 0; item < _metadata.SubKeysCount; item++)
            {
                uint sizeName  = _metadata.MaxSubKeyLen + 1;
                uint sizeClass = _metadata.MaxClassLen + 1;

                StringBuilder sbName   = new StringBuilder((int)sizeName);
                StringBuilder sbClass  = new StringBuilder((int)sizeClass);
                FILETIME      fileTime = new FILETIME();

                Win32Result result = OffregNative.EnumKey(_intPtr, item, sbName, ref sizeName, sbClass, ref sizeClass,
                                                          ref fileTime);

                if (result != Win32Result.ERROR_SUCCESS)
                {
                    throw new Win32Exception((int)result);
                }

                SubKeyContainer container = new SubKeyContainer();

                container.Name          = sbName.ToString();
                container.Class         = sbClass.ToString();
                container.LastWriteTime = fileTime;

                results[item] = container;
            }

            return(results);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Sets a value to the specified type.
        /// </summary>
        /// <param name="name">The name of the value.</param>
        /// <param name="type">The optional type for the value.</param>
        /// <param name="data">The data for the value.</param>
        private void SetValue(string name, RegValueType type, byte[] data)
        {
            IntPtr dataPtr = IntPtr.Zero;

            try
            {
                dataPtr = Marshal.AllocHGlobal(data.Length);
                Marshal.Copy(data, 0, dataPtr, data.Length);

                Win32Result result = OffregNative.SetValue(_intPtr, name, type, dataPtr, (uint)data.Length);

                if (result != Win32Result.ERROR_SUCCESS)
                {
                    throw new Win32Exception((int)result);
                }
            }
            finally
            {
                if (dataPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(dataPtr);
                }
            }

            RefreshMetadata();
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Enumerates all values, retrieving both their name, data and type at the same time.
        /// </summary>
        /// <returns>Names, datas and types of all the values.</returns>
        public ValueContainer[] EnumerateValues()
        {
            ValueContainer[] results = new ValueContainer[_metadata.ValuesCount];

            // Allocate data buffer
            IntPtr dataPtr = IntPtr.Zero;

            try
            {
                dataPtr = Marshal.AllocHGlobal((int)_metadata.MaxValueLen);

                // Iterate all values
                for (uint item = 0; item < _metadata.ValuesCount; item++)
                {
                    uint sizeName = _metadata.MaxValueNameLen + 1;
                    uint sizeData = _metadata.MaxValueLen;

                    StringBuilder sbName = new StringBuilder((int)sizeName);
                    RegValueType  type;

                    // Get item
                    Win32Result result = OffregNative.EnumValue(_intPtr, item, sbName, ref sizeName, out type, dataPtr,
                                                                ref sizeData);

                    if (result != Win32Result.ERROR_SUCCESS)
                    {
                        throw new Win32Exception((int)result);
                    }

                    byte[] data = new byte[sizeData];
                    Marshal.Copy(dataPtr, data, 0, (int)sizeData);

                    ValueContainer container = new ValueContainer();

                    if (!Enum.IsDefined(typeof(RegValueType), type))
                    {
                        WarnDebugForValueType(sbName.ToString(), type);
                        type = RegValueType.REG_BINARY;
                    }

                    object parsedData;
                    container.Name        = sbName.ToString();
                    container.InvalidData = !OffregHelper.TryConvertValueDataToObject(type, data, out parsedData);
                    container.Data        = parsedData;
                    container.Type        = type;

                    results[item] = container;
                }
            }
            finally
            {
                if (dataPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(dataPtr);
                }
            }

            return(results);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Detect if a value exists in this key.
        /// </summary>
        /// <param name="name">The value to find</param>
        /// <returns>True if it exists, false otherwise.</returns>
        public bool ValueExist(string name)
        {
            RegValueType type;
            uint         size   = 0;
            Win32Result  result = OffregNative.GetValue(_intPtr, null, name, out type, IntPtr.Zero, ref size);

            return(result == Win32Result.ERROR_SUCCESS);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///     Saves a hive to Disk.
        ///     See http://msdn.microsoft.com/en-us/library/ee210773(v=vs.85).aspx for more details.
        /// </summary>
        /// <remarks>The target file must not exist.</remarks>
        /// <param name="targetFile">The target file to write to.</param>
        /// <param name="majorVersionTarget">The compatibility version to save for, see the link in summary.</param>
        /// <param name="minorVersionTarget">The compatibility version to save for, see the link in summary.</param>
        public void SaveHive(string targetFile, uint majorVersionTarget, uint minorVersionTarget)
        {
            Win32Result res = OffregNative.SaveHive(_intPtr, targetFile, majorVersionTarget, minorVersionTarget);

            if (res != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)res);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Deletes a specified value.
        /// </summary>
        /// <param name="name">The name of the value to delete.</param>
        public void DeleteValue(string name)
        {
            Win32Result result = OffregNative.DeleteValue(_intPtr, name);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            RefreshMetadata();
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Calls QueryInfoKey and updates _metadata.
        /// </summary>
        private void RefreshMetadata()
        {
            uint     sizeClass = 0;
            uint     countSubKeys = 0, maxSubKeyLen = 0, maxClassLen = 0;
            uint     countValues = 0, maxValueNameLen = 0, maxValueLen = 0;
            uint     securityDescriptorSize = 0;
            FILETIME lastWrite              = new FILETIME();

            // Get size of class
            StringBuilder sbClass = new StringBuilder((int)sizeClass);

            Win32Result result = OffregNative.QueryInfoKey(_intPtr, sbClass, ref sizeClass, ref countSubKeys,
                                                           ref maxSubKeyLen,
                                                           ref maxClassLen,
                                                           ref countValues, ref maxValueNameLen, ref maxValueLen,
                                                           ref securityDescriptorSize,
                                                           ref lastWrite);

            if (result == Win32Result.ERROR_MORE_DATA)
            {
                // The returned size does is in characters (unicode), excluding NULL chars. Increment it to have space
                sizeClass = sizeClass + 1;

                // Allocate
                sbClass = new StringBuilder((int)sizeClass);

                result = OffregNative.QueryInfoKey(_intPtr, sbClass, ref sizeClass, ref countSubKeys, ref maxSubKeyLen,
                                                   ref maxClassLen,
                                                   ref countValues, ref maxValueNameLen, ref maxValueLen,
                                                   ref securityDescriptorSize,
                                                   ref lastWrite);

                if (result != Win32Result.ERROR_SUCCESS)
                {
                    throw new Win32Exception((int)result);
                }
            }
            else if (result != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            _metadata.Class         = sbClass.ToString();
            _metadata.LastWriteTime = lastWrite;

            _metadata.SubKeysCount           = countSubKeys;
            _metadata.MaxSubKeyLen           = maxSubKeyLen;
            _metadata.MaxClassLen            = maxClassLen;
            _metadata.ValuesCount            = countValues;
            _metadata.MaxValueNameLen        = maxValueNameLen;
            _metadata.MaxValueLen            = maxValueLen; // Bytes
            _metadata.SizeSecurityDescriptor = securityDescriptorSize;
        }
Ejemplo n.º 9
0
        /// <summary>
        ///     Opens an existing hive from the disk.
        /// </summary>
        /// <param name="hiveFile">The file to open.</param>
        /// <returns>The newly opened hive.</returns>
        public static OffregHive Open(string hiveFile)
        {
            IntPtr      existingHive;
            Win32Result res = OffregNative.OpenHive(hiveFile, out existingHive);

            if (res != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)res);
            }

            return(new OffregHive(existingHive));
        }
Ejemplo n.º 10
0
        /// <summary>
        ///     Creates a new hive in memory.
        /// </summary>
        /// <returns>The newly created hive.</returns>
        public static OffregHive Create()
        {
            IntPtr      newHive;
            Win32Result res = OffregNative.CreateHive(out newHive);

            if (res != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)res);
            }

            return(new OffregHive(newHive));
        }
Ejemplo n.º 11
0
        public override void Close()
        {
            if (_intPtr != IntPtr.Zero && _ownsPointer)
            {
                Win32Result res = OffregNative.CloseKey(_intPtr);

                if (res != Win32Result.ERROR_SUCCESS)
                {
                    throw new Win32Exception((int)res);
                }
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        ///     Gets the type of a single value.
        /// </summary>
        /// <param name="name">The name of the value to retrieve the type of.</param>
        /// <returns>The type of the value.</returns>
        public RegValueType GetValueKind(string name)
        {
            RegValueType type;

            Win32Result result = OffregNative.GetValue(_intPtr, null, name, out type, IntPtr.Zero, IntPtr.Zero);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            return(type);
        }
Ejemplo n.º 13
0
        /// <summary>
        ///     Tries to opens a subkey.
        ///     Will handle multi-level names, such as "Software\SubKey\Subkey2\"
        /// </summary>
        /// <param name="name">Name of the subkey to open.</param>
        /// <param name="key">The newly opened subkey</param>
        /// <returns>True if the operation was sucessful, false otherwise.</returns>
        public bool TryOpenSubKey(string name, out OffregKey key)
        {
            IntPtr      childPtr;
            Win32Result result = OffregNative.OpenKey(_intPtr, name, out childPtr);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                key = null;
                return(false);
            }

            key = new OffregKey(this, childPtr, name);
            return(true);
        }
Ejemplo n.º 14
0
        /// <summary>
        ///     Deletes a subkey of this key. The subkey must not contain any subkeys of its own, to delete recursively - see
        ///     <see
        ///         cref="DeleteSubKeyTree" />
        ///     .
        /// </summary>
        /// <param name="name">The name of the subkey to delete</param>
        public void DeleteSubKey(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            Win32Result result = OffregNative.DeleteKey(_intPtr, name);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            RefreshMetadata();
        }
Ejemplo n.º 15
0
        /// <summary>
        ///     Constructor, opens a subkey.
        /// </summary>
        /// <param name="parentKey">The parent key.</param>
        /// <param name="name">The name of the subkey to open.</param>
        internal OffregKey(OffregKey parentKey, string name)
        {
            Win32Result result = OffregNative.OpenKey(parentKey._intPtr, name, out _intPtr);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            Name     = name;
            FullName = (parentKey.FullName == null ? "" : parentKey.FullName + "\\") + name;
            _parent  = parentKey;

            _metadata = new QueryInfoKeyData();
            RefreshMetadata();
        }
Ejemplo n.º 16
0
        /// <summary>
        ///     Deletes this key, further operations will be invalid (except calls to <see cref="Close" />).
        /// </summary>
        public void Delete()
        {
            if (_parent == null)
            {
                throw new InvalidOperationException("Cannot delete the root key");
            }

            Win32Result result = OffregNative.DeleteKey(_intPtr, null);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            // Refresh parent
            _parent.RefreshMetadata();
        }
Ejemplo n.º 17
0
        /// <summary>
        ///     Internal helper to get the type and data for a specified value.
        /// </summary>
        /// <param name="name">The name of the value to retrieve data for.</param>
        /// <returns>The type and data for the specified value.</returns>
        internal Tuple <RegValueType, byte[]> GetValueInternal(string name)
        {
            RegValueType type;

            // Get the size first
            uint        size   = 0;
            Win32Result result = OffregNative.GetValue(_intPtr, null, name, out type, IntPtr.Zero, ref size);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            // Allocate buffer
            byte[] res     = new byte[size];
            IntPtr dataPtr = IntPtr.Zero;

            try
            {
                dataPtr = Marshal.AllocHGlobal((int)size);

                // Get data
                result = OffregNative.GetValue(_intPtr, null, name, out type, dataPtr, ref size);

                if (result != Win32Result.ERROR_SUCCESS)
                {
                    throw new Win32Exception((int)result);
                }

                // Copy data
                Marshal.Copy(dataPtr, res, 0, (int)size);
            }
            finally
            {
                // Release data
                if (dataPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(dataPtr);
                }
            }

            return(new Tuple <RegValueType, byte[]>(type, res));
        }
Ejemplo n.º 18
0
        /// <summary>
        ///     Detect if a subkey exists in this key.
        /// </summary>
        /// <param name="name">The subkey to find</param>
        /// <returns>True if it exists, false otherwise.</returns>
        public bool SubkeyExist(string name)
        {
            IntPtr intPtr = IntPtr.Zero;

            try
            {
                Win32Result result = OffregNative.OpenKey(_intPtr, name, out intPtr);

                return(result == Win32Result.ERROR_SUCCESS);
            }
            finally
            {
                // Close up shop
                if (intPtr != IntPtr.Zero)
                {
                    OffregNative.CloseKey(intPtr);
                }
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        ///     Creates a new subkey (or opens an existing one).
        /// </summary>
        /// <param name="name">The name of the subkey to create (or open).</param>
        /// <param name="options">Key creation options.</param>
        /// <returns>The newly created (or opened) key.</returns>
        public OffregKey CreateSubKey(string name, RegOption options = 0)
        {
            IntPtr         newKeyPtr;
            KeyDisposition disposition;
            Win32Result    result = OffregNative.CreateKey(_intPtr, name, null, options, IntPtr.Zero, out newKeyPtr,
                                                           out disposition);

            if (result != Win32Result.ERROR_SUCCESS)
            {
                throw new Win32Exception((int)result);
            }

            // Return new key
            OffregKey newKey = new OffregKey(this, newKeyPtr, name);

            RefreshMetadata();

            return(newKey);
        }
Ejemplo n.º 20
0
        /// <summary>
        ///     Enumerates all subkeys, only retrieving their names.
        /// </summary>
        /// <returns>Names of all the subkeys.</returns>
        public string[] GetSubKeyNames()
        {
            string[] results = new string[_metadata.SubKeysCount];

            for (uint item = 0; item < _metadata.SubKeysCount; item++)
            {
                uint sizeName = _metadata.MaxSubKeyLen + 1;

                StringBuilder sbName = new StringBuilder((int)sizeName);
                Win32Result   result = OffregNative.EnumKey(_intPtr, item, sbName, ref sizeName, null, IntPtr.Zero,
                                                            IntPtr.Zero);

                if (result != Win32Result.ERROR_SUCCESS)
                {
                    throw new Win32Exception((int)result);
                }

                results[item] = sbName.ToString();
            }

            return(results);
        }