[wix-users] Run as Administrator

Hoover, Jacob Jacob.Hoover at greenheck.com
Tue Sep 6 10:48:44 PDT 2016


Please note, running your BA elevated is against best practices and it may result in fallout which won't be supported.  You should really ask yourself why you think your BA needs to run elevated, as 99.99% of the time the reasons are invalid.  Your BA should never modify machine state, that's the engine/installers job, and that is what WiX has been designed to do.


-----Original Message-----
From: wix-users [mailto:wix-users-bounces at lists.wixtoolset.org] On Behalf Of Kaveesh Dashora
Sent: Tuesday, September 06, 2016 12:26 PM
To: WiX Toolset Users Mailing List <wix-users at lists.wixtoolset.org>
Cc: wix-users-bounces at lists.wixtoolset.org
Subject: Re: [wix-users] Run as Administrator

Below is the code which will help you in creating a Self-Elevating MSI

Step 1 Create a custom action in your MSI project, Say - [ElevateInstallerCA]

public static ActionResult ElevateInstaller(Session session) {
   WindowsPrincipal principal = new
WindowsPrincipal(WindowsIdentity.GetCurrent());
   bool hasAdministrativeRight =
principal.IsInRole(WindowsBuiltInRole.Administrator);
   if (!hasAdministrativeRight)
   {
       try
       {
           ProcessStartInfo processInfo = new ProcessStartInfo();
           processInfo.Verb = "runas";
   processInfo.FileName = "msiexec";
                    processInfo.Arguments = "/i \"" + session["OriginalDatabase"] + "\"";

           using (Process wrapperProcess = Process.Start(processInfo))
           {
               wrapperProcess.WaitForExit();
           }
           session["ElevatedFromCA"] = "1";
           return ActionResult.SkipRemainingActions;
       }
       catch
       {
           //Exit Gracefully, Probably the user canceled the UAC window
           return ActionResult.UserExit;
       }
   }
   else
   {
        return ActionResult.Success;
   }
}

In your wix project where you have defined your custom actions define this custom action:

<Binary Id="ElevateInstallerBin"
SourceFile="..\ElevateInstaller\bin\Release\ElevateInstallerCustomAction.CA.dll"
/>
<CustomAction Id="ElevateInstallerCA" BinaryKey="ElevateInstallerBin"
DllEntry="ElevateInstaller" Execute="immediate" Return="check" />

Change the InstallUISequence such that this custom action is run the first when the MSI begins execution

<InstallUISequence>
     <Custom Action="ElevateInstallerCA"
Before="FindRelatedProducts"></Custom>
</InstallUISequence>

Hope this helps,

Let me know if you have any questions on this.

Regards,
Kaveesh

On Tue, 6 Sep 2016, 10:26 p.m. Joel Budreau, <joel.budreau at gmail.com> wrote:

>
> http://superuser.com/questions/604927/how-do-i-configure-my-applicatio
> n-to-run-as-administrator-automatically
>
> On Mon, Sep 5, 2016 at 11:43 PM, Kunal Kalbande < 
> kunal.kalbande at synerzip.com
> > wrote:
>
> > Hi
> >
> >
> >
> > I also need to run my wix installer in admin mode. To be clear, when 
> > I click on application exe, it should prompt me to run as 
> > administrator, also the icon should have shield symbol on it.
> >
> >
> >
> > Appreciate your help.
> >
> >
> >
> > Thanks
> >
> > Kunal
> >
> >
> > --
> > This e-mail, including any attached files, may contain confidential 
> > and privileged information for the sole use of the intended 
> > recipient. Any review, use, distribution, or disclosure by others is 
> > strictly
> prohibited.
> > If you are not the intended recipient (or authorized to receive
> information
> > for the intended recipient), please contact the sender by reply 
> > e-mail
> and
> > delete all copies of this message.
> >
> >
> > ____________________________________________________________________
> > WiX Toolset Users Mailing List provided by FireGiant 
> > http://www.firegiant.com/
> >
>
> ____________________________________________________________________
> WiX Toolset Users Mailing List provided by FireGiant 
> http://www.firegiant.com/
>

____________________________________________________________________
WiX Toolset Users Mailing List provided by FireGiant http://www.firegiant.com/



More information about the wix-users mailing list