Search code examples
c#.net-4.0setup-project

Stop installation based on criteria


I am trying to check if a computer has the VC++ redistributable installed and the best way I have found to check for it is by running the following code:

bool CheckForVCRedist()
{
    bool install = false;

    if (!install) install = 5 == MsiQueryProductState("{196BB40D-1578-3D01-B289-BEFC77A11A1E}");
    if (!install) install = 5 == MsiQueryProductState("{DA5E371C-6333-3D8A-93A4-6FD5B20BCC6E}");
    if (!install) install = 5 == MsiQueryProductState("{C1A35166-4301-38E9-BA67-02823AD72A1B}");
    if (!install) install = 5 == MsiQueryProductState("{F0C3E5D1-1ADE-321E-8167-68EF0DE699A5}");
    if (!install) install = 5 == MsiQueryProductState("{1D8E6291-B0D5-35EC-8441-6616F567A0F7}");
    if (!install) install = 5 == MsiQueryProductState("{88C73C1C-2DE5-3B01-AFB8-B46EF4AB41CD}");

    return install;
}

[DllImport("msi.dll")]
private static extern int MsiQueryProductState(string product);

If any of the following are true, then my program will run correctly. I am trying to arrange it so that the installer stops based on the presence of the VC++ Redistributable. In the program installer cs file there is the following code:

protected override void OnBeforeInstall(IDictionary savedState)
{
    if (CheckForVCRedist())
    {
        base.OnBeforeInstall(savedState);
    }
    else
    {
        throw new Exception("You are missing the VC ++ 2010 Redistributable. Please follow the link to get it:\nhttp://www.microsoft.com/en-us/download/details.aspx?id=5555");
    }
}

This doesn't seem to work. Any advice?

Edit: I don't have a custom action set up to run this as I thought overriding the method was the correct way to go... I now feel like that is wrong.

Edit[2013-02-28 10:36]: The error is not being thrown in the installer, is there a better way to stop the installer form installing?


Solution

  • You should create a CustomAction and then override Install. Then you can make the setup cancel by throwing InstallException

    public override void Install(System.Collections.IDictionary stateSaver)
    {
        if (CheckForExceptionalCondition())
            throw new InstallException("Some message for user.");
    
        base.Install(savedState);
    }
    

    Then it shows a friendly message box during the installation and cancels the setup.

    enter image description here