Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use shell execute to run cmd as Admin

Tags:

I need to run cmd on a button click as admin. It works. but I need to run it as an admin. How is this done?

 ShellExecute(Handle, 'open', 'c:\Windows\system32\cmd.exe', nil, nil, SW_SHOWNORMAL) 
like image 672
user1868232 Avatar asked Mar 10 '13 05:03

user1868232


People also ask

How do I Run cmd as administrator?

You can open cmd as an administrator by searching for it in the Windows search bar located in the bottom left corner of the desktop screen. Then, right-click on Command Prompt and select Run as administrator.

How do I escalate as administrator in cmd?

In the search results window, under Programs, right-click on the program cmd.exe. In the pop-up menu, select Run As Administrator. If a User Access Control window appears, log in with a Windows user account that has full Administrator access rights. An Elevated Command Prompt window should now open.

How do I Run a command prompt as administrator without password?

To do so, search for Command Prompt in the Start menu, right-click the Command Prompt shortcut, and select Run as administrator. The Administrator user account is now enabled, although it has no password.


1 Answers

Replace the open verb with the runas as shown below. Anyway, try to avoid path hardcoding:

uses   ShellAPI;  procedure TForm1.Button1Click(Sender: TObject); begin   ShellExecute(Handle, 'runas', 'cmd.exe', nil, nil, SW_SHOWNORMAL); end; 

You can also add to your button the shield icon by setting the ElevationRequired property to True.

like image 132
TLama Avatar answered Oct 04 '22 12:10

TLama