Tell if Win2000 or XP is inactive

Richie Hindle richie at entrian.com
Thu Jul 15 17:02:18 CEST 2004


[Bart]
> Auditors want us to log out a user if the computer they are logged onto 
> has been unused/inactive for a set period of time. It's trivial to 
> logout the user, but we're having trouble telling if the system is 
> indeed inactive. [...]
> 
> We're using Python 2.3.x w/o Mr. Hammonds extra Windows extensions. We 
> do not want to install the win32 extensions.

It's a bit weird to want to do Windows-specific things without installing
the win32 extensions.  Would you be prepared to install ctypes and one extra
DLL?  In that case I have a solution for you.  If not, I don't believe it's
possible.

At http://www.codeproject.com/dll/trackuseridle.asp is an article on
tracking idle time.  From the misleading-named "Download source files" link
you can download a zip file which includes IdleTrac.dll.  Using ctypes you
can drive that DLL from Python like this:

---------------------------------------------------------------------------

import time
from ctypes import *

kernel32 = windll.kernel32
GetTickCount = kernel32.GetTickCount

# IdleTrac's API uses C++ mangled names.  No bother.
IdleTrac = windll.IdleTrac
IdleTrackerInit = getattr(IdleTrac, "?IdleTrackerInit@@YAHXZ")
IdleTrackerTerm = getattr(IdleTrac, "?IdleTrackerTerm@@YAXXZ")
IdleTrackerGetLastTickCount = \
        getattr(IdleTrac, "?IdleTrackerGetLastTickCount@@YAKXZ")

IdleTrackerInit()
for i in range(10):
   idleDelta = float(GetTickCount() - IdleTrackerGetLastTickCount()) / 1000
   print "Last input event was %.2f seconds ago." % idleDelta
   time.sleep(1)
IdleTrackerTerm()

---------------------------------------------------------------------------

Hope that helps,

-- 
Richie Hindle
richie at entrian.com




More information about the Python-list mailing list