Inno Setup desktop shortcut (link) which has “Run as administrator” advanced property set

狂风中的少年 提交于 2019-11-29 08:56:20

First, make sure you have a very good reason to run your application with Administrator privileges. User applications should not need Administrator privileges. If they need it, its usually a sign of a bad design. One common (bad) reason to want an application to run with Administrator privileges, is that the application needs to write to its installation folder.

See Application does not work when installed with Inno Setup


Inno Setup does not natively support creating a shortcut with "Run as Administrator" flag set.

The "Run as Administrator" flag is a bit the .lnk file. See:

You can set the bit using the following code:

[Icons]
Name: "{userdesktop}\My Program"; Filename: "{app}\MyProg.exe"; \
  AfterInstall: SetElevationBit('{userdesktop}\My Program.lnk')

[Code]

procedure SetElevationBit(Filename: string);
var
  Buffer: string;
  Stream: TStream;
begin
  Filename := ExpandConstant(Filename);
  Log('Setting elevation bit for ' + Filename);

  Stream := TFileStream.Create(FileName, fmOpenReadWrite);
  try
    Stream.Seek(21, soFromBeginning);
    SetLength(Buffer, 1);
    Stream.ReadBuffer(Buffer, 1);
    Buffer[1] := Chr(Ord(Buffer[1]) or $20);
    Stream.Seek(-1, soFromCurrent);
    Stream.WriteBuffer(Buffer, 1);
  finally
    Stream.Free;
  end;
end;

Tested on Unicode version of Inno Setup. But it should, even more naturally, work on Ansi version too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!