Commits

Yuya Nishihara committed 823cae1

python: dirty code to accept WM_CLOSE by console app

  • Participants
  • Parent commits be0e184

Comments (0)

Files changed (1)

File python/win32wmclose.py

+import sys
+from ctypes import pointer, windll
+from ctypes.wintypes import MSG
+from PyQt4.QtCore import QCoreApplication, QProcess, QTimer
+
+GenerateConsoleCtrlEvent = windll.kernel32.GenerateConsoleCtrlEvent
+GetMessage = windll.user32.GetMessageA
+TranslateMessage = windll.user32.TranslateMessage
+DispatchMessage = windll.user32.DispatchMessageA
+
+CTRL_C_EVENT = 0
+WM_CLOSE = 0x0010
+
+def slave():
+    # TODO hidden top-level window instead?
+    print 'start message loop'
+    sys.stdout.flush()
+    msg = MSG()
+    lpmsg = pointer(msg)
+    while True:
+        r = GetMessage(lpmsg, None, 0, 0)
+        if r == 0:  # WM_QUIT
+            break
+        elif r == -1:
+            # XXX handle error
+            continue
+        if msg.message == WM_CLOSE:
+            GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)
+        # probably no need to dispatch message to WndProc
+        #TranslateMessage(lpmsg)
+        #DispatchMessage(lpmsg)
+    sys.exit(msg.wParam)
+
+def master():
+    app = QCoreApplication(sys.argv)
+    proc = QProcess()
+    proc.setProcessChannelMode(QProcess.ForwardedChannels)
+    proc.finished.connect(app.quit)
+    proc.start(sys.executable, [__file__, 'slave'])
+    QTimer.singleShot(2000, proc.terminate)
+    app.exec_()
+
+if __name__ == '__main__':
+    if sys.argv[1:] == ['slave']:
+        slave()
+    else:
+        master()