这个代码今天在四个不同的Windows 7和10机器上为我工作,试试类似的东西。
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')
编辑:另外,这个今天也能用。
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.")