[python-win32] Windows Service, user activity and timed message box

Ferdinand Sousa ferdinandsousa at gmail.com
Sun Feb 1 15:25:22 CET 2009


Hi , first of all, thanks to Harald Armin Massa for the timed message box
code (attached as text for reference). I'll tweak it to my requirements.
Also, my thanks to Tim J Golden for his help.
 As regards the following:

> B] How do we check duration since last user activity -- Assuming that the
>> screen is set to turn off/display a screensaver after a certain duration,
>> I
>> wouldn't mind checking if this has indeed occurred (I know how to use
>> win32gui <http://win32gui.html/>.SystemParametersInfo to check if either
>> of
>> these has occurred). However, I would prefer to check the time elapsed
>> since
>> last user activity.
>>
>
> I don't know that there's a function to do that for
> the entire system. GetLastInputInfo will do it for your
> own application, but you'd probably need to install some
> system hooks and roll your own to get this at system level.
> (And that sounds like overkill for what you're after).
>
> Perhaps a more precise description of your requirement would help?
>

I couldn't find the GetLastInputInfo function (pywin32 212, python 2.5.2 on
Xp).
What I'm putting together is a script that uses Adobe Acrobat COM. It opens
a port and listens for connections on the LAN. When a pdf is sent to the
port, my script opens it in Acrobat and performs some operations. If Acrobat
is not currently open (checks for an open window with 1st 26 characters of
title text =="Adobe Acrobat Professional"), the script opens Acrobat (in
hidden mode, no visible window) performs the operations and closes it. If
Acrobat is already open, a dialog asks for user's permission before
continuing (because it is made invisible). It processes the file and closes
it, and the user gets back control of Acrobat as it was, with all the files
that were opened by the user as they were. The issue I want to handle is if
Acrobat is open, but, say, the user is away from his desk.

It just occurred to me, the timed message box and user activity  solve the
same problem (sometimes you're so keen on how-can-I-do-A-and-B that you
don't realise that either of them solve the same issue :-) ). Guess it is
overkill, just like you said, but I'm trying to learn as many new ways to do
things as possible.

Thanks very much,
Ferdi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20090201/ed7b37ed/attachment.htm>
-------------- next part --------------
import threading, time, ctypes, ctypes.wintypes
import win32con
import Queue

WM_CLOSE = 0x0010
MB_OK = 0

def find_messagebox(title, threadid, processid):
   hwnd = ctypes.windll.user32.FindWindowA(None, title)
   if hwnd:
       p = ctypes.wintypes.DWORD()
       t = ctypes.windll.user32.GetWindowThreadProcessId(hwnd, ctypes.byref(p))
       if p.value == processid and t == threadid:
           return hwnd
   return 0

def closer(title, timeout, threadid, processid, abort):
   # give windows some time to create the message box
   time.sleep(0.1)
   hwnd = 0
   while timeout > 0:
       if abort.isSet():
           return
       if not hwnd:
           hwnd = find_messagebox(title, threadid, processid)
       if hwnd:
           ctypes.windll.user32.SetWindowTextA(hwnd, "%s - (%d
Sekunden)" % (title, int(timeout)))
       wait = min(timeout, 1)
       time.sleep(wait)
       timeout -= wait
   if hwnd:
       ctypes.windll.user32.PostMessageA(hwnd, WM_CLOSE, 0, 0)

def MessageBoxTimeout(hwnd, text, title, flags=MB_OK, timeout=30):
   class MBT(threading.Thread):
       def run(self):
           threadid = ctypes.windll.kernel32.GetCurrentThreadId()
           processid = ctypes.windll.kernel32.GetCurrentProcessId()
           abort = threading.Event()
           t = threading.Thread(target=closer, args = (title,
timeout, threadid, processid, abort))
           t.setDaemon(True)
           t.start()
           result = ctypes.windll.user32.MessageBoxA(hwnd, text, title, flags)
           abort.set()

   x=MBT()
   x.start()
   return x


More information about the python-win32 mailing list