3

So my Vista machine is set to turn off it's monitors after 10 minutes. (Note : machine is not suspended, or hibernated)

I have a .Net application running, that needs to "wake" the system at a specified time. As a human, you just move the mouse or press a key. How do I do this programatically?

I've tried : programatically setting the cursor; using "SendKeys"; and even pinvoking CreateWaitableTimer to unsuspend (even though it's not suspended) in the hope that that would trigger something.

Ideally code in c# would be great, but the correct Win API would suffice.

Many thanks in advance.

1 Answers1

3

You should be able to control monitor power by sending system command messages as follows. Note that this is tested on XP, Vista may have changed things somewhat so you'll need to test it and let us know.

This code is in VB but you can see the Win32 API call that it uses. You need to pass a window handle to the function so your code will need a window created to process the message (just pass it through to the default window processing function).

Const SC_MONITORPOWER As Integer = &HF170
Const WM_SYSCOMMAND As Short = &H112S
Private Function SendMessage(
    ByVal Handle As Int32,
    ByVal wMsg As Int32,
    ByVal wParam As Int32,
    ByVal lParam As Int32) As Int32
End Function
Sub MonStandBy(hWnd as Int32)
    SendMessage(hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 1)
End Sub
Sub MonOff(hWnd as Int32)
    SendMessage(hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 2)
End Sub
Sub MonOn(hWnd as Int32)
    SendMessage(hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, -1)
End Sub
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • from my tests it seems that a valid window handle was not mandatory. we can simply pass HWND_BROADCAST as hWnd. see this answer: http://stackoverflow.com/a/42393472/1468842 – Opmet Feb 22 '17 at 15:57
  • According to others, this won't work to wake up the display in Windows 7+. You might want to look at this answer instead: https://stackoverflow.com/a/14698416/999120 – Doin Jan 20 '19 at 09:47