Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

windows installer - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:emp.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);
                }
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...