Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock windows workstation using Python

Tags:

python

windows

Is there a way to lock the PC from a Python script on Windows?

I do not want to implement some kind of locking on my own - I'd like to use the same lock screen that's also used when the user presses WIN+L or locks the machine via the start menu.

like image 821
ThiefMaster Avatar asked Dec 22 '13 19:12

ThiefMaster


People also ask

How do you lock your computer workstation?

To lock a Windows workstation press CTRL + ALT + DEL and choose "Lock" from the list. To resume work, log back in with your password. Alternatively you can use the keyboard shortcut Windows button + L, which immediately locks the workstation.

How do I lock my Windows 10 workstation?

Locking Your Computer Press and hold the Windows logo key on your keyboard (this key should appear next to the Alt key), and then press the L key. Your computer will be locked, and the Windows 10 login screen will be displayed.

What is the quickest way to lock your Windows workstation?

Press the Windows Key + L Press the Windows and L keys at the same time. It should lock instantly.


2 Answers

This can be done with the LockWorkStation() function from user32.dll:

This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation.

In Python it can be called using using the ctypes/windll FFI from the Python stdlib:

import ctypes
ctypes.windll.user32.LockWorkStation()
like image 181
ThiefMaster Avatar answered Oct 02 '22 16:10

ThiefMaster


A good solution that makes us avoid using Libraries/DLL files is to use the command prompet/ power shell. try running this command in your cmd rundll32.exe user32.dll, LockWorkStation....The PC is Locked!! so we can use subprocess to run this command like this:

    import subprocess
    cmd='rundll32.exe user32.dll, LockWorkStation'
    subprocess.call(cmd)
like image 41
Mahmoud Hossam Avatar answered Oct 02 '22 17:10

Mahmoud Hossam