Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.user32.isWorkstationLocked().

like image 449
Ivan Baksheev Avatar asked Dec 29 '15 16:12

Ivan Baksheev


People also ask

How do I know if a Python application is open?

We import the psutil module. Then we search for chrome.exe in all running processes on the local machine using psutil. process_iter(). If found it will return output as TRUE, else FALSE.

How do I lock a window in Python?

If we type "rundll32.exe user32. dll, LockWorkStation" in the command prompt or powershell then it will lock the pc too.


1 Answers

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.")
like image 98
macok Avatar answered Oct 13 '22 01:10

macok