You can check if UAC is active using this function
interface
uses
Registry, SysUtils;
function IsUACActive: Boolean;
implementation
function IsUACActive: Boolean;
var
Reg: TRegistry;
begin
Result := FALSE;
// There's a chance it's active as we're on Vista or Windows 7. Now check the registry
if CheckWin32Version(6, 0) then
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System') then
begin
if (Reg.ValueExists('EnableLUA')) and (Reg.ReadBool('EnableLUA')) then
Result := TRUE;
end;
finally
FreeAndNil(Reg);
end;
end;
end;
You can run an elevated process using the following function:
...
interface
uses
Windows, ShellAPI, Forms;
type
TExecuteFileOption = (
eoHide,
eoWait,
eoElevate
);
TExecuteFileOptions = set of TExecuteFileOption;
function ExecuteFile(Handle: HWND; const Filename, Paramaters: String; Options: TExecuteFileOptions): Integer;
implementation
function ExecuteFile(Handle: HWND; const Filename, Paramaters: String; Options: TExecuteFileOptions): Integer;
var
ShellExecuteInfo: TShellExecuteInfo;
ExitCode: DWORD;
begin
Result := -1;
ZeroMemory(@ShellExecuteInfo, SizeOf(ShellExecuteInfo));
ShellExecuteInfo.cbSize := SizeOf(TShellExecuteInfo);
ShellExecuteInfo.Wnd := Handle;
ShellExecuteInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
if (eoElevate in Options) and (IsUACActive) then
ShellExecuteInfo.lpVerb := PChar('runas');
ShellExecuteInfo.lpFile := PChar(Filename);
if Paramaters <> '' then
ShellExecuteInfo.lpParameters := PChar(Paramaters);
// Show or hide the window
if eoHide in Options then
ShellExecuteInfo.nShow := SW_HIDE
else
ShellExecuteInfo.nShow := SW_SHOWNORMAL;
if ShellExecuteEx(@ShellExecuteInfo) then
Result := 0;
if (Result = 0) and (eoWait in Options) then
begin
GetExitCodeProcess(ShellExecuteInfo.hProcess, ExitCode);
while (ExitCode = STILL_ACTIVE) and
(not Application.Terminated) do
begin
sleep(50);
GetExitCodeProcess(ShellExecuteInfo.hProcess, ExitCode);
end;
Result := ExitCode;
end;
end;
To run an elevated, hidden process and wait for it to exit:
ExecuteFile(Self.Handle, 'Filename', 'Parameters', [eoHide, eoWait, eoElevate]);
Hope this helps