[wix-users] Run as Administrator

Kaveesh Dashora kaveeshd at gmail.com
Tue Sep 6 10:25:50 PDT 2016


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-application-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/
>


More information about the wix-users mailing list