public static void PatchDisplayConfig(DisplayConfiguration config)
        {
            var lookupConfig = QueryDisplayConfig(QueryDisplayFlags.AllPaths);
            var idCache      = new Dictionary <uint, LUID>();

            for (int i = 0; i < config.DisplayPaths.Length; i++)
            {
                uint id;
                LUID adapterId;

                id        = config.DisplayPaths[i].SourceInfo.Id;
                adapterId = config.DisplayPaths[i].SourceInfo.AdapterId;
                config.DisplayPaths[i].SourceInfo.AdapterId = LookupAdapterId(lookupConfig, idCache, id, adapterId);

                id        = config.DisplayPaths[i].TargetInfo.Id;
                adapterId = config.DisplayPaths[i].TargetInfo.AdapterId;
                config.DisplayPaths[i].TargetInfo.AdapterId = LookupAdapterId(lookupConfig, idCache, id, adapterId);
            }
            for (int i = 0; i < config.DisplayModes.Length; i++)
            {
                uint id;
                LUID adapterId;

                id        = config.DisplayModes[i].Id;
                adapterId = config.DisplayModes[i].AdapterId;
                config.DisplayModes[i].AdapterId = LookupAdapterId(lookupConfig, idCache, id, adapterId);
            }
        }
        private static LUID?FindAdapterId(DisplayConfiguration config, uint id)
        {
            var matchingTargetPath = config.DisplayPaths
                                     .Cast <DisplayConfigPathInfo?>()
                                     .FirstOrDefault(p => p.Value.TargetInfo.Id == id);

            if (matchingTargetPath.HasValue)
            {
                var adapterId = matchingTargetPath.Value.TargetInfo.AdapterId;
                Debug.WriteLine($"TARGET PATH MATCH: Id {id} -> AdapterId {adapterId}");
                return(adapterId);
            }
            var matchingSourcePath = config.DisplayPaths
                                     .Cast <DisplayConfigPathInfo?>()
                                     .FirstOrDefault(p => p.Value.SourceInfo.Id == id);

            if (matchingSourcePath.HasValue)
            {
                var adapterId = matchingSourcePath.Value.SourceInfo.AdapterId;
                Debug.WriteLine($"SOURCE PATH MATCH: Id {id} -> AdapterId {adapterId}");
                return(adapterId);
            }
            Debug.WriteLine($"No MATCH: Id {id}");
            return(null);
        }
 private static LUID LookupAdapterId(DisplayConfiguration config, Dictionary <uint, LUID> idCache, uint id, LUID oldAdapterId)
 {
     if (idCache.TryGetValue(id, out LUID adapterId))
     {
         return(adapterId);
     }
     adapterId   = FindAdapterId(config, id) ?? FindAdapterId(config, 0) ?? oldAdapterId;
     idCache[id] = adapterId;
     return(adapterId);
 }
        public static bool SetDisplayConfig(DisplayConfiguration config, bool persistent = false)
        {
            var flags = SdcFlags.Apply | SdcFlags.UseSuppliedDisplayConfig;

            if (persistent)
            {
                flags |= SdcFlags.SaveToDatabase;
            }
            var errorCode = User32.SetDisplayConfig(
                (uint)config.DisplayPaths.Length, config.DisplayPaths,
                (uint)config.DisplayModes.Length, config.DisplayModes,
                flags);

            if (errorCode != 0)
            {
                Console.Error.WriteLine("SetDisplayConfig Error: " + errorCode);
            }
            return(errorCode == 0);
        }