Recent

Author Topic: [Solved] Elevated running with XCOPY.exe  (Read 1007 times)

AlexTP

  • Hero Member
  • *****
  • Posts: 2479
    • UVviewsoft
[Solved] Elevated running with XCOPY.exe
« on: April 22, 2024, 07:19:40 am »
Here is the primitive demo which calls RunElevated (in included unit) to copy project1.ico to system folder "C:\Windows\System32\drivers\etc".
It shows the elevation OS window, I press Yes, XCOPY asks for 'F' or 'D' key, I press F, and nothing. File is not copied.
What can I do wrong?

It is needed for the CudaText project to overwrite 'hosts' file when user tries to save it.

Code:
Code: Pascal  [Select][+][-]
  1. unit proc_windows_elevated;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. function RunElevated(const AProgram, AParameters: UnicodeString): boolean;
  8.  
  9. implementation
  10.  
  11. uses
  12.   Windows, ShellAPI, SysUtils, Classes, Forms;
  13.  
  14. function RunElevated(const AProgram, AParameters: UnicodeString): boolean;
  15. var
  16.   sei: TShellExecuteInfoW;
  17. begin
  18.   FillChar(sei, SizeOf(sei), 0);
  19.   sei.cbSize := SizeOf(sei);
  20.   sei.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_UNICODE;
  21.   sei.Wnd := Application.MainForm.Handle;
  22.   sei.lpVerb := 'runas';
  23.   sei.lpFile := PWideChar(AProgram);
  24.   sei.lpParameters := PWideChar(AParameters);
  25.   sei.nShow := SW_SHOW;
  26.  
  27.   Result := ShellExecuteExW(@sei);
  28.   if Result then
  29.   begin
  30.     WaitForSingleObject(sei.hProcess, INFINITE);
  31.     CloseHandle(sei.hProcess);
  32.   end
  33.   else
  34.     Application.MessageBox(
  35.       PChar(Format('ShellExecuteExW failed: %d', [GetLastError])),
  36.       'CudaText');
  37. end;
  38.  
  39. end.
  40.  
« Last Edit: April 22, 2024, 10:03:13 am by AlexTP »

AlexTP

  • Hero Member
  • *****
  • Posts: 2479
    • UVviewsoft
Re: Help with 'evevated running' which runs XCOPY.exe
« Reply #1 on: April 22, 2024, 09:40:28 am »
Found the reason. I used Win32 32-bit target. For C:\Windows paths, I must use 64-bit target.

 

TinyPortal © 2005-2018