Project ActivityMonitoring
From Frederick Chapleau Wiki
Contents |
Great, but... why?
In clear, just to know if I'm too much in front of my screens just like my mother was doing when I was 8.
The real question is more: In which circumstances that can be usefull? To be a Big Brother? Maybe, to automatically launch a program after a delay? Yes, more in that way.
The Solution
It's a Windows Service that is using some Windows API to calculate time elapsed since the last user interaction. After a pre-defined amount of time, it is calling a web service that is using a SQL Server database to store the actions.
By the way, a little hint: if you want to monitor a computer globally for inputs, just use the Windows Service via the Local System account. This will also monitor the RDP Connections.
The Code
Let's begin with the 20% code that is used 80% of the time.
The bigest challenges were setting or deducting the types. The MSDN documenation did not have the right types neither the right code to access the API.
Monitoring Code
[StructLayout(LayoutKind.Sequential)] public struct LASTINPUTINFO { public uint cbSize; public uint dwTime; }; [DllImport("USER32.DLL")] private static extern bool GetLastInputInfo(ref LASTINPUTINFO ii); public long GetIdleTime() { LASTINPUTINFO lastInPut = new LASTINPUTINFO(); lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut); if (GetLastInputInfo(ref lastInPut)) { long el = lastInPut.dwTime; long ui = (Environment.TickCount - el); // Overflow if (ui < 0) ui = ui + uint.MaxValue + 1; return ui; } else { throw new ApplicationException("Timespan"); } } public TimeSpan GetIdleTimeSpan() { return new TimeSpan(GetIdleTime() * 10000); }
RDP Sessions State
Check out the RoyalTS Source Code for great RDP Wrappers.
Session 0 Isolation
There is a problem with Windows Vista that introducte Session 0 isolation. With this "feature", you can monitor globally idle time, without spanning a process inside each session, that return the result.
As a workaround, my activity monitoring tiny client was refactored as a standard Windows UI, so that it can run on the current session.

