tags:

views:

2989

answers:

9

Does anyone know if there is an API to get the current monitor state (on or off) in Windows (XP/Vista/2000/2003)?

All of my searches seem to indicate there is no real way of doing this.

This thread tries to use GetDevicePowerState which according to Microsoft's docs does not work for display devices.

In Vista I can listen to GUID_MONITOR_POWER_ON but I do not seem to get events when the monitor is turned off manually.

In XP I can hook into WM_SYSCOMMAND SC_MONITORPOWER, looking for status 2. This only works for situations where the system triggers the power off.

The WMI Win32_DesktopMonitor class does not seem to help out as well.

Edit: Here is a discussion on comp.os.ms-windows.programmer.win32 indicating there is no reliable way of doing this.

Anyone else have any other ideas?

A: 

What will you be using this information for?

Have you considered the case where the monitor is on but is not connected to the display card?

Sijin
I don't follow how either of those questions are relevant, am I missing something?
Sam Saffron
His first question is relevant. What happens if the monitor is off and someone uses Remote Desktop or VNC to control the machine? What happens if the computer is connected to a KVM switch, Windows might detect this a a powered on monitor, but the KVM could be switched to a different computer.
Otherside
For remote desktop, Im not interested in mirroring devices etc... Jus tin physical monitor states. For KVMs, if they are cheating windows and pretending the monitor is on, then so be it ...
Sam Saffron
I think the 1st question is unnecessary and the 2nd question is ridiculous.I have 3 monitors near me right now that are not connected to this computer, why would I care about those ever when running an app on THIS box?
hunter
The first question is absolutely pertinent. It's one thing to say where can I find 50 feet of rope, but if you need the rope to swing across the road then maybe the answer is not to use rope but to go along to the bridge a little way down.
glenatron
+4  A: 

GetDevicePowerState sometimes works for monitors. If it's present, you can open the \\.\LCD device. Close it immediately after you've finished with it.

Essentially, you're out of luck -- there is no reliable way to detect the monitor power state, short of writing a device driver and filtering all of the power IRPs up and down the display driver chain. And that's not very reliable either.

Roger Lipscombe
I think this is the closest answer to the question. Its a real failure of the hardware mfgs for not giving us that level of comms in the DVI standard.
Sam Saffron
A: 

Of course it's possible. There are several standard methods

You can ask on Win32 api newsgroup : news://194.177.96.26/comp.os.ms-windows.programmer.win32 where it has often been discussed

+7  A: 

Before doing anything based on the monitor state, just remember that users can use a machine with remote desktop of other systems that don't require a monitor connected to the machine - so don't turn off any visualization based on the monitor state.

Nir
The windows API allow you to figure out if a monitor is "real" or not. See: DISPLAY_DEVICE_MIRRORING_DRIVER.
Sam Saffron
Remote desktop apps still affect the screen saver, don't they? (At least RealVNC does.) I know windows's own Remote Desktop sessions uses another system, so things might differ there.
Marcus Lindblom
+4  A: 

Look like all monitor power capabilities connected to the "power safe mode"
After searching i found here code that connecting between SC_MONITORPOWER message and system values (post number 2)
I use the code to testing if the system values is changing when i am manually switch off the monitor.

int main()
{
    for(;monitorOff()!=1;)
        Sleep(500);
    return 0;
}//main

And the code is never stopped, no matter how long i am switch off my monitor.
There the code of monitorOff function:

int monitorOff()
{
    const GUID MonitorClassGuid =
        {0x4d36e96e, 0xe325, 0x11ce, 
            {0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}};

    list<DevData> monitors;
    ListDeviceClassData(&MonitorClassGuid, monitors);

    list<DevData>::iterator it = monitors.begin(),
                            it_end = monitors.end();
    for (; it != it_end; ++it)
    {
        const char *off_msg = "";

        //it->PowerData.PD_PowerStateMapping
        if (it->PowerData.PD_MostRecentPowerState != PowerDeviceD0)
        {
            return 1;
        }
    }//for

    return 0;
}//monitorOff

Conclusion : when you manually switch of the the monitor, you cant catch it by windows (if there is no unusual driver interface for this), because all windows capabilities is connected to "power safe mode".

Avram
A: 

I don't see why you are so against telling us why you want to do that. There is a good chance that what you want to acomplish can be done in another fashion without the monitor involved at all.

For example, you can detect if the screen saver is active or if the workstation is locked.

HOW TO: Determine If Screen Saver Is Running

Detecting Windows/Workstation Locked / Unlocked in .NET

Jonathan Allen
What if I want to estimate the power usage of the monitor?
Sam Saffron
Then plug in a power meter between the wall and the monitor.
Jonathan Allen
really: http://www.1e.com/SoftwareProducts/NightWatchman/Features.aspx , sometimes when you are doing this for 10,000 computers hardware may not be an option and an estimate may be good enough (hardware metering is always going to be much more accurate)
Sam Saffron
+1  A: 

You could hook up a webcam, point it at your screen and do some analysis on the images you receive ;)

thijs
But then you may mistake reflections for screen action.. I think you're turning a device driver problem into an image processing problem.. I LIKE IT :)
cpatrick
This technique has been used to monitor racks full of equipment in remote locations. Point a camera at the rack, and watch for "unusual" changes in the indicator lights. Its brute force, but in some cases it is the only reasonable answer.
RBerteig
A: 

If this can be done, I'm not sure how you are going to solve the next problem of ensuring someone is actually looking at the monitor.

Philluminati
That does not really matter, Im just trying to estimate power usage
Sam Saffron
A: 

Hi,

In Windows XP or later you can use the IMSVidDevice Interface.

See http://msdn.microsoft.com/en-us/library/dd376775(VS.85).aspx

(not sure if this works in Sever 2003)

Fraser