Configs for SetDisplayConfig become invalid after computer restart

0

I'm using WINAPI to Enable/Disable my TV monitor by calling a method.

The method I found to work was saving the whole pathInfo and modeInfo arrays for both states
(Enabled/Disabled) and using them to restore to that state .

The method works properly but after restarting my computer, the settings become obsolete.
It works fine if I just restart the program, so the serialization is correct.

The goal was to Save/Serialize those settings ONCE on a computer, and be able to use them forever.

I've tried saving only the path and mode of the individual monitor that I'm interested on (by filtering the active displays only), but it turns out that the other screens' modes are affected as well when there is an extra display in the setup.

The main method is the method below, but you can find the whole class here!
(I'm using the User32 PInvoke lib on top of that)

    public static void ChangeTVState(bool Update = false, bool Save = false) {
        uint numPathArrayElements = 0;
        uint numModeInfoArrayElements = 0;
        uint id = QDC_ALL_PATHS;

        int bufferError = GetDisplayConfigBufferSizes(id, ref numPathArrayElements, ref numModeInfoArrayElements);
        DISPLAYCONFIG_PATH_INFO[] pathArray = new DISPLAYCONFIG_PATH_INFO[numPathArrayElements];
        DISPLAYCONFIG_MODE_INFO[] modeArray = new DISPLAYCONFIG_MODE_INFO[numModeInfoArrayElements];

        QueryDisplayConfig(id, ref numPathArrayElements, pathArray, ref numModeInfoArrayElements, modeArray, IntPtr.Zero);

        var active_modeArray = modeArray.Where(x => x.targetMode.targetVideoSignalInfo.activeSize.cx != 0).ToArray();
        var active_pathArray = pathArray.Where(x => x.flags != 0).ToArray();

        bool ThirdScreenIsConnected = active_pathArray.Length >= 3 && active_modeArray.Length >= 3;

        if (Save) {
            if (ThirdScreenIsConnected) {
                Disabled = new TVSettings() {
                    Path = pathArray,
                    Mode = modeArray
                };
            }
            else {
                Enabled = new TVSettings() {
                    Path = pathArray,
                    Mode = modeArray
                };
            }
        }

        if (Update) {
            if (Enabled == null || Disabled == null) {
                Console.WriteLine("Enabled & Disabled Settings are not configured properly.");
                Console.WriteLine("Please save both and try again.");
                return;
            }

            var Settings = ThirdScreenIsConnected ? Enabled : Disabled;
            pathArray = Settings.Path;
            modeArray = Settings.Mode;

            int f = SetDisplayConfig((uint)pathArray.Length, pathArray, (uint)modeArray.Length, modeArray, (SDC_APPLY | SDC_SAVE_TO_DATABASE | SDC_ALLOW_CHANGES | SDC_USE_SUPPLIED_DISPLAY_CONFIG));

            if (f == 0) { Console.WriteLine("Successfully updated Screen setup!"); }
            else { Console.WriteLine("SetDisplayConfig ERROR: " + f); }
        }

    }


I would expect the settings to be valid on multiple windows sessions, but they are being deemed invalid instead.

The error that appears after restarting is : 87 -- INVALID PARAMETERS, which also appeared when I attempted to alter the individual settings of the monitor (in pathArray and modeArray).

If you care enough to try this on your machine, here's some usable Save/Load functionality, together with a simple context menu for WPF

(You have to exit via the "Exit" button on the context menu for the serialization to occur)
(ps: you have to save twice -- once with 2nd screen Enabled and once with it Disabled)

Any help would be appreciated! :)

c#
windows
winapi
pinvoke
multiple-monitors
asked on Stack Overflow Jul 18, 2019 by Lyrcxis • edited Jul 18, 2019 by Lyrcxis

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0