/// <summary> /// Validates an array of paths before applying /// </summary> /// <param name="pathInfos">The array of paths</param> /// <param name="allowChanges">true to allow changes and reordering of the provided paths, otherwise false</param> /// <returns>true if the provided paths are valid, otherwise false</returns> public static bool ValidatePathInfos(IEnumerable <PathInfo> pathInfos, bool allowChanges = true) { DisplayConfigModeInfo[] displayConfigModeInfos; var displayConfigPathInfos = GetDisplayConfigPathInfos(pathInfos, out displayConfigModeInfos); if (displayConfigPathInfos.Length <= 0) { return(false); } var flags = displayConfigModeInfos.Length == 0 ? SetDisplayConfigFlags.TopologySupplied : SetDisplayConfigFlags.UseSuppliedDisplayConfig; if (allowChanges) { flags |= displayConfigModeInfos.Length == 0 ? SetDisplayConfigFlags.AllowPathOrderChanges : SetDisplayConfigFlags.AllowChanges; } return (DisplayConfigApi.SetDisplayConfig( (uint)displayConfigPathInfos.Length, displayConfigPathInfos, (uint)displayConfigModeInfos.Length, displayConfigModeInfos.Length > 0 ? displayConfigModeInfos : null, SetDisplayConfigFlags.Validate | flags) == Win32Status.Success); }
/// <summary> /// Validates a topology before applying /// </summary> /// <param name="topology">The topology identification</param> /// <returns>true if topology is applicable, otherwise false</returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static bool ValidateTopology(DisplayConfigTopologyId topology) { if (topology == DisplayConfigTopologyId.None) { throw new ArgumentOutOfRangeException(nameof(topology), "Topology should not be empty."); } var flags = (SetDisplayConfigFlags)topology; return(DisplayConfigApi.SetDisplayConfig(0, null, 0, null, SetDisplayConfigFlags.Validate | flags) == Win32Status.Success); }
/// <summary> /// Applies an array of paths /// </summary> /// <param name="pathInfos">The array of paths</param> /// <param name="allowChanges">true to allow changes and reordering of the provided paths, otherwise false</param> /// <param name="saveToDatabase">true to save the paths to the persistence database if call succeed, otherwise false</param> /// <param name="forceModeEnumeration">true to force driver mode enumeration before applying the paths</param> /// <exception cref="PathChangeException">Error in changing paths</exception> public static void ApplyPathInfos(IEnumerable <PathInfo> pathInfos, bool allowChanges = true, bool saveToDatabase = false, bool forceModeEnumeration = false) { var pathInfosArray = pathInfos.ToArray(); if (!ValidatePathInfos(pathInfosArray, allowChanges)) { throw new PathChangeException("Invalid paths information."); } DisplayConfigModeInfo[] displayConfigModeInfos; var displayConfigPathInfos = GetDisplayConfigPathInfos(pathInfosArray, out displayConfigModeInfos); if (displayConfigPathInfos.Length <= 0) { return; } var flags = displayConfigModeInfos.Length == 0 ? SetDisplayConfigFlags.TopologySupplied : SetDisplayConfigFlags.UseSuppliedDisplayConfig; if (allowChanges) { flags |= displayConfigModeInfos.Length == 0 ? SetDisplayConfigFlags.AllowPathOrderChanges : SetDisplayConfigFlags.AllowChanges; } else if (displayConfigModeInfos.Length > 0) { flags |= SetDisplayConfigFlags.NoOptimization; } if (saveToDatabase && (displayConfigModeInfos.Length > 0)) { flags |= SetDisplayConfigFlags.SaveToDatabase; } if (forceModeEnumeration && (displayConfigModeInfos.Length > 0)) { flags |= SetDisplayConfigFlags.ForceModeEnumeration; } var result = DisplayConfigApi.SetDisplayConfig( (uint)displayConfigPathInfos.Length, displayConfigPathInfos, (uint)displayConfigModeInfos.Length, displayConfigModeInfos.Length > 0 ? displayConfigModeInfos : null, SetDisplayConfigFlags.Apply | flags); if (result != Win32Status.Success) { throw new PathChangeException("An error occurred while applying the paths information.", new Win32Exception((int)result)); } }
/// <summary> /// Applies a saved topology /// </summary> /// <param name="topology">The topology identification to apply</param> /// <param name="allowPersistence">true to allows persistence of the changes, otherwise false</param> /// <exception cref="PathChangeException">Error in changing paths</exception> public static void ApplyTopology(DisplayConfigTopologyId topology, bool allowPersistence = false) { if (!ValidateTopology(topology)) { throw new PathChangeException("Invalid topology request."); } var flags = (SetDisplayConfigFlags)topology; if (allowPersistence) { flags |= SetDisplayConfigFlags.PathPersistIfRequired; } var result = DisplayConfigApi.SetDisplayConfig(0, null, 0, null, SetDisplayConfigFlags.Apply | flags); if (result != Win32Status.Success) { throw new PathChangeException("An error occurred while applying the requested topology.", new Win32Exception((int)result)); } }