在Python 3中,如何判断Windows是否被锁定?

8wigbo56  于 2022-11-18  发布在  Windows
关注(0)|答案(9)|浏览(556)

如何检查Windows操作系统工作站是否锁定?(例如,Win+L或在Ctrl+Alt+Del后选择锁定选项。)
我想要类似ctypes.windll.user32.isWorkstationLocked()的东西。

2vuwiymt

2vuwiymt1#

这段代码今天在四台不同的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.")
csga3l58

csga3l582#

我发现了一个查看Windows 10是否被锁定的方法,就是使用psutil查看正在运行的进程。然后搜索LogonUI.exe是否正在运行。这个进程只在用户有一个锁定的会话时运行。
注意:如果您使用“切换用户”,此进程将显示为正在运行,此解决方法将不起作用。Windows实际上会产生多个LogonUI.exe进程,每个登录的锁定用户一个。只有当一次只有一个用户登录时,此方法才有用。

import psutil

for proc in psutil.process_iter():
    if(proc.name() == "LogonUI.exe"):
        print ("Locked")
tjrkku2a

tjrkku2a3#

类似这样的东西应该可以做到这一点:

import time
import ctypes

user32 = ctypes.windll.User32
OpenDesktop = user32.OpenDesktopA
SwitchDesktop = user32.SwitchDesktop
DESKTOP_SWITCHDESKTOP = 0x0100

while 1:
  hDesktop = OpenDesktop ("default", 0, False, DESKTOP_SWITCHDESKTOP)
  result = SwitchDesktop (hDesktop)
  if result:
    print "Unlocked"
    time.sleep (1.0)
  else:
    print time.asctime (), "still locked"
    time.sleep (2)
bkkx9g8r

bkkx9g8r4#

你可以把窗口放在最上面,当会话被锁定时,函数返回0。

import ctypes
user32 = ctypes.windll.User32

def isLocked():
    return user32.GetForegroundWindow() == 0
v09wglhw

v09wglhw5#

LockWorkStation() documentation
您无法调用任何函数来确定工作站是否已锁定。
不是Python的限制,而是系统本身。

5kgi1eie

5kgi1eie6#

我在Windows 10 Pro上的工作方式是获取前景窗口:

whnd = win32gui.GetForegroundWindow()
(_, pid) = win32process.GetWindowThreadProcessId(whnd)
handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid)
filename = win32process.GetModuleFileNameEx(handle, 0)
window_text = win32gui.GetWindowText(whnd)

锁定时,返回Windows Default Lock Screen作为窗口标题,C:\Windows\SystemApp\Microsoft.LockApp_<randomcharacters>\LockApp.exe作为文件名。
但是,正如James Koss所提到的,如果用户正在键入密码,GetForeGroundWindow将返回0。还有其他(非锁定)情况,其中当前ForegroundWindow为0,因此不能依赖于此。

siotufzp

siotufzp7#

嗨,检查这4行..
返回屏幕上的应用程序名称。如果窗口已锁定,则返回字符串- Windows默认锁定屏幕。

from win32gui import GetWindowText, GetForegroundWindow
import time
time.sleep(5)
# lock the system or open the application for a check
print(GetWindowText(GetForegroundWindow()))
uqjltbpv

uqjltbpv8#

根据@Stardidi的回答,这对我很有效(Windows 10专业版):

import time
import win32gui
import win32api
import win32con
import win32process

while True:
    time.sleep(1)
    _, pid = win32process.GetWindowThreadProcessId(win32gui.GetForegroundWindow())

    try:
        handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid)
        filename = win32process.GetModuleFileNameEx(handle, 0)
    except Exception as _e:
        filename = "LockApp.exe"
        del _e

    current_status = "locked" if "LockApp" in filename else "unlocked"
xnifntxz

xnifntxz9#

这里没有简单的答案,但您可以通过会话跟踪来实现这一点。
LockWorkStation() documentation
您无法呼叫任何函数来判断工作站是否已锁定。若要在使用者登入时接收通知,请使用WTSRegisterSessionNotification函数来接收WM_WTSSESSION_CHANGE消息。您可以使用工作阶段通知来追踪桌面状态,以便了解是否可以与使用者互动。
开始将会话通知注册到程序的窗口。

def register(handle: HWND) -> bool:
    """
    @param handle: handle for your message window.
    When registered, Windows Messages related to session event changes will be
    sent to the message window.
    @returns: True is session tracking is successfully registered.

    Blocks until Windows accepts session tracking registration.

    Every call to this function must be paired with a call to unregister.

    https://learn.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsregistersessionnotification
    """
    # OpenEvent handle must be closed with CloseHandle.
    eventObjectHandle: HANDLE = ctypes.windll.kernel32.OpenEventW(
        # Blocks until WTS session tracking can be registered.
        # Windows needs time for the WTS session tracking service to initialize.
        # must ensure that the WTS session tracking service is ready before trying to register
        SYNCHRONIZE,  # DWORD dwDesiredAccess
        False,  # BOOL bInheritHandle - sub-processes do not need to inherit this handle
        # According to the docs, when the Global\TermSrvReadyEvent global event is set,
        # all dependent services have started and WTSRegisterSessionNotification can be successfully called.
        # https://learn.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsregistersessionnotification#remarks
        "Global\\TermSrvReadyEvent"  # LPCWSTR lpName - The name of the event object.
    )
    if not eventObjectHandle:
        error = ctypes.WinError()
        log.error("Unexpected error waiting to register session tracking.")
        return False

    registrationSuccess = ctypes.windll.wtsapi32.WTSRegisterSessionNotification(handle, NOTIFY_FOR_THIS_SESSION)
    ctypes.windll.kernel32.CloseHandle(eventObjectHandle)

    if registrationSuccess:
        log.debug("Registered session tracking")
    else:
        error = ctypes.WinError()
        if error.errno == RPC_S_INVALID_BINDING:
            log.error(
                "WTS registration failed. "
                "Waited successfully on TermSrvReadyEvent to ensure that WTS is ready to allow registration. "
                "Cause of failure unknown. "
            )
        else:
            log.error("Unexpected error registering session tracking.")

    return registrationSuccess

def unregister(handle: HWND) -> None:
    """
    This function must be called once for every call to register.
    If unregistration fails, session tracking may not work properly until the session can be unregistered in a new instance.

    https://learn.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsunregistersessionnotification
    """
    if ctypes.windll.wtsapi32.WTSUnRegisterSessionNotification(handle):
        log.debug("Unregistered session tracking")
    else:
        error = ctypes.WinError()
        log.error("Unexpected error unregistering session tracking.")

在 windows 的Window Message行程常式中,当您收到WM_WTSSESSION_CHANGE时,行程WTS_SESSION_UNLOCKWTS_SESSION_LOCK事件,以追踪锁定的Windows状态。
类似的答案可能会给予更多关于handling windows messages的内容。

相关问题