The function launches the application asking for administrator rights. As if it were launched from the context menu of the file - "Run as administrator".
1 2 3 4 5 6 7 8 9 10 11 12 |
Uses Winapi.ShellAPI; ... var app, params: WideString; begin app:= 'C:\Windows\notepad.exe'; params:= 'C:\Windows\System32\drivers\etc\hosts'; runAsAdmin(0, app, params); end; |
Options
app - path to the application
params - parameters (keys) with which to start the application, if required
Result
True if the application was started or false if not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function runAsAdmin(hWnd: HWND; filename: string; params: string): Boolean; var sei: TShellExecuteInfo; begin ZeroMemory(@sei, SizeOf(sei)); sei.cbSize := SizeOf(TShellExecuteInfo); sei.Wnd := hwnd; sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI; sei.lpVerb := PChar('runas'); sei.lpFile := PChar(Filename); if params <> '' then sei.lpParameters := PChar(params); sei.nShow := SW_SHOWNORMAL; Result := ShellExecuteEx(@sei); end; |