@@ -72,40 +72,13 @@ def is_protocol_registered(self) -> bool:
7272 return False
7373
7474 def _register_windows_protocol (self ) -> bool :
75- """Windows系统注册协议"""
75+ """Windows系统注册协议(单用户模式,无需管理员权限) """
7676 try :
7777 # 获取当前可执行文件路径
7878 exe_path = self ._get_executable_path ()
7979
80- # 首先尝试注册到HKEY_CLASSES_ROOT
81- try :
82- # 注册协议到HKEY_CLASSES_ROOT
83- with winreg .CreateKey (
84- winreg .HKEY_CLASSES_ROOT , self .protocol_name
85- ) as key :
86- winreg .SetValueEx (
87- key , "" , 0 , winreg .REG_SZ , f"URL:{ self .app_name } Protocol"
88- )
89- winreg .SetValueEx (key , "URL Protocol" , 0 , winreg .REG_SZ , "" )
90-
91- # 注册命令
92- command_key_path = f"{ self .protocol_name } \\ shell\\ open\\ command"
93- with winreg .CreateKey (
94- winreg .HKEY_CLASSES_ROOT , command_key_path
95- ) as key :
96- winreg .SetValueEx (
97- key , "" , 0 , winreg .REG_SZ , f'"{ exe_path } " --url "%1"'
98- )
99-
100- return True
101-
102- except (OSError , PermissionError ) as e :
103- # 如果HKEY_CLASSES_ROOT失败,尝试注册到HKEY_CURRENT_USER
104- if e .errno == 5 or "Access is denied" in str (e ): # WinError 5
105- logger .error (f"管理员权限不足,尝试注册到当前用户: { e } " )
106- return self ._register_windows_protocol_current_user (exe_path )
107- else :
108- raise
80+ # 直接注册到HKEY_CURRENT_USER(单用户模式,无需管理员权限)
81+ return self ._register_windows_protocol_current_user (exe_path )
10982
11083 except Exception as e :
11184 logger .error (f"Windows协议注册失败: { e } " )
@@ -136,28 +109,10 @@ def _register_windows_protocol_current_user(self, exe_path: str) -> bool:
136109 return False
137110
138111 def _unregister_windows_protocol (self ) -> bool :
139- """Windows系统注销协议"""
112+ """Windows系统注销协议(单用户模式,无需管理员权限) """
140113 try :
141- # 首先尝试删除系统级注册(HKEY_CLASSES_ROOT)
142- try :
143- winreg .DeleteKey (
144- winreg .HKEY_CLASSES_ROOT ,
145- f"{ self .protocol_name } \\ shell\\ open\\ command" ,
146- )
147- winreg .DeleteKey (
148- winreg .HKEY_CLASSES_ROOT , f"{ self .protocol_name } \\ shell\\ open"
149- )
150- winreg .DeleteKey (
151- winreg .HKEY_CLASSES_ROOT , f"{ self .protocol_name } \\ shell"
152- )
153- winreg .DeleteKey (winreg .HKEY_CLASSES_ROOT , self .protocol_name )
154- return True
155- except (OSError , PermissionError ) as e :
156- if e .errno == 5 or "Access is denied" in str (e ): # WinError 5
157- # 如果系统级删除失败,尝试删除当前用户注册
158- return self ._unregister_windows_protocol_current_user ()
159- else :
160- raise
114+ # 直接删除当前用户注册表项(单用户模式,无需管理员权限)
115+ return self ._unregister_windows_protocol_current_user ()
161116
162117 except Exception as e :
163118 logger .error (f"Windows协议注销失败: { e } " )
@@ -187,19 +142,14 @@ def _unregister_windows_protocol_current_user(self) -> bool:
187142 return False
188143
189144 def _is_windows_protocol_registered (self ) -> bool :
190- """检查Windows协议是否已注册"""
145+ """检查Windows协议是否已注册(单用户模式) """
191146 try :
192- # 首先检查HKEY_CLASSES_ROOT(系统级)
193- with winreg .OpenKey (winreg .HKEY_CLASSES_ROOT , self .protocol_name ) as key :
147+ # 检查HKEY_CURRENT_USER(用户级)
148+ key_path = f"Software\\ Classes\\ { self .protocol_name } "
149+ with winreg .OpenKey (winreg .HKEY_CURRENT_USER , key_path ) as key :
194150 return True
195151 except (OSError , FileNotFoundError ):
196- try :
197- # 如果没找到,检查HKEY_CURRENT_USER(用户级)
198- key_path = f"Software\\ Classes\\ { self .protocol_name } "
199- with winreg .OpenKey (winreg .HKEY_CURRENT_USER , key_path ) as key :
200- return True
201- except (OSError , FileNotFoundError ):
202- return False
152+ return False
203153
204154 def _register_linux_protocol (self ) -> bool :
205155 """Linux系统注册协议"""
0 commit comments