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,11 +270,26 @@ exit ...@@ -270,11 +270,26 @@ exit
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
if script_path and os.path.exists(script_path): if script_path and os.path.exists(script_path):
os.unlink(script_path) os.unlink(script_path)
raise Exception(f"Failed to format USB drive: {e}")
# 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: except Exception as e:
if script_path and os.path.exists(script_path): if script_path and os.path.exists(script_path):
os.unlink(script_path) os.unlink(script_path)
raise Exception(f"USB creation failed: {e}")
# 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): def _copy_directory_windows(self, src_dir, dst_dir):
"""Copy directory contents using robocopy for better Windows compatibility""" """Copy directory contents using robocopy for better Windows compatibility"""
...@@ -740,15 +755,33 @@ class USBCreatorApp(QMainWindow): ...@@ -740,15 +755,33 @@ class USBCreatorApp(QMainWindow):
self.close() 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(): def main():
app = QApplication(sys.argv) app = QApplication(sys.argv)
app.setApplicationName("MBetter USB Creator") app.setApplicationName("MBetter USB Creator")
app.setApplicationVersion("1.0") app.setApplicationVersion("1.0")
# Check for admin privileges # 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: if os.geteuid() != 0:
QMessageBox.warning(None, "Admin Required", QMessageBox.warning(None, "Admin Required",
"This application requires sudo privileges.\n" "This application requires sudo privileges.\n"
"Please run with: sudo python3 usb_creator_gui.py") "Please run with: sudo python3 usb_creator_gui.py")
sys.exit(1) sys.exit(1)
......
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