In Python 3, how can I tell if Windows is locked?

后端 未结 8 678
小鲜肉
小鲜肉 2021-01-13 11:27

How can I check whether a Windows OS workstation is locked? (e.g. Win+L or choosing the lock option after Ctrl+Alt+Del.)

I want something like ctypes.windll.us

相关标签:
8条回答
  • 2021-01-13 12:21

    A hacky I discovered to get around to see if Windows 10 is locked is to look at the running processes using psutil. You then search to see if LogonUI.exe is running. This process only runs if there when a user has a locked session.

    Note: If you use switch users this process will show as running and this workaround will not work. Windows actually spawns multiple LogonUI.exe processes, one per logged on locked user. It is only useful where only one person is logged on at a time.

    import psutil
    
    for proc in psutil.process_iter():
        if(proc.name() == "LogonUI.exe"):
            print ("Locked")
    
    0 讨论(0)
  • 2021-01-13 12:24

    Hi Check these 4 lines..

    returns the application name which is on the screen.. if window is locked returns the string - Windows Default Lock Screen.

    from win32gui import GetWindowText, GetForegroundWindow
    import time
    time.sleep(5)
    # lock the system or open the application for a check
    print(GetWindowText(GetForegroundWindow()))
    
    0 讨论(0)
提交回复
热议问题