Author Topic: Creating a program that will self-elevate  (Read 1751 times)

0 Members and 1 Guest are viewing this topic.

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Creating a program that will self-elevate
« on: April 18, 2019, 12:13:15 pm »
If you have a need to have your program run with elevated privileges, this code will take care of that for you. If the program is not run elevated then this code will re-launch the program elevated and terminate the original instance.
 

Code: QB64: [Select]
  1. ' This application needs to be run with elevated permissions. Perform a test to see if user started the program
  2. ' with elevated permissions. If not, then relaunch the program with elevated permissions and close the
  3. ' original instance of the program.
  4.  
  5. Cmd$ = ">nul 2>&1 " + CHR$(34) + "%SYSTEMROOT%\system32\cacls.exe" + CHR$(34) + " " + CHR$(34) + "%SYSTEMROOT%\system32\config\system" + CHR$(34)
  6. errorlevel = _SHELLHIDE(Cmd$)
  7. IF errorlevel <> 0 THEN
  8.     SHELL _HIDE "powershell.exe Start-Process " + CHR$(34) + (MID$(COMMAND$(0), _INSTRREV(COMMAND$(0), "\") + 1)) + CHR$(34) + " -Verb runAs"
  9.     SYSTEM
  10.  
  11. ' We reach this point when the program is run elevated.
  12.  
  13. PRINT "The program is now running elevated."

Offline Diguelo

  • Newbie
  • Posts: 2
  • I newer mayk typo errurs in coad.
    • View Profile
Re: Creating a program that will self-elevate
« Reply #1 on: April 21, 2019, 07:26:24 am »
Thanks, useful little code
Programming is like religion.
You pray that it works, then blame an unseen deity when it doesn't

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Creating a program that will self-elevate
« Reply #2 on: April 21, 2019, 11:55:07 am »
I'm more curious as to why anyone would need this? Is it solely because of trying to RUN or SHELL to other apps that require administrative pillages, or is there some other reason? Maybe on a shared network? I simply work on a home system today, and when I had my office network set up, things were a lot simpler. I still did not require anything run as an administrator.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Creating a program that will self-elevate
« Reply #3 on: May 02, 2019, 10:52:21 pm »
Pete, apologies for the slow response. Just saw your message now.

In my case, I was running a lot of commands via SHELL such as DISM that require elevation.

Maybe it's not something that is needed frequently, but it's nice to know it can be done rather easily when needed.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Creating a program that will self-elevate
« Reply #4 on: May 22, 2020, 08:39:09 am »
That code made my computer recursively launch and close my program for a very long time until I held down CTRL + C in a new PowerShell window. Seems like a very dangerous thing to try to SHELL. This is because my computer's ERROR level is 2 for admin and 5 for regular user. I'm not sure why you have chosen errorlevel <> 0 unless there is a difference in our versions of Windows.
« Last Edit: May 22, 2020, 08:41:30 am by SpriggsySpriggs »
Shuwatch!