Query monitor presence

Started by
2 comments, last by Sandman 19 years, 6 months ago
Lo ppl :) Hoping you can help me out here. Is there any way to check if a monitor is present (ie. connected)? I was thinking of ops such as changing res, but that is very invasive. Possibly set up an OGL/DX context and performing some checks for support of a common pixel format on a specific output? The program also needs to notice if the monitor is unplugged, however as long as queries are reliable (and not system intensive), they could be performed on a regular basis. Thanks for any help :)
Advertisement
I put together a sample program illustrating how to use the GDI display device enumeration functions to check this:

Note the EnumDisplaySettings(...) function call; when you pass ENUM_CURRENT_SETTINGS it retrieves the current state. If it returns TRUE the device is initialized, and has a graphics mode.
#include <windows.h>#include <wingdi.h>#include <stdio.h>int main(int argc, char **argv){	DISPLAY_DEVICE dd;	int dev = 0;	dd.cb = sizeof(DISPLAY_DEVICE);	printf("Enumerating display devices:\n");	while(EnumDisplayDevices(NULL, dev, &dd, 0))	{		DEVMODE dm;		dd.cb = sizeof(DISPLAY_DEVICE);		dm.dmSize = sizeof(DEVMODE);		printf("\nDevice  : %s \t {%s}\n", dd.DeviceName, dd.DeviceString);		printf("Context : %s\n", dd.DeviceID);		if (dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) printf("Part of Desktop\n");		if (dd.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) printf("Pseudo device for monitoring\n");		if (dd.StateFlags & DISPLAY_DEVICE_MODESPRUNED) printf("Multiple Display Modes\n");		if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) printf("Primary Display Adapter\n");		if (dd.StateFlags & DISPLAY_DEVICE_REMOVABLE) printf("Removable Device (?!?)\n");		if (dd.StateFlags & DISPLAY_DEVICE_VGA_COMPATIBLE) printf("VGA Compatible\n");				if (EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dm))		{			printf("Status\n");			printf("Device      : %s\n", dm.dmDeviceName);			printf("Bits per Pel: %d\n", dm.dmBitsPerPel);			printf("Pel Width   : %d\n", dm.dmPelsWidth);			printf("Pel Height  : %d\n", dm.dmPelsHeight);			printf("Frequency   : %d\n", dm.dmDisplayFrequency);		}		dev++;	}	if (dev == 0) {		printf("\nApparently there are no devices connected...");	}}


Here is sample output with my system:

Enumerating display devices:Device  : \\.\DISPLAY1   {3dfx Voodoo3}Context : PCI\VEN_121A&DEV_0005&SUBSYS_0036121A&REV_01Part of DesktopStatusDevice      : 3dfxvsBits per Pel: 32Pel Width   : 1024Pel Height  : 768Frequency   : 75Device  : \\.\DISPLAY2   {RADEON 7500 SERIES  }Context : PCI\VEN_1002&DEV_5157&SUBSYS_0F2A1787&REV_00Part of DesktopMultiple Display ModesPrimary Display AdapterStatusDevice      : ATI2DVAGBits per Pel: 32Pel Width   : 1280Pel Height  : 1024Frequency   : 75Device  : \\.\DISPLAY3   {RADEON 7500 SERIES  }Context : PCI\VEN_1002&DEV_5157&SUBSYS_0F2A1787&REV_00Removable Device (?!?)Device  : \\.\DISPLAYV1          {NetMeeting driver}Context :Pseudo device for monitoringDevice  : \\.\DISPLAYV2          {RDPDD Chained DD}Context :Pseudo device for monitoring


And again after I plugged in and configured the adapter for the second port on the RADEON card...

Enumerating display devices:Device  : \\.\DISPLAY1   {3dfx Voodoo3}Context : PCI\VEN_121A&DEV_0005&SUBSYS_0036121A&REV_01Part of DesktopStatusDevice      : 3dfxvsBits per Pel: 32Pel Width   : 1024Pel Height  : 768Frequency   : 75Device  : \\.\DISPLAY2   {RADEON 7500 SERIES  }Context : PCI\VEN_1002&DEV_5157&SUBSYS_0F2A1787&REV_00Part of DesktopMultiple Display ModesPrimary Display AdapterStatusDevice      : ATI2DVAGBits per Pel: 32Pel Width   : 1280Pel Height  : 1024Frequency   : 75Device  : \\.\DISPLAY3   {RADEON 7500 SERIES  }Context : PCI\VEN_1002&DEV_5157&SUBSYS_0F2A1787&REV_00Removable Device (?!?)Device  : \\.\DISPLAYV1          {NetMeeting driver}Context :Pseudo device for monitoringDevice  : \\.\DISPLAYV2          {RDPDD Chained DD}Context :Pseudo device for monitoring
Thanks for the code :) I'm sure I'll use it in the future, but its not as responsive as I need. If I physically unplug a device while the PC is running, it still appears as initialised and ready to use if I run the program again :(
Funny you should ask this, I was just looking at this article, which I think should help you do what you want.

This topic is closed to new replies.

Advertisement