Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running setup in elevated mode #1723

Open
abhijeet-vidhate-spider opened this issue Jan 13, 2025 · 3 comments
Open

Running setup in elevated mode #1723

abhijeet-vidhate-spider opened this issue Jan 13, 2025 · 3 comments

Comments

@abhijeet-vidhate-spider

Hello,

I have a product for which I had written setup using WinSharp 3, wix toolkit 3 and .net framework 4.7.2. In this setup code, I had to use project.InstallPrivileges = InstallPrivileges.elevated as my code is doing some system level changes.

Now I am upgrading the product to use .net 8, setup to use WixSharp core and wix 5.0.2. With these packages, an error is thrown on the above line stating it is deprecated. If I comment this line, my new setup runs in elevated mode, but I have upgrade issues i.e. duplicate entries in add/remove programs, duplicate shortcuts, etc. If I set project.Scope = InstallScope.perUserOrMachine or InstallScope.perUser, this solves my upgrade issue, but the setup ops that needed elevated permissions fail with access denied error. I have explicitly run the setup as admin for those ops to complete.

Is there a substitute to project.InstallPrivileges = InstallPrivileges.elevated in the new version that will help me avoid explicit run as admin?

Regards,
Abhijeet

@Torchok19081986
Copy link

moin, in samples folder, exists example for runs as admin. I added this to setup project, whereever user dont has permissoions to install , it starts package with runas and execute your package as user with elevated permissons.

Try it in this way.

Code

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);
            }
        };

@abhijeet-vidhate-spider
Copy link
Author

Thanks, @Torchok19081986, for the suggestion. I am not able to use the UIInitialized event handler as I am not using ManagedUI. I tried using Load event, but that occurs way too late in the execution of first run. So instead, I am experimenting with a possibility of converting the event handler above into a console app which will accept the msi file name as a parameter. I will post an update here on how it goes.

@oleg-shilo
Copy link
Owner

Have a look at Selt-executable_MSI sample. It might be the direct answer to the problem you are trying to solve:

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants