1

I'm trying to check if monitor is connected to the power or not. I'm trying that code but even if I try on different machine, it always return me that monitor is off.

What could be the problem?

This is what i try:

#include <iostream>
#include <Windows.h>

int main(int argc, char *argv[])
{
    POINT p = { 0, 0 };
    HMONITOR monitor = MonitorFromPoint(p, MONITOR_DEFAULTTONULL);

    if (monitor == NULL) 
        std::cout << "No monitor found for point (0, 0)\n";
    else {
        BOOL result;
        if(GetDevicePowerState(monitor,&result)){
            std::cout << "Monitor is on!: \n";
        }else{
            std::cout << "Monitor is off!: \n";
        }
    }
}
user247702
  • 23,641
  • 15
  • 110
  • 157
Jayyrus
  • 12,961
  • 41
  • 132
  • 214

1 Answers1

1

You are not calling the function correctly. You are passing the address of a BOOL variable as the second parameter, but also assigning the function return value to that variable. The pfOn parameter is not the same thing as the function return value

You need to modify the code to separate those two distinct values.

if (GetDevicePowerState(monitor,&result))
{
    ... do something with result
}
else
{
    ... the call to GetDevicePowerState failed, result is ill-defined
}

By similar token, you are not checking the return value of GetMonitorInfo.

Now, once we fix this, the code might look like this:

#include <iostream>
#include <Windows.h>

int main(int argc, char *argv[])
{
    POINT p = { 0, 0 };
    HMONITOR monitor = MonitorFromPoint(p, MONITOR_DEFAULTTONULL);

    if (monitor == NULL) 
        std::cout << "No monitor found for point (0, 0)\n";
    else {

        MONITORINFOEX info;
        BOOL result;
        if (GetDevicePowerState(monitor,&result))
        {
            std::cout << "Result = " << result << "\n";
            if(result==TRUE){
                std::cout << "Monitor is on!: \n";
            }else{
                std::cout << "Monitor is off!: \n";
            }
        }
        else
        {
            std::cout << "Call to GetDevicePowerState failed\n";
        }

        if (GetMonitorInfo(monitor, &info))
        {
            std::cout << "Monitor: " << info.szDevice << "\n";
        }
        else
        {
            std::cout << "Call to GetMonitorInfo failed\n";
        }
    }
}

And when I run this program, the output is:

Call to GetDevicePowerState failed
Call to GetMonitorInfo failed

So, why does the call to GetDevicePowerState fail? Well, because that function does not accept an HMONITOR. It accepts a handle to a device and an HMONITOR does not meet the requirement.

And why does the call to GetMonitorInfo fail? Well, you failed to do what the documentation told you to do. It says:

You must set the cbSize member of the structure to sizeof(MONITORINFO) or sizeof(MONITORINFOEX) before calling the GetMonitorInfo function.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • sorry but it continue giving me the same result! :( – Jayyrus Nov 18 '13 at 12:31
  • Read my latest update. Step 1 has to be to learn how to call the functions properly and perform error checking. – David Heffernan Nov 18 '13 at 12:34
  • ok, so i can't use GetDevicePowerState to check if monitor is connected to power.. how can i solve? – Jayyrus Nov 18 '13 at 12:36
  • 1
    That's a different question, which has been asked before (http://stackoverflow.com/questions/203355/is-there-any-way-to-detect-the-monitor-state-in-windows-on-or-off). I think I answered the question you asked here. – David Heffernan Nov 18 '13 at 12:37