|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import ctypes |
| 4 | +from ctypes import wintypes |
| 5 | +from subprocess import list2cmdline |
| 6 | +from app.tools.path_utils import get_data_path |
| 7 | + |
| 8 | +from loguru import logger |
| 9 | + |
| 10 | +_uiaccess_dll = None |
| 11 | +_user32 = None |
| 12 | +_kernel32 = None |
| 13 | + |
| 14 | +UIACCESS_RESTART_ENV = "SECRANDOM_RESTART_UIACCESS" |
| 15 | + |
| 16 | + |
| 17 | +def _is_windows() -> bool: |
| 18 | + return os.name == "nt" |
| 19 | + |
| 20 | + |
| 21 | +def _get_kernel32(): |
| 22 | + global _kernel32 |
| 23 | + if _kernel32 is not None: |
| 24 | + return _kernel32 |
| 25 | + _kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) |
| 26 | + _kernel32.ProcessIdToSessionId.argtypes = [ |
| 27 | + wintypes.DWORD, |
| 28 | + ctypes.POINTER(wintypes.DWORD), |
| 29 | + ] |
| 30 | + _kernel32.ProcessIdToSessionId.restype = wintypes.BOOL |
| 31 | + return _kernel32 |
| 32 | + |
| 33 | + |
| 34 | +def _get_user32(): |
| 35 | + global _user32 |
| 36 | + if _user32 is not None: |
| 37 | + return _user32 |
| 38 | + _user32 = ctypes.WinDLL("user32", use_last_error=True) |
| 39 | + try: |
| 40 | + fn = _user32.SetWindowBand |
| 41 | + fn.argtypes = [wintypes.HWND, wintypes.HWND, wintypes.DWORD] |
| 42 | + fn.restype = wintypes.BOOL |
| 43 | + except Exception: |
| 44 | + pass |
| 45 | + _user32.SetWindowPos.argtypes = [ |
| 46 | + wintypes.HWND, |
| 47 | + wintypes.HWND, |
| 48 | + ctypes.c_int, |
| 49 | + ctypes.c_int, |
| 50 | + ctypes.c_int, |
| 51 | + ctypes.c_int, |
| 52 | + wintypes.UINT, |
| 53 | + ] |
| 54 | + _user32.SetWindowPos.restype = wintypes.BOOL |
| 55 | + return _user32 |
| 56 | + |
| 57 | + |
| 58 | +def _get_uiaccess_dll(): |
| 59 | + global _uiaccess_dll |
| 60 | + if _uiaccess_dll is not None: |
| 61 | + return _uiaccess_dll |
| 62 | + dll_path = get_data_path("dlls", "uiaccess.dll") |
| 63 | + _uiaccess_dll = ctypes.WinDLL(str(dll_path), use_last_error=True) |
| 64 | + |
| 65 | + _uiaccess_dll.IsUIAccess.argtypes = [] |
| 66 | + _uiaccess_dll.IsUIAccess.restype = wintypes.BOOL |
| 67 | + |
| 68 | + _uiaccess_dll.StartUIAccessProcess.argtypes = [ |
| 69 | + wintypes.LPCWSTR, |
| 70 | + wintypes.LPCWSTR, |
| 71 | + wintypes.DWORD, |
| 72 | + ctypes.POINTER(wintypes.DWORD), |
| 73 | + wintypes.DWORD, |
| 74 | + ] |
| 75 | + _uiaccess_dll.StartUIAccessProcess.restype = wintypes.BOOL |
| 76 | + |
| 77 | + return _uiaccess_dll |
| 78 | + |
| 79 | + |
| 80 | +def is_uiaccess_process() -> bool: |
| 81 | + if not _is_windows(): |
| 82 | + return False |
| 83 | + try: |
| 84 | + dll = _get_uiaccess_dll() |
| 85 | + return bool(dll.IsUIAccess()) |
| 86 | + except Exception: |
| 87 | + return False |
| 88 | + |
| 89 | + |
| 90 | +def start_uiaccess_process(cmd_list: list[str]) -> int: |
| 91 | + if not _is_windows(): |
| 92 | + return 0 |
| 93 | + if not cmd_list: |
| 94 | + return 0 |
| 95 | + |
| 96 | + try: |
| 97 | + kernel32 = _get_kernel32() |
| 98 | + pid = wintypes.DWORD(os.getpid()) |
| 99 | + session = wintypes.DWORD(0) |
| 100 | + if not kernel32.ProcessIdToSessionId(pid, ctypes.byref(session)): |
| 101 | + err = ctypes.get_last_error() |
| 102 | + logger.debug("获取 SessionId 失败: {}", err) |
| 103 | + return 0 |
| 104 | + |
| 105 | + cmd_line = list2cmdline(list(cmd_list)) |
| 106 | + |
| 107 | + dll = _get_uiaccess_dll() |
| 108 | + out_pid = wintypes.DWORD(0) |
| 109 | + ctypes.set_last_error(0) |
| 110 | + ok = bool( |
| 111 | + dll.StartUIAccessProcess( |
| 112 | + None, |
| 113 | + cmd_line, |
| 114 | + 0, |
| 115 | + ctypes.byref(out_pid), |
| 116 | + session.value, |
| 117 | + ) |
| 118 | + ) |
| 119 | + if not ok: |
| 120 | + err = ctypes.get_last_error() |
| 121 | + logger.debug("启动 UIAccess 进程失败: {}", err) |
| 122 | + return 0 |
| 123 | + |
| 124 | + logger.debug("已启动 UIAccess 进程: pid={}", int(out_pid.value)) |
| 125 | + return int(out_pid.value) |
| 126 | + except Exception as e: |
| 127 | + logger.debug("启动 UIAccess 进程异常: {}", e) |
| 128 | + return 0 |
| 129 | + |
| 130 | + |
| 131 | +def ensure_uiaccess_for_current_process() -> bool: |
| 132 | + if not _is_windows(): |
| 133 | + return False |
| 134 | + if is_uiaccess_process(): |
| 135 | + return False |
| 136 | + |
| 137 | + try: |
| 138 | + kernel32 = _get_kernel32() |
| 139 | + pid = wintypes.DWORD(os.getpid()) |
| 140 | + session = wintypes.DWORD(0) |
| 141 | + if not kernel32.ProcessIdToSessionId(pid, ctypes.byref(session)): |
| 142 | + err = ctypes.get_last_error() |
| 143 | + logger.debug("获取 SessionId 失败: {}", err) |
| 144 | + return False |
| 145 | + |
| 146 | + if getattr(sys, "frozen", False): |
| 147 | + cmd_list = [sys.executable] + (sys.argv[1:] if sys.argv else []) |
| 148 | + else: |
| 149 | + argv = list(sys.argv) if sys.argv else [] |
| 150 | + if argv: |
| 151 | + cmd_list = [sys.executable] + argv |
| 152 | + else: |
| 153 | + cmd_list = [sys.executable] |
| 154 | + |
| 155 | + return bool(start_uiaccess_process(cmd_list)) |
| 156 | + except Exception as e: |
| 157 | + logger.debug("启动 UIAccess 进程异常: {}", e) |
| 158 | + return False |
| 159 | + |
| 160 | + |
| 161 | +def set_window_band_uiaccess(hwnd: int) -> bool: |
| 162 | + if not _is_windows(): |
| 163 | + return False |
| 164 | + if not hwnd: |
| 165 | + return False |
| 166 | + if not is_uiaccess_process(): |
| 167 | + return False |
| 168 | + |
| 169 | + try: |
| 170 | + user32 = _get_user32() |
| 171 | + h = wintypes.HWND(hwnd) |
| 172 | + hwnd_topmost = wintypes.HWND(-1) |
| 173 | + |
| 174 | + SWP_NOMOVE = 0x0002 |
| 175 | + SWP_NOSIZE = 0x0001 |
| 176 | + SWP_NOACTIVATE = 0x0010 |
| 177 | + SWP_SHOWWINDOW = 0x0040 |
| 178 | + flags = SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW |
| 179 | + return bool(user32.SetWindowPos(h, hwnd_topmost, 0, 0, 0, 0, flags)) |
| 180 | + except Exception: |
| 181 | + return False |
0 commit comments