예제 #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);
        }
예제 #2
0
        /// <summary>
        ///     Enumerates all vaues, only retrieving their names.
        /// </summary>
        /// <returns>Names of all the values.</returns>
        public string[] GetValueNames()
        {
            string[] results = new string[_metadata.ValuesCount];

            for (uint item = 0; item < _metadata.ValuesCount; item++)
            {
                uint sizeName = _metadata.MaxValueNameLen + 1;

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

                Win32Result result = OffregNative.EnumValue(_intPtr, item, sbName, ref sizeName, IntPtr.Zero,
                                                            IntPtr.Zero,
                                                            IntPtr.Zero);

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

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

            return(results);
        }