admin管理员组文章数量:1295117
I'm working on a Win32 desktop application in C++ and testing what happens when I retrieve display information using DISPLAY_DEVICE after disconnecting all physical displays. Surprisingly, the API still reports an active display even when no monitors are physically connected.
Sample code
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_TO_DESKTOP)) {
deviceIndex++;
continue; // Skip displays that are not part of the desktop
}
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++;
}
}
This is the output when I used DISPLAY_DEVICE with EnumDisplayDevices(), where I have 1 physical monitor connected
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
When I removed all the physical monitors, This API is still telling me that I have an active Display 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
Why is this so? Does windows keep any "virtual display" active even when all monitor is physically disconnected?
I'm working on a Win32 desktop application in C++ and testing what happens when I retrieve display information using DISPLAY_DEVICE after disconnecting all physical displays. Surprisingly, the API still reports an active display even when no monitors are physically connected.
Sample code
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_TO_DESKTOP)) {
deviceIndex++;
continue; // Skip displays that are not part of the desktop
}
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++;
}
}
This is the output when I used DISPLAY_DEVICE with EnumDisplayDevices(), where I have 1 physical monitor connected
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
When I removed all the physical monitors, This API is still telling me that I have an active Display 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
Why is this so? Does windows keep any "virtual display" active even when all monitor is physically disconnected?
Share Improve this question edited Feb 12 at 9:36 Thomas Weller 59.6k23 gold badges137 silver badges253 bronze badges asked Feb 12 at 9:20 HarshithHarshith 3203 silver badges12 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1You might want to test DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
and/or DISPLAY_DEVICE_ACTIVE
on the DISPLAY_DEVICE
of the monitor, not the graphics device as you're currently doing (i.e. with monitorDevice
)
本文标签:
版权声明:本文标题:c++ - Why does DISPLAY_DEVICE return me a Valid display details when no physical monitors are connected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741612421a2388336.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
EnumDisplayMonitors
andEnumDisplayDevices
. Which one is your question about? Your mention ofDISPLAY_DEVICE
suggests the latter. – Mark Jansen Commented Feb 12 at 9:30StateFlags
can contain a flagDISPLAY_DEVICE_MIRRORING_DRIVER
, you did not check this flag. Windows can return devices that are virtual. – Mark Jansen Commented Feb 12 at 9:59displayDevice.DeviceKey
field, that will point you to a registry key underHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Video` followed by a guid. Inspecting that key might yield some information about the type of device it is. For example, my system has
{67D6A1EB-FBC2-11EE-A045-DC46286180E5}\Video` that contains aDeviceDesc
that says:@rdpidd.inf,%rdpidd.devicedesc%;Microsoft Remote Display Adapter
, indicating this is a display adapter for remote desktop. – Mark Jansen Commented Feb 12 at 14:34