Example #1
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);
        }
Example #2
0
        /// <summary>
        ///     Attempt to read the data for a value, and parse it.
        /// </summary>
        /// <param name="name">The name of the value</param>
        /// <param name="data">The parsed data, or byte[] if parsing failed.</param>
        /// <returns>True for success, false otherwise. If the result is false, the data is always a byte[].</returns>
        public bool TryParseValue(string name, out object data)
        {
            Tuple <RegValueType, byte[]> internalData = GetValueInternal(name);

            if (!Enum.IsDefined(typeof(RegValueType), internalData.Item1))
            {
                WarnDebugForValueType(name, internalData.Item1);
                internalData = new Tuple <RegValueType, byte[]>(RegValueType.REG_BINARY, internalData.Item2);
            }

            return(OffregHelper.TryConvertValueDataToObject(internalData.Item1, internalData.Item2, out data));
        }