How to count the time a Windows users is logged in (VC++)
I am writing a Windows service for doing Parental Control (Visual C++).
I am having trouble with the following:- how to count the time a Windows user has been logged on today (time measured开发者_开发百科 in hours)
- how to trigger log-off from the user Windows account, when time per day exceeds a specified value, say 5 hours.
Any help is appreciated.
Regards!To do that I think there may me several ways.
I think the simplest one is to use a timer that could be launched when the service start (or when your application start if placed in the startup menu).
For that you can use the SetTimer method associated to a WM_TIMER message :
http://msdn.microsoft.com/en-us/library/ms644906(v=vs.85).aspx
You should get the time before the launching of the timer using a CTime function for example :
CTime t;
t.GetCurrentTime ();
timeAtLaunched=t.GetSecond(); //or GetHour()
After that you can put a similar CTime call inside your timer function to have a currentTime value and calculate a difference between the currentTime value and the timeAtLaunched value
To keep the connected time during a day if an user connect and disconnect several times, you should keep the difference value inside a file or inside the registry.
To force logoff, you can use :
ExitWindowsEx (EWX_SHUTDOWN | EWX_FORCE, SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
SHTDN_REASON_MINOR_UPGRADE | SHTDN_REASON_FLAG_PLANNED)
精彩评论