Author Topic: Request elevation in a program without using PowerShell  (Read 3380 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Request elevation in a program without using PowerShell
« on: February 06, 2021, 11:48:01 pm »
The below code uses the standard setup of asking command prompt if the scope of the current session is user or admin. Then, if not admin, prints a statement saying "please run as administrator". Press enter and then the program uses ShellExecute from Shell32 to apply the "runas" verb to COMMAND$(0) (which is the executable's full path). Then you just press yes on the UAC prompt and voila; an elevated program without using PowerShell. I used to use PowerShell to achieve this but of course that is much slower. ShellExecute isn't only limited to this but I figured I'd at least share this bit with y'all before I do more usage and put it in my API collection.

Code: QB64: [Select]
  1.  
  2.     Function ShellExecute%& Alias ShellExecuteA (ByVal hwnd As Long, lpOperation As String, lpFile As String, lpParameters As String, lpDirectory As String, Byval nShowCmd As Integer)
  3.  
  4. Dim As Long errorlevel
  5.  
  6. cmd = ">nul 2>&1 " + Chr$(34) + "%SYSTEMROOT%\system32\cacls.exe" + Chr$(34) + " " + Chr$(34) + "%SYSTEMROOT%\system32\config\system" + Chr$(34)
  7. errorlevel = _ShellHide(cmd)
  8. Print errorlevel
  9. If errorlevel <> 0 Then
  10.     Print "not elevated."
  11.     Print "please run as administrator."
  12.     Sleep
  13.     Print ShellExecute(0, "runas" + Chr$(0), Command$(0) + Chr$(0), "", "", 5)
  14.     System
  15.     Print "elevated."
« Last Edit: February 07, 2021, 12:02:12 am by SpriggsySpriggs »
Shuwatch!