Why does EnumDisplayMonitors Still Return just Disconnected Monitor
I'm working om win32 Desktop application in CPP and I'm checking what happens if I try to fetch Display info with EnumDisplayMonitors when All displays are disconnected! Surprisingly it is giving display info of previously connected Display. Why is this so? Is it returning cached info?
Windows API - Win32
-
RLWA32 • 47,246 Reputation points
2025-02-11T08:33:43.86+00:00 If you use multiple monitors what does Device Manager show before disconnecting one of them and then after reconnecting it? Then try it again after scanning for hardware changes.
-
Harshithraj1871 • 1,581 Reputation points
2025-02-11T08:35:46.6166667+00:00 When we have multiple monitor the behaviours are fair, before disconnecting it will show 2 and after that it will show 1
-
RLWA32 • 47,246 Reputation points
2025-02-11T08:37:34.07+00:00 Is a manual scan for hardware changes needed to get the expected results?
-
Harshithraj1871 • 1,581 Reputation points
2025-02-11T08:40:45.1333333+00:00 Yes actually we want to do a manual scan before we bring up our window
-
RLWA32 • 47,246 Reputation points
2025-02-11T08:59:14.5166667+00:00 Maybe you could use WMI and the Win32_DesktopMonitor class to identify a disconnected monitor.
-
Castorix31 • 86,986 Reputation points
2025-02-11T09:12:33.2+00:00 Can't you use DISPLAY_DEVICE with DISPLAY_DEVICE_ACTIVE or DISPLAY_DEVICE_ATTACHED ?
-
Harshithraj1871 • 1,581 Reputation points
2025-02-11T09:22:13+00:00 @Castorix31 tried that with EnumDisplayDevices(), I have 1 Display connected, still it gave me 4 displays (1 active and 3 inactive).
Display Device 0: \\.\DISPLAY1 - Intel(R) UHD Graphics 770 Status: Inactive Primary Display: No Could not retrieve display settings. --------------------------------- Display Device 1: \\.\DISPLAY2 - Intel(R) UHD Graphics 770 Status: Active Primary Display: Yes Monitor 0: \\.\DISPLAY2\Monitor0 - Generic PnP Monitor Screen Resolution: 1920 x 1080 pixels Refresh Rate: 60 Hz --------------------------------- Display Device 2: \\.\DISPLAY3 - Intel(R) UHD Graphics 770 Status: Inactive Primary Display: No Could not retrieve display settings. --------------------------------- Display Device 3: \\.\DISPLAY4 - Intel(R) UHD Graphics 770 Status: Inactive Primary Display: No Could not retrieve display settings. ---------------------------------
And when i unplugged the my only monitor it still gave me 4 displays (1 active and 3 inactive).
Display Device 0: \\.\DISPLAY1 - Intel(R) UHD Graphics 770 Status: Active Primary Display: Yes Monitor 0: \\.\DISPLAY1\Monitor0 - Digital Flat Panel (640x480 60Hz) Screen Resolution: 1920 x 1080 pixels Refresh Rate: 60 Hz --------------------------------- Display Device 1: \\.\DISPLAY2 - Intel(R) UHD Graphics 770 Status: Inactive Primary Display: No Could not retrieve display settings. --------------------------------- Display Device 2: \\.\DISPLAY3 - Intel(R) UHD Graphics 770 Status: Inactive Primary Display: No Could not retrieve display settings. --------------------------------- Display Device 3: \\.\DISPLAY4 - Intel(R) UHD Graphics 770 Status: Inactive Primary Display: No Could not retrieve display settings. ---------------------------------
EDIT : after using DISPLAY_DEVICE_ATTACHED_TO_DESKTOP flag all the inactive enumeration can be skipped but when no display is connected - i will still get an Active display as in above output
-
RLWA32 • 47,246 Reputation points
2025-02-11T16:50:13.99+00:00 Yes actually we want to do a manual scan before we bring up our window
Some posts on the internet suggested using CM_Locate_DevNode with CM_LOCATE_DEVNODE_NORMAL and CM_Reenumerate_DevNode (requires Administrator privileges) to initiate a hardware scan.
-
Darran Rowe • 1,426 Reputation points
2025-02-11T19:40:31.53+00:00 Because that is an Intel(R) UHD Graphics adapter, is this a laptop? I'm wondering if there is something non obvious going on hardware wise, like extra hardware is being enabled when it detects an external display being plugged in.
-
Harshithraj1871 • 1,581 Reputation points
2025-02-12T06:16:25.6366667+00:00 @RLWA32 : Thanks I'm looking for some API which could run without Admin Mode
-
Harshithraj1871 • 1,581 Reputation points
2025-02-12T06:18:57.4933333+00:00 @Darran Rowe : No I'm not using Laptop. I'm using simple CPU-monitor Setup
-
Harshithraj1871 • 1,581 Reputation points
2025-02-12T09:09:17.0666667+00:00 Does windows keep any "virtual display" active even when all monitor is physically disconnected
-
Castorix31 • 86,986 Reputation points
2025-02-12T16:22:50.73+00:00 On my PC (1 monitor only), when I test both DISPLAY_DEVICE_ATTACHED && DISPLAY_DEVICE_ACTIVE, enumeration is skipped when I unplug it (Intel(R) HD Graphics and 247ELH Philips monitor)
-
Harshithraj1871 • 1,581 Reputation points
2025-02-19T05:34:41.5266667+00:00 @Castorix31 I tried that in this way
if (!(displayDevice.StateFlags & (DISPLAY_DEVICE_ATTACHED && DISPLAY_DEVICE_ACTIVE))) { deviceIndex++; continue; } if (!(displayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) { deviceIndex++; continue; }
Still I'm getting this output when no display is connected
Display Device 0: \\.\DISPLAY1 - Intel(R) UHD Graphics 770 Status: Active Primary Display: Yes Monitor 0: \\.\DISPLAY1\Monitor0 - Digital Flat Panel (640x480 60Hz) Screen Resolution: 1920 x 1080 pixels Refresh Rate: 60 Hz ---------------------------------
-
Harshithraj1871 • 1,581 Reputation points
2025-02-19T05:46:52.0166667+00:00 .....
-
Harshithraj1871 • 1,581 Reputation points
2025-02-19T05:53:23.73+00:00 @Castorix31 adding my code for better view
void GetMonitorInfoUsingDisplayDevice() { DISPLAY_DEVICE displayDevice; displayDevice.cb = sizeof(DISPLAY_DEVICE); int deviceIndex = 0; while (EnumDisplayDevices(NULL, deviceIndex, &displayDevice, 0)) { if (!(displayDevice.StateFlags & (DISPLAY_DEVICE_ATTACHED && DISPLAY_DEVICE_ACTIVE))) { deviceIndex++; continue; } if (!(displayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) { deviceIndex++; continue; } std::wstringstream debugMessage; debugMessage << L"Display Device " << deviceIndex << L": " << displayDevice.DeviceName << L" - " << displayDevice.DeviceString << L"\n"; // Check if the display is active if (displayDevice.StateFlags & DISPLAY_DEVICE_ACTIVE) { debugMessage << L" Status: Active\n"; } else { debugMessage << L" Status: Inactive\n"; } // Check if it's the primary display if (displayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) { debugMessage << L" Primary Display: Yes\n"; } else { debugMessage << L" Primary Display: No\n"; } // Now, enumerate the monitor attached to this display device DISPLAY_DEVICE monitorDevice; monitorDevice.cb = sizeof(DISPLAY_DEVICE); int monitorIndex = 0; while (EnumDisplayDevices(displayDevice.DeviceName, monitorIndex, &monitorDevice, 0)) { debugMessage << L" Monitor " << monitorIndex << L": " << monitorDevice.DeviceName << L" - " << monitorDevice.DeviceString << L"\n"; monitorIndex++; } // Retrieve screen resolution using DEVMODE DEVMODE devMode; devMode.dmSize = sizeof(DEVMODE); if (EnumDisplaySettings(displayDevice.DeviceName, ENUM_CURRENT_SETTINGS, &devMode)) { debugMessage << L" Screen Resolution: " << devMode.dmPelsWidth << L" x " << devMode.dmPelsHeight << L" pixels\n"; debugMessage << L" Refresh Rate: " << devMode.dmDisplayFrequency << L" Hz\n"; } else { debugMessage << L" Could not retrieve display settings.\n"; } debugMessage << L"---------------------------------\n"; // Send output to Visual Studio Debug window LogToDebugWindow(debugMessage.str()); deviceIndex++; }
-
Roy Li - MSFT • 34,006 Reputation points • Microsoft Vendor
2025-02-19T06:10:16.74+00:00 Hope you've been well. Any updates about this issue?
-
Harshithraj1871 • 1,581 Reputation points
2025-02-19T06:56:05.5366667+00:00 @Roy Li - MSFT I have added few comments, but this page is not showing that, looks like a bug , this happened many time.
Sign in to comment