Many people add a “requireAdministrator” manifest to their Application to get elevation in Vista. However this is not needed all the time so there is a second way that allows to elevate even in Windows XP.
The solution is to restart the application with ShellExecute and the “runas” verb. In Vista you’ll get the elevation prompt, on XP (or if UAC is disabled) you’ll get a logon dialog that allows the user to type in Administrator credentials.

The following code executes the same application a second time, but does the trick with “runas”. JwShellExecute raises and exception if anything goes wrong. The exception error constant E_USER_CANCELED_OPERATION reports that the user has canceled the elevation.

  1. uses JwsclElevation,…;
  2.   try
  3.     JwShellExecute(0, //const hWnd: HWND;
  4.         ParamStr(0), //FileName,
  5.         //Parameters,
  6.         , //Directory: TJwString;
  7.         0, //ShowCmd: Integer;
  8.         [sefIgnoreElevationIfNotAvailable]);
  9.   except
  10.     on E : EJwsclWinCallFailedException do
  11.     begin
  12.       if E.LastError = E_USER_CANCELED_OPERATION then
  13.         //runas canceled
  14.       else
  15.       //error catching here
  16.     end;
  17.   end;
Send post as PDF to www.pdf24.org
convert this post to pdf.