/// <summary> /// /// </summary> /// <param name="device"></param> /// <param name="propertyKey"></param> /// <param name="propertyDataType"></param> /// <param name="requiredSize"></param> /// <returns></returns> public static bool GetDeviceProperty(DeviceInfo device, DevicePropertyKey propertyKey, out DevicePropertyType propertyDataType, out int requiredSize) { var deviceInfoList = device.InfoSet.InfoSet; var deviceInfoData = device.InfoData; var devicePropertyKey = propertyKey.PropertyKey; var success = GetDeviceProperty(deviceInfoList, ref deviceInfoData, ref devicePropertyKey, out propertyDataType, IntPtr.Zero, 0, out requiredSize, 0); if (!success) { var lastError = ErrorHelpers.GetLastError(); return(lastError == ErrorCode.InsufficientBuffer); } return(true); }
/// <summary> /// /// </summary> /// <param name="device"></param> /// <param name="propertyKey"></param> /// <param name="propertyDataType"></param> /// <param name="propertyData"></param> /// <returns></returns> public static bool GetDeviceProperty(DeviceInfo device, DevicePropertyKey propertyKey, out DevicePropertyType propertyDataType, out Api.Buffer propertyData) { var deviceInfoList = device.InfoSet.InfoSet; var deviceInfoData = device.InfoData; var devicePropertyKey = propertyKey.PropertyKey; propertyData = new Api.Buffer(); int requiredSize; // Keep trying until we've got success or an unrecoverable error. while (true) { var success = GetDeviceProperty(deviceInfoList, ref deviceInfoData, ref devicePropertyKey, out propertyDataType, propertyData.Data, propertyData.Length, out requiredSize, 0); // 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); } }
/// <summary> /// Gets the structure that represents a <see cref="DevicePropertyKey"/> at the API level. /// </summary> /// <param name="propertyKey"> /// The <see cref="DevicePropertyKey"/> being referenced in an API call. /// </param> /// <returns>A <see cref="DEVPROPKEY"/> structure.</returns> public static DEVPROPKEY ToApiValue(this DevicePropertyKey propertyKey) { return(propertyKey.PropertyKey); }