Created
June 14, 2026 06:58
-
-
Save ynkdir/779c66ba4fb508bdcba1ee8fa25c4e2b to your computer and use it in GitHub Desktop.
runas
This file contains hidden or 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
| import sys | |
| from ctypes import sizeof | |
| from win32more import WinError | |
| from win32more.Windows.Win32.Foundation import CloseHandle | |
| from win32more.Windows.Win32.System.Threading import INFINITE, WaitForSingleObject | |
| from win32more.Windows.Win32.UI.Shell import ( | |
| SEE_MASK_NOCLOSEPROCESS, | |
| SHELLEXECUTEINFOW, | |
| ShellExecuteExW, | |
| ) | |
| from win32more.Windows.Win32.UI.WindowsAndMessaging import SW_SHOW | |
| def runas(exe, params): | |
| sei = SHELLEXECUTEINFOW() | |
| sei.cbSize = sizeof(SHELLEXECUTEINFOW) | |
| sei.fMask = SEE_MASK_NOCLOSEPROCESS | |
| sei.lpVerb = "runas" | |
| sei.lpFile = exe | |
| sei.lpParameters = params | |
| sei.nShow = SW_SHOW | |
| # UAC dialog will be shown | |
| r = ShellExecuteExW(sei) | |
| if not r: | |
| raise WinError() | |
| WaitForSingleObject(sei.hProcess, INFINITE) | |
| CloseHandle(sei.hProcess) | |
| def main(): | |
| runas(sys.executable, "") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment