Created
April 13, 2019 08:17
-
-
Save aweffr/22e94f9ab4efe809f3a5d2488172809c to your computer and use it in GitHub Desktop.
python: windows 提升到 admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_as_admin(argv=None, debug=False): | |
shell32 = ctypes.windll.shell32 | |
if argv is None and shell32.IsUserAnAdmin(): | |
return True | |
if argv is None: | |
argv = sys.argv | |
if hasattr(sys, '_MEIPASS'): | |
# Support pyinstaller wrapped program. | |
arguments = list(map(str, argv[1:])) | |
else: | |
arguments = list(map(str, argv)) | |
argument_line = ' '.join(arguments) | |
executable = str(sys.executable) | |
if debug: | |
print('Command line: ', executable, argument_line) | |
ret = shell32.ShellExecuteW(None, "runas", executable, argument_line, None, 1) | |
if int(ret) <= 32: | |
return False | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment