Recent

Author Topic: [SOLVED]How run elevated command?  (Read 251 times)

VinDerKlaus

  • New Member
  • *
  • Posts: 27
[SOLVED]How run elevated command?
« on: February 18, 2020, 02:23:27 pm »
Hello friends,

I using Windows and trying to run command as administrator. I try this, but it give no output (uses ShellApi):

Code: Pascal  [Select][+][-]
  1. ShellExecute(0, 'runas', 'cmd.exe echo "FirstLine" > "C:\Windows\textfile.txt"', nil, nil, SW_SHOWNORMAL);

This command give output if I run in cmd.exe with elevated privelages, but not from my program?

I need execute as admin without UAC prompt (because user will be administrator anyway).

Does anyone know solution?
« Last Edit: February 18, 2020, 08:08:42 pm by VinDerKlaus »

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8385
  • FPC developer.
Re: How run elevated command?
« Reply #1 on: February 18, 2020, 02:38:56 pm »
Seems to be perfectly normal. Searching for "windows runas redirection" has as first hit:

https://stackoverflow.com/questions/1462644/win7-runas-command-how-to-capture-output-of-command-that-is-run

VinDerKlaus

  • New Member
  • *
  • Posts: 27
Re: How run elevated command?
« Reply #2 on: February 18, 2020, 03:40:47 pm »
I tries a solution to run application with admin from wiki, but this didn't create file either (by the way, creating file isn't my end-goal, it just a way to see if it work). Here is code:

Code: Pascal  [Select][+][-]
  1.   function RunAsAdmin(const Handle: Hwnd; const Path, Params: string): Boolean;
  2.   var
  3.     sei: TShellExecuteInfoA;
  4.   begin
  5.     FillChar(sei, SizeOf(sei), 0);
  6.     sei.cbSize := SizeOf(sei);
  7.     sei.Wnd := Handle;
  8.     sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
  9.     sei.lpVerb := 'runas';
  10.     sei.lpFile := PAnsiChar(Path);
  11.     sei.lpParameters := PAnsiChar(Params);
  12.     sei.nShow := SW_SHOWNORMAL;
  13.     Result := ShellExecuteExA(@sei);
  14.   end;  
  15.  
  16. RunAsAdmin(0, 'cmd.exe echo "FirstLine" > "C:\Windows\textfile.txt"', '');

But file does not create?

How to run cmd command with admin from program? Thank.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 774
    • Lebeau Software
Re: [SOLVED]How run elevated command?
« Reply #3 on: February 18, 2020, 09:04:23 pm »
I need execute as admin without UAC prompt (because user will be administrator anyway).

Sorry, but you can't spawn an elevated process without a UAC prompt appearing, unless you are already running elevated to begin with.  Even admin users have to be prompted to elevate.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018