Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating msi transform using c#

I am creating a cutomization software which will do all the standardization to a mst file. Below is the code of class that will change product name and genrate transform.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsInstaller;
using System.Data;

namespace Automation
{
    class CustomInstaller
    {
        public CustomInstaller()
        {
        }
        public Record getInstaller(string msiFile,MsiOpenDatabaseMode mode,string query)
        {
            Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
            Installer inst = (Installer)Activator.CreateInstance(type);
            Database db = inst.OpenDatabase(msiFile, mode);
            WindowsInstaller.View view = db.OpenView(query);
            view.Execute(null); 
            Record record = view.Fetch();
            db.Commit();
            return record;

        }
        public bool generateTrans(string file1, string file2,string transName)
        {
            Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
            Installer inst = (Installer)Activator.CreateInstance(type);
            Database db1 = inst.OpenDatabase(file1, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);

            try
            {
                Database db2 = inst.OpenDatabase(file2, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);
                return db2.GenerateTransform(db1, transName);

            }
            catch (Exception e) { }
            return false;
        }
        public int editTransform(string msiFile, MsiOpenDatabaseMode mode, string query)
        {
            Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
            Installer inst = (Installer)Activator.CreateInstance(type);
            Database db = inst.OpenDatabase(msiFile, mode);
            WindowsInstaller.View view = db.OpenView(query);
            view.Execute(null);
            db.Commit();
            int o=(int)db.DatabaseState;
            db = null;
            inst = null;
            type = null;
            return 1;
        }
    }
}

First editTransform() is called which will create a copy of original msi and do some changes in it, then generateTrans() is called which will get difference detween two msi files and create a transform file. Now issue is when genrateTrans() is called, then it goes to catch block of it as inst.OpenDatabase return "MSI Api Error". It seems to me that the copy of file crated by editTransform is still locked by it and is not available for use for generateTrans() menthod. Please help here.

PS: mode used for edit transform is transact.

like image 790
Harshdeep Singh Avatar asked Dec 27 '22 04:12

Harshdeep Singh


1 Answers

Instead of doing the COM Interop, checkout the far superior interop library ( Microsoft.Deployment.WindowsInstaller ) found in Windows Installer XML Deployment Tools Foundation. You'll find it much easier to use.

using System;
using System.IO;
using Microsoft.Deployment.WindowsInstaller;

namespace ConsoleApplication1
{

    class Program
    {
        const string REFERENCEDATABASE = @"C:\orig.msi";
        const string TEMPDATABASE = @"C:\temp.msi";
        const string TRANSFORM = @"c:\foo.mst";

        static void Main(string[] args)
        {
            File.Copy(REFERENCEDATABASE, TEMPDATABASE, true);
            using (var origDatabase = new Database(REFERENCEDATABASE, DatabaseOpenMode.ReadOnly))
            {
                using (var database = new Database(TEMPDATABASE, DatabaseOpenMode.Direct))
                {
                    database.Execute("Update `Property` Set `Property`.`Value` = 'Test' WHERE `Property`.`Property` = 'ProductName'");
                    database.GenerateTransform(origDatabase, TRANSFORM);
                    database.CreateTransformSummaryInfo(origDatabase, TRANSFORM, TransformErrors.None, TransformValidations.None);
                }
            }
        }
    }
}
like image 124
Christopher Painter Avatar answered Jan 14 '23 17:01

Christopher Painter