Note that there are some explanatory texts on larger screens.

plurals
  1. POPython - Windows Shutdown Events
    primarykey
    data
    text
    <p>When using win32api.setConsoleCtrlHandler(), I'm able to receive shutdown/logoff/etc events from Windows, and cleanly shut down my app.</p> <p>However, this only works when running the app under python.exe (i.e., it has a console window), but not under pythonw.exe (no console window).</p> <p>Is there an equivalent way in Windows to receive these events when you have no console and no window to receive them? Or, is there a programmatic way to hide the console window?</p> <p>To be clear - my goal is to be able to successfully receive Windows shutdown/logoff/etc events, without having any kind of console window showing.</p> <p><strong>EDIT:</strong> I've been playing around, and I've gotten quite a bit further. I wrote a piece of test code for this. When I do a "taskkill /im pythonw.exe" - it will receive the message.</p> <p>However, when I do a shutdown, restart, or logoff on Windows, I do not get any messages.</p> <p>Here's the whole thing:</p> <pre><code>""" Testing Windows shutdown events """ import win32con import win32api import win32gui import sys import time def log_info(msg): """ Prints """ print msg f = open("c:\\test.log", "a") f.write(msg + "\n") f.close() def wndproc(hwnd, msg, wparam, lparam): log_info("wndproc: %s" % msg) if __name__ == "__main__": log_info("*** STARTING ***") hinst = win32api.GetModuleHandle(None) wndclass = win32gui.WNDCLASS() wndclass.hInstance = hinst wndclass.lpszClassName = "testWindowClass" messageMap = { win32con.WM_QUERYENDSESSION : wndproc, win32con.WM_ENDSESSION : wndproc, win32con.WM_QUIT : wndproc, win32con.WM_DESTROY : wndproc, win32con.WM_CLOSE : wndproc } wndclass.lpfnWndProc = messageMap try: myWindowClass = win32gui.RegisterClass(wndclass) hwnd = win32gui.CreateWindowEx(win32con.WS_EX_LEFT, myWindowClass, "testMsgWindow", 0, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, win32con.HWND_MESSAGE, 0, hinst, None) except Exception, e: log_info("Exception: %s" % str(e)) if hwnd is None: log_info("hwnd is none!") else: log_info("hwnd: %s" % hwnd) while True: win32gui.PumpWaitingMessages() time.sleep(1) </code></pre> <p>I feel like I'm pretty close here, but I'm definitely missing something!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload