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

后端 未结 8 784
小鲜肉
小鲜肉 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 11:58

    This code worked today for me on four different Windows 7 and 10 machines, try something similar:

    import ctypes
    import time
    user32 = ctypes.windll.User32
    time.sleep(5)
    #
    #print(user32.GetForegroundWindow())
    #
    if (user32.GetForegroundWindow() % 10 == 0): print('Locked')
    # 10553666 - return code for unlocked workstation1
    # 0 - return code for locked workstation1
    #
    # 132782 - return code for unlocked workstation2
    # 67370 -  return code for locked workstation2
    #
    # 3216806 - return code for unlocked workstation3
    # 1901390 - return code for locked workstation3
    #
    # 197944 - return code for unlocked workstation4
    # 0 -  return code for locked workstation4
    #
    else: print('Unlocked')
    

    Edit: Also, this one works today:

    import subprocess
    import time
    time.sleep(5)
    process_name='LogonUI.exe'
    callall='TASKLIST'
    outputall=subprocess.check_output(callall)
    outputstringall=str(outputall)
    if process_name in outputstringall:
        print("Locked.")
    else: 
        print("Unlocked.")
    

提交回复
热议问题