| 7 | |
| 8 | |
| 9 | def is_admin() -> bool: |
| 10 | # NOTE: 需要在执行注册表写入与系统设置变更前做权限门禁,避免界面层触发后才失败 |
| 11 | try: |
| 12 | ok = bool(ctypes.windll.shell32.IsUserAnAdmin()) |
| 13 | log_event(logging.INFO, "admin_check", "管理员权限检查完成", action="admin_check", status=("ok" if ok else "not_admin")) |
| 14 | return ok |
| 15 | except Exception as e: |
| 16 | log_event( |
| 17 | logging.ERROR, |
| 18 | "admin_check_failed", |
| 19 | f"管理员权限检查失败: {e}", |
| 20 | action="admin_check", |
| 21 | status="failed", |
| 22 | error_type=type(e).__name__, |
| 23 | error_message=str(e), |
| 24 | traceback=traceback.format_exc(), |
| 25 | exc_info=True, |
| 26 | ) |
| 27 | return False |
| 28 | |
| 29 | |
| 30 | def run_as_admin() -> None: |