Inno Setup怎样创建一个自动申请管理员身份运行的快捷

时间:2022-11-08 03:42:12

如果你使用的是 Unicode 版本的 Inno Setup,那么以下是更为专业的解决方法。
    这是 mlaan 提及的再一种方法。

QUOTE(     CodeAutomation3.iss)

; -- CodeAutomation2.iss --
    ;
    ; This script shows how to use IUnknown based COM Automation objects.
    ;
    ; REQUIRES UNICODE INNO SETUP!
    ;
    ; Note: some unneeded interface functions which had special types have been     replaced
    ; by dummies to avoid having to define those types. Do not remove these     dummies as
    ; that would change the function indices which is bad. Also, not all     function
    ; protoypes have been tested, only those used by this example.
   
    [Setup]
   
AppName=My Program
    AppVerName=My Program version 1.5
    CreateAppDir=no
    DisableProgramGroupPage=yes
    DefaultGroupName=My Program
    UninstallDisplayIcon={app}\MyProg.exe
    OutputDir=userdocs:Inno Setup Examples Output
   
    [Code]
   
   
{---     IShellLink ---}
   
    const
    CLSID_ShellLink     = '{00021401-0000-0000-C000-000000000046}';
    SLDF_RUNAS_USER = $2000;
   
    type
    IShellLinkW = interface(IUnknown)
      '{000214F9-0000-0000-C000-000000000046}'
      procedure Dummy;
      procedure Dummy2;
      procedure Dummy3;
      function GetDescription(pszName: String; cchMaxName: Integer): HResult;
      function SetDescription(pszName: String): HResult;
      function GetWorkingDirectory(pszDir: String; cchMaxPath: Integer): HResult;
      function SetWorkingDirectory(pszDir: String): HResult;
      function GetArguments(pszArgs: String; cchMaxPath: Integer): HResult;
      function SetArguments(pszArgs: String): HResult;
      function GetHotkey(var pwHotkey: Word): HResult;
      function SetHotkey(wHotkey: Word): HResult;
      function GetShowCmd(out piShowCmd: Integer): HResult;
      function SetShowCmd(iShowCmd: Integer):     HResult;
      function GetIconLocation(pszIconPath: String; cchIconPath: Integer;
        out     piIcon:     Integer): HResult;
      function SetIconLocation(pszIconPath: String; iIcon: Integer): HResult;
      function SetRelativePath(pszPathRel: String; dwReserved: DWORD): HResult;
      function Resolve(Wnd: HWND; fFlags: DWORD):     HResult;
      function SetPath(pszFile: String): HResult;
    end;
   
    IPersist = interface(IUnknown)
      '{0000010C-0000-0000-C000-000000000046}'
      function GetClassID(var classID: TGUID): HResult;
    end;
   
    IPersistFile = interface(IPersist)
      '{0000010B-0000-0000-C000-000000000046}'
      function IsDirty: HResult;
      function Load(pszFileName: String; dwMode: Longint): HResult;
      function Save(pszFileName: String; fRemember: BOOL): HResult;
      function SaveCompleted(pszFileName: String): HResult;
      function GetCurFile(out pszFileName: String): HResult;
      function GetFlags(out Flags: DWORD): HResult;
      function SetFlags(Flags: DWORD): HResult;
    end;
   
    IShellLinkDataList = interface(IUnknown)
      '{45E2B4AE-B1C3-11D0-B92F-00A0C90312E1}'
      function AddDataBlock(pDataBlock : DWORD) :     HResult;
      function CopyDataBlock(dwSig : DWORD; var ppDataBlock : DWORD) : HResult;
      function RemoveDataBlock(dwSig : DWORD) :     HResult;
      function GetFlags(var pdwFlags : DWORD) : HResult;
      function SetFlags(dwFlags : DWORD) : HResult;
    end;
   
    procedure IShellLinkButtonOnClick(Sender:     TObject);
    var
    Obj: IUnknown;
    SL: IShellLinkW;
    PF: IPersistFile;
    SDL: IShellLinkDataList;
    Flags: DWord;
    begin
    { Create the     main ShellLink COM Automation object }
    Obj :=     CreateComObject(StringToGuid(CLSID_ShellLink));
   
    { Set the     shortcut properties }
    SL :=     IShellLinkW(Obj);
    OleCheck(SL.SetPath(ExpandConstant('{srcexe}')));
    OleCheck(SL.SetArguments(''));
    OleCheck(SL.SetShowCmd(SW_SHOWNORMAL));
   
    // 设置 以其他用户身份运行
    Flags := 0;
    SDL := IShellLinkDataList(Obj);
    OleCheck(SDL.GetFlags(Flags));
    OleCheck(SDL.SetFlags(Flags or SLDF_RUNAS_USER));
   
    { Save the     shortcut }
    PF :=     IPersistFile(Obj);
    OleCheck(PF.Save(ExpandConstant('{commondesktop}\CodeAutomation2 Test.lnk'), True));
   
    MsgBox('Saved a     shortcut named ''CodeAutomation2 Test'' on the common desktop.', mbInformation, mb_Ok);
    end;
   
    {---}
   
    procedure CreateButton(ALeft, ATop: Integer; ACaption:     String; ANotifyEvent: TNotifyEvent);
    begin
    with TButton.Create(WizardForm)     do begin
      Left := ALeft;
      Top := ATop;
      Width := (WizardForm.CancelButton.Width*3)/2;
      Height := WizardForm.CancelButton.Height;
      Caption := ACaption;
      OnClick := ANotifyEvent;
      Parent := WizardForm.WelcomePage;
    end;
    end;
   
    procedure InitializeWizard();
    var
    Left, LeftInc,     Top, TopInc: Integer;
    begin
    Left :=     WizardForm.WelcomeLabel2.Left;
    LeftInc := (WizardForm.CancelButton.Width*3)/2 + ScaleX(8);
    TopInc := WizardForm.CancelButton.Height + ScaleY(8);
    Top := WizardForm.WelcomeLabel2.Top + WizardForm.WelcomeLabel2.Height -     4*TopInc;
   
    CreateButton(Left, Top, '&IShellLink...', @IShellLinkButtonOnClick);
    end;