From: lukkycharm1 on
Hi everyone:


I've been directed to this thread from the win32.programmer.kernel
with the following question:

Is there a way to tell if a computer monitor is off (from a program)?


This looks like a trivial task, but I can't seem to find a documented
solution from MS. I tried Win32_DesktopMonitor WMI class, which
returned the same (wrong) results under Win XP, and now someone
suggested me to refer to undocumented APIs. At this point I reached a
deadend, so any help would be greatly appreciated.
From: Sebastian G. on
lukkycharm1(a)gmail.com wrote:


> I've been directed to this thread from the win32.programmer.kernel
> with the following question:
>
> Is there a way to tell if a computer monitor is off (from a program)?


Unless the power button becomes part of the data that can be queried via
DDI, the answer is no.
From: Christian ASTOR on
lukkycharm1(a)gmail.com wrote:

> Is there a way to tell if a computer monitor is off (from a program)?

If you're talking about monitor power :
On Vista, normally WM_POWERBROADCAST.
Otherwise, there is NtGetDevicePowerState(), which works for me (tested
on XP SP2) - PowerDeviceD0 : On, PowerDeviceD3 : Off, but may not work
depending on the video driver.
Another simple method is to compare SPI_GETPOWEROFFTIMEOUT (if ACTIVE)
with the current idle time (GetLastInputInfo())
From: lukkycharm1 on
On Apr 13, 12:51 pm, Christian ASTOR <casto...(a)club-internet.fr>
wrote:
> lukkycha...(a)gmail.com wrote:
> > Is there a way to tell if a computer monitor is off (from a program)?
>
> If you're talking about monitor power :
> On Vista, normally WM_POWERBROADCAST.
> Otherwise, there is NtGetDevicePowerState(), which works for me (tested
> on XP SP2) - PowerDeviceD0 : On, PowerDeviceD3 : Off, but may not work
> depending on the video driver.
> Another simple method is to compare SPI_GETPOWEROFFTIMEOUT (if ACTIVE)
> with the current idle time (GetLastInputInfo())


Thanks a lot, Christian. I see your point with SPI_GETPOWEROFFTIMEOUT
and GetLastInputInfo, but I'd really like to try the
NtGetDevicePowerState() method. Unfortunately I'm not very good with
NT kernel mode programming and I can't find any info on it. Can you
please post a small sample of how you use it to get monitor power
state? I tried a more documented GetDevicePowerState but I can't get a
monitor handle for it.

Thanks in advance.
From: Christian ASTOR on
On 14 avr, 00:31, lukkycha...(a)gmail.com wrote:

> Thanks a lot, Christian. I see your point with SPI_GETPOWEROFFTIMEOUT and GetLastInputInfo, but I'd really like to try the NtGetDevicePowerState() method. Unfortunately I'm not very good with
> NT kernel mode programming and I can't find any info on it. Can you
> please post a small sample of how you use it to get monitor power
> state? I tried a more documented GetDevicePowerState but I can't get a monitor handle for it.

I test it like this =>

// ...
#include <ntsecapi.h> // for NTSTATUS
// ...
typedef NTSTATUS (WINAPI *NTGETDEVICEPOWERSTATE)(HANDLE hDevice,
PDEVICE_POWER_STATE pPowerState );
NTGETDEVICEPOWERSTATE pNtGetDevicePowerState;

// ...

HANDLE hDevice;
hDevice = CreateFile("\\\\.\\LCD", GENERIC_READ, FILE_SHARE_READ,NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
if (hDevice != INVALID_HANDLE_VALUE)
{
DEVICE_POWER_STATE PowerState = PowerDeviceUnspecified;
HMODULE hNTDLL = GetModuleHandle ("NTDLL.DLL");
if (hNTDLL)
{
pNtGetDevicePowerState =
(NTGETDEVICEPOWERSTATE)GetProcAddress(hNTDLL,
"NtGetDevicePowerState");
if (pNtGetDevicePowerState)
{
NTSTATUS nStatus = pNtGetDevicePowerState(hDevice, &PowerState);
if (nStatus == 0)
{
if (PowerState != PowerDeviceD0)
{
//....
}
}
}
FreeLibrary(hNTDLL);
}
}