0

I have a PyQt5 application, which I would like to check if the Windows workstation is in a locked state or not.

At first, I have tried to use snippet See if my workstation is locked. It did not work at all on my Windows 7 64-bit. It thinks that workstation is locked all the time.

I have noticed in SO question How to detect Windows is locked? that the above solution is probably a hack and I should use WTSRegisterSessionNotification. I have found the following snippet Terminal Services event monitor for Windows NT/XP/2003/.... It works fine used as is.

I have simplified the code to the following:

import win32con
import win32gui
import win32ts


WM_WTSSESSION_CHANGE        = 0x2B1


class WTSMonitor():
    className = "WTSMonitor"
    wndName = "WTS Event Monitor"

    def __init__(self):
        wc = win32gui.WNDCLASS()
        wc.hInstance = hInst = win32gui.GetModuleHandle(None)
        wc.lpszClassName = self.className
        wc.lpfnWndProc = self.WndProc
        self.classAtom = win32gui.RegisterClass(wc)

        style = 0
        self.hWnd = win32gui.CreateWindow(self.classAtom, self.wndName,
            style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,
            0, 0, hInst, None)
        win32gui.UpdateWindow(self.hWnd)
        win32ts.WTSRegisterSessionNotification(self.hWnd, win32ts.NOTIFY_FOR_ALL_SESSIONS)

    def start(self):
        win32gui.PumpMessages()

    def stop(self):
        win32gui.PostQuitMessage(0)

    def WndProc(self, hWnd, message, wParam, lParam):
        if message == WM_WTSSESSION_CHANGE:
            self.OnSession(wParam, lParam)

    def OnSession(self, event, sessionID):
        print(event)

if __name__ == '__main__':
    m = WTSMonitor()
    m.start()

Now I am trying to merge it with PyQt5 skeleton:

import sys
from PyQt5.QtWidgets import *

class Window(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)

        self.show()

if __name__ == '__main__':    
    app = QApplication(sys.argv)
    win = Window()
    sys.exit(app.exec_())

However, I am not sure how to do that. Every attempt I have made did not work, the event does not seem to be registered. Any idea how to make this work?

Edit: This is one of the merges I have tried.

import sys
from PyQt5.QtWidgets import *
import win32gui
import win32ts


WM_WTSSESSION_CHANGE        = 0x2B1


class WTSMonitor(QWidget):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)    
        self.show()
        win32ts.WTSRegisterSessionNotification(self.winId(), win32ts.NOTIFY_FOR_ALL_SESSIONS)

    def start(self):
        win32gui.PumpMessages()

    def stop(self):
        win32gui.PostQuitMessage(0)

    def WndProc(self, hWnd, message, wParam, lParam):
        if message == WM_WTSSESSION_CHANGE:
            self.OnSession(wParam, lParam)

    def OnSession(self, event, sessionID):
        print(event)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = WTSMonitor()
    win.start()
    sys.exit(app.exec_())
Community
  • 1
  • 1
Fenikso
  • 9,251
  • 5
  • 44
  • 72
  • Can you provide examples of what you tried (when merging) that didn't work? – three_pineapples Feb 19 '15 at 20:32
  • @three_pineapples - I have added one of the merges. The other ones are basically the same just without start/stop. I know it is very naive approach but I am not good enough with winapi. I kind of feel that `win32gui.PumpMessages()` introduces second event loop which will not work. – Fenikso Feb 20 '15 at 07:49

1 Answers1

0

I have adapted ideas from WndProcHookMixin.py posted on wxPython wiki to PyQt. It works as expected.

import sys

from PyQt5.QtWidgets import *
import win32api
import win32con
import win32gui
import win32ts


WM_WTSSESSION_CHANGE = 0x2B1
WTS_SESSION_LOCK = 0x7
WTS_SESSION_UNLOCK = 0x8 


# http://wiki.wxpython.org/HookingTheWndProc
# http://wiki.wxpython.org/HookingTheWndProc?action=AttachFile&do=view&target=WndProcHookMixin.py
# http://wiki.wxpython.org/HookingTheWndProc?action=AttachFile&do=view&target=WndProcHookMixinCtypes.py
class WndProcHookMixin:
    def __init__(self):
        self.msgDict = {}

    def hookWndProc(self):
        self.oldWndProc = win32gui.SetWindowLong(self.winId(), win32con.GWL_WNDPROC, self.localWndProc)

    def unhookWndProc(self):
        win32api.SetWindowLong(self.winId(), win32con.GWL_WNDPROC, self.oldWndProc)

    def addMsgHandler(self, messageNumber, handler):
        self.msgDict[messageNumber] = handler

    def localWndProc(self, hWnd, msg, wParam, lParam):
        if msg in self.msgDict:
            if self.msgDict[msg](wParam, lParam) == False:
                return

        if msg == win32con.WM_DESTROY: 
            self.unhookWndProc()

        return win32gui.CallWindowProc(self.oldWndProc, hWnd, msg, wParam, lParam)


class Window(QWidget, WndProcHookMixin):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)    
        self.show()
        win32ts.WTSRegisterSessionNotification(self.winId(), win32ts.NOTIFY_FOR_ALL_SESSIONS)
        self.addMsgHandler(WM_WTSSESSION_CHANGE, self.on_session)
        self.hookWndProc()

    def on_session(self, wParam, lParam):
        event, session_id = wParam, lParam
        if event == WTS_SESSION_LOCK:
            print("Locked")
        if event == WTS_SESSION_UNLOCK:
            print("Unlocked")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Window()
    sys.exit(app.exec_())
Fenikso
  • 9,251
  • 5
  • 44
  • 72