/// <summary>
        ///
        /// </summary>
        /// <param name="device"></param>
        /// <param name="propertyKey"></param>
        /// <param name="propertyType"></param>
        /// <param name="requiredSize"></param>
        /// <returns></returns>
        public static bool GetDeviceRegistryProperty(DeviceInfo device, DeviceRegistryPropertyKey propertyKey,
                                                     out DeviceRegistryPropertyType propertyType, out int requiredSize)
        {
            var deviceInfoList = device.InfoSet.InfoSet;
            var deviceInfoData = device.InfoData;

            var success = GetDeviceRegistryProperty(deviceInfoList, ref deviceInfoData, propertyKey.PropertyCode,
                                                    out propertyType, IntPtr.Zero, 0, out requiredSize);

            if (!success)
            {
                var lastError = ErrorHelpers.GetLastError();
                return(lastError == ErrorCode.InsufficientBuffer);
            }

            return(true);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="device"></param>
        /// <param name="propertyKey"></param>
        /// <param name="propertyType"></param>
        /// <param name="propertyData"></param>
        /// <returns></returns>
        public static bool GetDeviceRegistryProperty(DeviceInfo device, DeviceRegistryPropertyKey propertyKey,
                                                     out DeviceRegistryPropertyType propertyType, out Api.Buffer propertyData)
        {
            var deviceInfoList = device.InfoSet.InfoSet;
            var deviceInfoData = device.InfoData;

            propertyData = new Api.Buffer();
            int requiredSize;

            // Keep trying until we've got success or an unrecoverable error.
            while (true)
            {
                var success = GetDeviceRegistryProperty(deviceInfoList, ref deviceInfoData, propertyKey.PropertyCode,
                                                        out propertyType, propertyData.Data, propertyData.Length, out requiredSize);

                // If the data was read successfully, truncate the buffer to match the length read.
                if (success)
                {
                    propertyData.Truncate(requiredSize);
                    return(true);
                }

                // If the last error was for anything except the buffer being too small, cleanly get rid of the buffer
                // before returning failure.
                var lastError = ErrorHelpers.GetLastError();
                if (lastError != ErrorCode.InsufficientBuffer)
                {
                    propertyData.Dispose();
                    propertyData = null;

                    return(false);
                }

                // Resize the buffer to the required length before trying again.
                propertyData.Resize(requiredSize);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Gets the value that represents a <see cref="DeviceRegistryPropertyKey"/> at the API level.
 /// </summary>
 /// <param name="propertyKey">
 /// The <see cref="DeviceRegistryPropertyKey"/> being referenced in an API call.
 /// </param>
 /// <returns>A <see cref="DeviceRegistryPropertyCode"/> value.</returns>
 public static DeviceRegistryPropertyCode ToApiValue(this DeviceRegistryPropertyKey propertyKey)
 {
     return(propertyKey.PropertyCode);
 }