Programmatically installing MSI packages
Asked Answered
H

6

14

I would like to install a given .msi package programmatically from my C# .NET application, preferably with the installation parameters that my application specifies (like the installation path, decline crapware, etc.).

I did some searches, but I haven't really found anything useful. The most promising hit was this topic, but I cannot find any documentation of Microsoft.Deployment.WindowsInstaller or of WindowsInstaller.Installer for that matter.

Hub answered 23/4, 2011 at 14:50 Comment(7)
@David Heffernan: I guess, but would it do what I want it to do?Hub
Well, msiexec is the most common way to invoke installerMaundy
@David Heffernan: I checked out the documentation, and it does appear to allow non-gui installation, however, I cannot see anything that would allow me configure the setup (e.g. set installation path). Any clues to that?Hub
That all depends on the .msi. You'd need to pass settings specific to the particular .msiMaundy
@David Heffernan: how can I do that? Could you please elaborate your idea in an answer, so that I can accept if it proves to be the best solution?Hub
Not really possible since I don't know what options there are in your msi.Maundy
@David Heffernan: okay, thanks for your suggestion, probably the best one I've had so far. msiexec does appear to be the best solution I have. Would you care to write it as a proper answer, so that I can accept it?Hub
C
14

I find the Deployment Tools Foundation project mentioned above to be a solid way to do this from .NET. Having referenced Microsoft.Deployment.WindowsInstaller.dll, use code like this to install a package:

Installer.SetInternalUI(InstallUIOptions.Silent);
Installer.InstallProduct(msiFilename, "ACTION=INSTALL ALLUSERS=2 MSIINSTALLPERUSER=");

The documentation for the .NET wrapper is in a .chm file in the Windows Installer XML installation directory in Program Files. Some parts of that DLL loosely wrap the native Windows APIs so the documentation here can be useful as well, which is how I worked out the string in the above snippet to suit my situation.

Cahoot answered 14/9, 2012 at 1:8 Comment(1)
How to check whether MSI installation is completed or not? Please guide me to achieve this.Lorgnon
O
8

There's a COM object that offers an API for the installer:

First add a reference to COM object "Microsoft Windows Installer Object Library" to your project. Then you can start with the following code:

using System;
using WindowsInstaller;

namespace TestApp
{
    public class InstallerTest
    {
        public static void Install()
        {
            Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
            Installer installer = (Installer)Activator.CreateInstance(type);
            installer.InstallProduct("YourPackage.msi");
        }
    }
}

And there's a documentation about the Installer Object.

Overunder answered 23/4, 2011 at 15:19 Comment(2)
According to the documentation, the InstallProduct will display the GUI wizard to the user. My aim would be to completely automate the installation. Do you have an idea for that?Hub
Have you tried "installer.UILevel = MsiUILevel.msiUILevelNone;"? I've never used it. But it's worth a try.Overunder
S
7

The "Deployment Tools Foundation" project which is a part of the WIX3.5 install contains a .NET wrapper for most (if not all) of the Windows Installer API. Get it by downloading and installing the WiX install: http://wixtoolset.org/ (currently WiX 3.11, updated Aug.2017).

Locate the Microsoft.Deployment.WindowsInstaller.dll file in the %ProgramFiles%\Windows Installer XML v3.??\SDK\ folder. Set a reference in your C# project and try to run the different APIs and see if you get the desired functionality.

I highly recommend using Deployment Tools Foundation over any COM Interop from .NET code.

Spelling answered 24/4, 2011 at 0:3 Comment(2)
Thanks, I'll give it a try, but I was hoping that there was some online documentation that could have given me some hints...Hub
Check the help file at %ProgramFiles%\Windows Installer XML v3.5\doc\DTF.chm. Also check out: wix.tramontana.co.hu/tutorialGlia
J
4

The basic Win32 API (that can be pinvoked if necessary) is MsiInstallProduct. This is where practically all other mentioned APIs and calls will end up.

https://msdn.microsoft.com/en-us/library/aa370315(v=vs.85).aspx

Just pass the full path to the MSI file and your command line (including quiet options etc) and check the result to see if it installed correctly.

Note that there is a simple p/invoke declaration for managed code:

[DllImport("msi.dll", CharSet = CharSet.Auto, SetLastError=true)]

static extern UInt32 MsiInstallProduct(string packagePath, string commandLine);

Jansson answered 24/6, 2015 at 22:13 Comment(0)
M
3

The very simplest solution is to use msiexec to invoke the installer on the .msi.

You can customise the installation using command line settings including setting .msi properties, silent installation etc.

Maundy answered 27/4, 2011 at 2:32 Comment(1)
How can I detect completion of an msi installer? Is Process.WaitForExit sufficient?Gunrunning
N
3

There are two approaches to solving your problem.

The first one as mentioned by @Glytzhkof is to use the Microsoft.Deployment.WindowsInstaller .NET wrapper API. This is some seriously powerful stuff but requires some time to get familiar with. You can get the latest version here (UPDATE: Stein Åsmul 28.12.2018: DTF is now part of the WiX toolkit).

The other approach is to use Transforms (.MST files). Transform files can be generated using Microsoft Orca or InstallShiled. The MSTs contains all the customizations that you need and can be applied on the MSI using this command line:

msiexec /i somemsi.msi TRANSFORMS=somemst.mst /qb

Additionally you can pass parameters directly in the command line:

msiexec /i <somemsi.msi> /qb AGREETOLICENSE=YES INSTALLDIR=C:\Temp
etc...

However, you will need to edit the MSI in an ORCA/InstallShield to determine which parameters are actually used.

The parameters used in the above example are not universal.

The actual installation can be complicated because of the presence of custom actions etc. In fact there is a whole industry that is built around msi customizations. Its called Applications Repackaging.

Natalia answered 18/4, 2012 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.