-8

So i have been looking everywhere on how to do it but there is no answer that fits what i need.

  • To run admin command prompt on any account
  • To not need an admin password

I want my c++ program to open up the command prompt as administrator so the user can run admin commands (the use of this will not be displayed in this post!)

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Possible duplicate of [C++ Console Application prompt "Run as Admin" to run as administrator?](http://stackoverflow.com/questions/8139480/c-console-application-prompt-run-as-admin-to-run-as-administrator) – Random Davis Jan 03 '17 at 21:19
  • @RandomDavis so how do i actually OPEN the command prompt –  Jan 03 '17 at 21:21
  • I assume you're referring to the UAC prompt when you say "command prompt". If so, it would open automatically upon launching your application if you follow the steps in that question I linked. If you want to open it while your application is running at some point, it appears that that might not be possible according to the answers to [this question](http://stackoverflow.com/questions/6418791/requesting-administrator-privileges-at-run-time). – Random Davis Jan 03 '17 at 21:26
  • What operating system are you using? – Robᵩ Jan 03 '17 at 21:27
  • @Robᵩ obviously windows –  Jan 03 '17 at 21:29
  • now i have the UAC and getting admin working. how do i get the command prompt window open from the c++ program? –  Jan 03 '17 at 21:30
  • open the C++ program as admin: if it is the executable then right-click it and open as administrator, if your are on compiler then close it then open it again as adminitrator – Raindrop7 Jan 03 '17 at 21:30
  • 1
    @ Kid8 - Search for `ShellExecute` or `CreateProcess` as ways to exec `cmd.exe` – Unimportant Jan 03 '17 at 21:34
  • @Raindrop7 I've done that already but how do i actually open the command window? do i do system("cmd.exe") or ExecuteShell(SOMETHING) ? i am currently stuck on how to actually open the command window –  Jan 03 '17 at 21:34
  • `ShellExecute()` is enough in your case – Raindrop7 Jan 03 '17 at 21:37
  • 2
    ITT: nobody seems to know what a command prompt is – M.M Jan 03 '17 at 22:19

1 Answers1

1

If you use system you can use:

system("runas /user:<admin-user> \"program.exe\"");

Or ShellExecute function:

ShellExecute(hwnd, "runas", "program.exe", 0, 0, SW_SHOWNORMAL);

look at Msdn: Shell Execute function

Mara Black
  • 1,666
  • 18
  • 23