Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Created June 14, 2026 06:58
Show Gist options
  • Select an option

  • Save ynkdir/779c66ba4fb508bdcba1ee8fa25c4e2b to your computer and use it in GitHub Desktop.

Select an option

Save ynkdir/779c66ba4fb508bdcba1ee8fa25c4e2b to your computer and use it in GitHub Desktop.
runas
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