5

I have used the code mentioned here to calculate the system Idle time through a window service. But in windows 7 & Vista I always get the LastInputInfo.dwTime as zero by which I cannot calculate the system idle time. I have given the code sample below.

 // If we have a value from the function
if (GetLastInputInfo(ref LastInputInfo))
{
    // Get the number of ticks at the point when the last activity was seen
    LastInputTicks = (int)LastInputInfo.dwTime;
    // Number of idle ticks = system uptime ticks - number of ticks at last input
    IdleTicks = systemUptime - LastInputTicks;
}

When I test the same code in a windows application I get the correct dwTime. I need to have the correct dwTime in my service to calculate correct idle time of the system.

Please help with some examples

Rob
  • 45,296
  • 24
  • 122
  • 150
deepu
  • 1,993
  • 6
  • 42
  • 64

1 Answers1

2

I dont think you will be able to use GetLastInputInfo in windows service on Windows 7. This is not about the code, It's about service hardening in Vista and later.

Read this: Services isolation in Session 0 of Windows Vista and Longhorn Server

HABJAN
  • 9,212
  • 3
  • 35
  • 59
  • Is there any way to bypass this.. making service run in user session – deepu Apr 01 '11 at 13:24
  • Well, you can eventually create standard windows application (that will start when windows starts) which will send idle time to your service via some IPC (inter process communication) mechanism. – HABJAN Apr 01 '11 at 13:27
  • Or maby you can help yourself with CPU idle time ( if you need that). Check out this article: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c39b416c-e959-41e2-a57e-c02f7b22cc1d – HABJAN Apr 01 '11 at 13:29
  • yeah i need the cpu idle time but how can i get in windows7 when theres seperate session for service and application – deepu Apr 01 '11 at 18:40