Add Windows administrator privilege checking and error handling

- Add is_admin_windows() function to check for administrator privileges
- Require administrator privileges on Windows startup with clear instructions
- Enhanced error handling in USB writing to detect elevation errors
- Provide clear guidance on running as administrator
- Handle WinError 740 and other elevation-related errors
- Resolves 'The requested operation requires elevation' error
parent c52aa4a0
......@@ -270,10 +270,25 @@ exit
except subprocess.CalledProcessError as e:
if script_path and os.path.exists(script_path):
os.unlink(script_path)
# Check if it's a permission error
if "Access is denied" in str(e) or "elevation" in str(e).lower():
raise Exception("Administrator privileges required. Please run the application as administrator.")
else:
raise Exception(f"Failed to format USB drive: {e}")
except PermissionError:
if script_path and os.path.exists(script_path):
os.unlink(script_path)
raise Exception("Administrator privileges required. Please run the application as administrator.")
except Exception as e:
if script_path and os.path.exists(script_path):
os.unlink(script_path)
# Check if it's a Windows elevation error
error_str = str(e).lower()
if "winerror 740" in error_str or "elevation" in error_str or "administrator" in error_str:
raise Exception("Administrator privileges required. Please run the application as administrator.")
else:
raise Exception(f"USB creation failed: {e}")
def _copy_directory_windows(self, src_dir, dst_dir):
......@@ -740,13 +755,31 @@ class USBCreatorApp(QMainWindow):
self.close()
def is_admin_windows():
"""Check if running with administrator privileges on Windows"""
try:
import ctypes
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def main():
app = QApplication(sys.argv)
app.setApplicationName("MBetter USB Creator")
app.setApplicationVersion("1.0")
# Check for admin privileges
if platform.system() != "Windows":
if platform.system() == "Windows":
if not is_admin_windows():
QMessageBox.warning(None, "Administrator Required",
"This application requires administrator privileges to format USB drives.\n\n"
"Please run as administrator:\n"
"1. Right-click on the executable or Python file\n"
"2. Select 'Run as administrator'\n\n"
"Or run from elevated command prompt:\n"
"python usb_creator_gui.py")
sys.exit(1)
else:
if os.geteuid() != 0:
QMessageBox.warning(None, "Admin Required",
"This application requires sudo privileges.\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment