1

I have a Windows service program (written using C++) that is required to perform an energy saving power operation at a certain time of the day. I need to find out if a user might be at the terminal at the time when the power operation is performed and if he/she is, postpone it then. So my question is, how do you know the moment of the last user activity from a Windows service (running as a local system)?

PS. By user activity I mean keyboard and mouse activity.

ahmd0
  • 16,633
  • 33
  • 137
  • 233

3 Answers3

1

Each user session will have to run a background app within its session that communicates with the service, then the app can report the last activity time so the service can make decisions based on that.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Yeah, that's what I thought. But there are two issues: 1. (Major one) Will that still be possible to do in Windows 8 and Metro interface when the desktop is just an app? 2. What if we have a Remote session, or in case a user hit Ctrl+Alt+Del and sees a logon screen -- I don't think one can run a background app in that session? – ahmd0 May 02 '12 at 00:17
0

Depending on how deterministic it has to be, you could use the task scheduler. It can trigger the task when the computer is idle, wait for a while for it to be, etc. You can add the task manually to begin, then use the API.

Task Scheduler power settings

ixe013
  • 9,559
  • 3
  • 46
  • 77
0

In a rare case of Microsoft actually providing the API function that you need, you can use GetLastInputInfo().

Greg Wittmeyer
  • 439
  • 4
  • 14
  • 1
    Unfortunately this doesn't work if called by a service because the service is running in a different session from the user. This method will always return 0 unless it's called from a console session. – JRiggles Apr 27 '23 at 16:23
  • JRiggles, you are correct. – Greg Wittmeyer Apr 27 '23 at 20:46