Lock windows workstation using Python

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()

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)

Tags:

Python

Windows