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:
unit proc_windows_elevated;
{$mode ObjFPC}{$H+}
interface
function RunElevated(const AProgram, AParameters: UnicodeString): boolean;
implementation
uses
Windows, ShellAPI, SysUtils, Classes, Forms;
function RunElevated(const AProgram, AParameters: UnicodeString): boolean;
var
sei: TShellExecuteInfoW;
begin
FillChar(sei, SizeOf(sei), 0);
sei.cbSize := SizeOf(sei);
sei.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_UNICODE;
sei.Wnd := Application.MainForm.Handle;
sei.lpVerb := 'runas';
sei.lpFile := PWideChar(AProgram);
sei.lpParameters := PWideChar(AParameters);
sei.nShow := SW_SHOW;
Result := ShellExecuteExW(@sei);
if Result then
begin
WaitForSingleObject(sei.hProcess, INFINITE);
CloseHandle(sei.hProcess);
end
else
Application.MessageBox(
PChar(Format('ShellExecuteExW failed: %d', [GetLastError])),
'CudaText');
end;
end.