nvidiascreen-resolutionnvapi

Enable resolutions not exposed by display programmatically on NVIDIA GPUs


I'm working on a solution where there is a need to set a custom resolution for a particular connected displays on a set of systems. What I have now works fine, but only as long as the "Enable resolutions not exposed by the display" option has been checked manually through the NVIDIA Control Panel (found under Display -> Change resolution > Customize... > Enable resolutions not exposed by the display).

Is there a way to enable this option programmatically, preferably through NVIDIA's core SDK - NVAPI.


Solution

  • Setting custom resolutions can be enabled through the ChangeDisplaySettingsEx function, exposed by the Windows API, by passing in CDS_ENABLE_UNSAFE_MODES as the fourth parameter dwflags. (To disable, use CDS_DISABLE_UNSAFE_MODES.)

    Code extract exemplifying usage:

    DWORD deviceIndex = 0;
    DISPLAY_DEVICE displayDevice = { 0 };
    displayDevice.cb = sizeof(DISPLAY_DEVICE);
    
    while (EnumDisplayDevices(NULL, deviceIndex, &displayDevice, 0)) {
        deviceIndex++;
    
        DEVMODE deviceMode = { 0 };
        deviceMode.dmSize = sizeof(DEVMODE);
    
        if (!EnumDisplaySettings(displayDevice.DeviceName, ENUM_CURRENT_SETTINGS, &deviceMode))
            continue;
    
        auto result = ChangeDisplaySettingsEx(displayDevice.DeviceName, &deviceMode, NULL, CDS_ENABLE_UNSAFE_MODES, NULL);
        if (result != DISP_CHANGE_SUCCESSFUL) {
            // Handle failure here...
        }
    }
    

    Note that this will enable unsafe graphics modes for all display devices.