Skip to content

Commit

Permalink
- Added sample for restarting UI setup elevated. Triggered by #384 and
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-shilo committed Jun 5, 2018
1 parent 7ad81ca commit 90111a5
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 1 deletion.
66 changes: 65 additions & 1 deletion Source/src/WixSharp.Samples/Support/testpad/setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using System.Windows.Forms;
using System.Xml.Linq;
Expand All @@ -26,6 +28,30 @@ public static ActionResult MyAction(Session session)
return ActionResult.Success;
}

[CustomAction]
public static ActionResult CheckIfAdmin(Session session)
{
if (!new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
{
MessageBox.Show(session.GetMainWindow(), "You must start the msi file as admin");

var startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = "msiexec.exe";
startInfo.Arguments = "/i \"" + session.Property("OriginalDatabase") + "\"";
startInfo.Verb = "runas";

Process.Start(startInfo);

return ActionResult.Failure;
}
else
{
return ActionResult.Success;
}
}

public static bool Is64BitProcess
{
get { return IntPtr.Size == 8; }
Expand All @@ -44,6 +70,43 @@ static void prepare_dirs(string root)
}
}

static void Issue_386()
{
var project =
new ManagedProject("ElevatedSetup",
new Dir(@"%ProgramFiles%\My Company\My Product",
new File(@"Files\bin\MyApp.exe")));

project.ManagedUI = ManagedUI.Default;
project.AddAction(new ManagedAction(CustomActions.CheckIfAdmin,
Return.check,
When.Before,
Step.AppSearch,
Condition.NOT_Installed,
Sequence.InstallUISequence));

project.UIInitialized += (SetupEventArgs e) =>
{
if (!new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
{
MessageBox.Show(e.Session.GetMainWindow(), "You must start the msi file as admin", e.ProductName);
e.Result = ActionResult.Failure;
var startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = "msiexec.exe";
startInfo.Arguments = "/i \"" + e.MsiFile + "\"";
startInfo.Verb = "runas";
Process.Start(startInfo);
}
};

// project.PreserveTempFiles = true;
Compiler.BuildMsi(project);
}

static void Issue_374()
{
string inDir = @"C:\temp\wixIn\";
Expand Down Expand Up @@ -79,7 +142,7 @@ static void Issue_378()
// project.DefaultFeature = mainFeature;
project.PreserveTempFiles = true;
project.GUID = new Guid("6fe30b47-2577-43ad-9095-1861ba25889c");
project.BuildMsi();
project.BuildMsi();
}

static void Issue_298()
Expand Down Expand Up @@ -124,6 +187,7 @@ static void Issue_298b()

static public void Main(string[] args)
{
Issue_386(); return;
Issue_378(); return;
Issue_374(); return;
Issue_298(); return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
..\..\cscs.exe setup.cs
pause
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This sample demonstrates how to ensure that setup runs elevated UI.
Normally MSI elevates the actual installation stage (InstallSequence) while leaving the UI interaction (InstallUISequence) unelevated. This sample shows how to elevate the whole round trip.
Note: the ManagedUI gives you the best user experience as native UI does not allow full control over dialogs (the initial setup exit dialog needs to be closed manually).
Execute corresponding .cmd file to build desired msi. Then execute the .msi to start the installation.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//css_dir ..\..\;
//css_ref Wix_bin\SDK\Microsoft.Deployment.WindowsInstaller.dll;
//css_ref WixSharp.UI.dll;
using System;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security.Principal;
using Microsoft.Deployment.WindowsInstaller;
using WixSharp;
using WixSharp.Forms;

class Script
{
static void Main()
{
var project =
new ManagedProject("ElevatedSetupUI",
new Dir(@"%ProgramFiles%\My Company\My Product",
new File("readme.txt")));

project.ManagedUI = ManagedUI.Default;
project.UIInitialized += (SetupEventArgs e) =>
{
if (!new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
{
MessageBox.Show(e.Session.GetMainWindow(), "You must start the msi file as admin", e.ProductName);
e.Result = ActionResult.Failure;
var startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = "msiexec.exe";
startInfo.Arguments = "/i \"" + e.MsiFile + "\"";
startInfo.Verb = "runas";
Process.Start(startInfo);
}
};

// For native UI you will need to add managed action implementing restart logic as above
// project.AddAction(new ManagedAction(CustomActions.RestartIfNotAdmin,
// Return.check,
// When.Before,
// Step.AppSearch,
// Condition.NOT_Installed,
// Sequence.InstallUISequence));

Compiler.BuildMsi(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
start msiexec /uninstall ElevatedSetupUI.msi
Binary file modified Source/src/WixSharp.Samples/WixSharp.Msi.dll
Binary file not shown.

0 comments on commit 90111a5

Please sign in to comment.