Run a Program with Admin Privileges in Delphi

Run an application with Admin privileges:

function RunAsAdmin(hWnd: HWND; sFile: String; sParams: String): BOOL;
var
ShellExecInfo: TShellExecuteInfo;
begin
Result := False;
if not FileExists(sFile) then Exit;
FillChar(ShellExecInfo, SizeOf(ShellExecInfo), 0);
ShellExecInfo.cbSize := sizeof(ShellExecInfo);
ShellExecInfo.Wnd := hWnd;
ShellExecInfo.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
ShellExecInfo.lpVerb := 'runas';
ShellExecInfo.lpFile := PChar(sFile);
ShellExecInfo.lpParameters := PChar(sParams);
ShellExecInfo.nShow := SW_SHOWNORMAL;
Result := ShellExecuteEx(@ShellExecInfo);
end;

Exaple usage:

RunAsAdmin(0, 'C:\Path\To\Program.exe', '-param1 -param2');

More Related Posts