-1

Using python, I need to be able to detect when a windows explorer window has been closed by the user. At first I tried PID's before i realised that it was all connected to a central proccess, "explorer.exe" :( classic noobish me (this meant that PID wasn't changed at all after it was closed). So my question is, how would i be able to detect when a windows explorer windows has been closed. Thank you :)

Here is my current code (it definitely works, but it can't detect when it closes because explorer.exe also runs the task bar etc) :

import psutil
import re

def check4pid(PROCNAME):

for proc in psutil.process_iter():
    if proc.name() == PROCNAME:
        PIDX=re.search('pid=(.*?), name',str(proc)).group(1)
        #print proc
        print PIDX
if psutil.pid_exists(int(PIDX)):
    print "a process with PID %d exists" % int(PIDX)
else:
    print "a process with PID %d does not exist" % int(PIDX)

check4pid("explorer.exe")

John Hon
  • 183
  • 2
  • 10
  • Possible duplicate of [List running processes on 64-bit Windows](http://stackoverflow.com/questions/1632234/list-running-processes-on-64-bit-windows) – Torxed Apr 13 '16 at 15:54
  • Not sure how much COM support you get from Python, but this is how you would do it (without brute-forcing it, like @Torxed suggests): [A big little program: Monitoring Internet Explorer and Explorer windows, part 3: Tracking creation and destruction](https://blogs.msdn.microsoft.com/oldnewthing/20130614-00/?p=4083/). – IInspectable Apr 13 '16 at 16:20
  • @Ilnspectable i never suggested a brute force, whi is a standard communication tool within windows much like DBUS is a standard messaging protocol/tool in Linux (yea I know it's a bad comparison). Using a wmi module in python is more elegant than whatever piece or C code you just linked? – Torxed Apr 13 '16 at 16:24
  • I think I understand what you mean by brute force. You want to listen to the actual calls with would require a c library to load the appropriate win32 dlls to get this function. Or use pywin32 that hasn't been touched in years. Sure that would work too but using Wmi the proper way would be no less elegant. Also this question falls under the "recommend to find a tool" / "do the code for me" category anyways. – Torxed Apr 13 '16 at 16:25
  • @Torxed: The brute-force part is, where you have to constantly take snapshots, and compare those to a previous snapshot to deduce, whether an Explorer process was terminated. There is nothing even remotely elegant about polling. The link I provided shows you an event-based approach, where Explorer informs your application, whenever an instance is launched, or terminated. – IInspectable Apr 13 '16 at 16:28
  • @Ilnspectable if it's Explorer that handles that event creation, what will you do when Explorer crashes and it's the very exe you're trying to catch running or not? – Torxed Apr 13 '16 at 16:30
  • @Torxed: It's the Windows Shell, that manages the list of `ShellWindows`. I have no doubt, that the Shell doesn't require the collaboration of foreign processes to maintain a consistent list of `ShellWindows`. If Explorer crashes or gets terminated through *taskkill*, or the Task Manager, it's windows are removed from the list (and the appropriate events triggered and dispatched to listeners). – IInspectable Apr 13 '16 at 16:37
  • @Ilnspectable good, just wanted to clarify that when you sad "Explorer informs" you ment a system service and not the actual process Explorer. Thanks for clarifying this. – Torxed Apr 13 '16 at 16:39

1 Answers1

0

Have you used psutil?

This might help you,

import psutil
list = psutil.pids()

for i in range(0, len(list)):
    try:
        prc = psutil.Process(list[i])
        if prc.cmdline()[0].find("explorer.exe") = 1:
          print ('Explorer found open')
            break;
    except:
       print('Explorer not found open')
Andy
  • 49,085
  • 60
  • 166
  • 233
  • And... how exactly does this help you monitor, that an Explorer instance was closed? Besides, checking for *"explorer.exe"* in a command line is insanely brittle. – IInspectable Apr 13 '16 at 16:21
  • Do you want to monitor if explorer is closed by a specific user? –  Apr 13 '16 at 16:24
  • i have tried psutil but it was then that i realised that explorer.exe was a big program not for just a windows. It's not like firefox.exe where if you close the window then it stops. – John Hon Apr 14 '16 at 02:39