Enhanced MSI Analyzer in .NET using C# and Windows Forms

Enhanced MSI Analyzer in .NET using C# and Windows Forms

 

Most of us are quite familiar in creating MSI or Setup for our applications. By using a MSI, we can make sure all dependencies for running the application will be placed properly. MSI is the best option available on Windows OS for packaging and distributing our application. For .NET developers, Visual Studio presents lot of features in creating setup and deployment projects for our application. But, there are no built-in tools in Visual Studio to look into the MSI contents. And that too, to make a small change in MSI, it requires rebuilding the entire Setup Project. So, I think it's better to design an application that will analyze the MSI and give the details of it along with capability of updating it without rebuilding.

 

So, I design this application in VS.NET 2008 using C# and windows forms. I will explain features provided by this application, followed by its design and coding.

 

Features present in this application:

  • It allows us to look into the contents of the MSI.
  • It allows us to export the contents of the MSI.
  • It allows us to update commonly changing properties without rebuilding it.
  • Easy to use UI.
  • Now, Updating an MSI is quite simple.

Outline of the MSI Database:

 

In order to access MSI database in .NET, we will follow below steps:

 

  • Create an instance of Installer, Database, View and Record.
  • Open the database using Installer's OpenDatabase method.
  • Prepare a Select query which is having syntax similar to SQL.
  • Pass that query to OpenView method of the Database object.
  • Execute the query by calling Execute method of the View object.
  • Fetch the records by using Fetch method of the View object.
  • If we want to update, delete, insert data into the MSI tables, we can follow the same steps by passing the update, insert SQL syntax query instead of Select query.
  • MSI database won't support all operators of SQL.

 

 

Now, create a new Windows Application using C# in VS.NET 2008 and name it as MSI Analyzer. Add controls to Main Form (frmMSI) as shown below:

 

Add a reference to COM based MSI.dll present in system32 folder of your machine.

MsiImg1.jpg

 

Now, I will outline functionality of few main controls in the application:

 

·         openMSIdialog: This control is used to select the MSI file.

·         MSInotifyIcon:  This control is used the show/hide the application.

·         recentMSIList:   This control is used to maintain the list of MSIs opened recently.

·         statusStrip1:  This control is used to show count of tables/rows present in the MSI database.

menuStrip1 (Menu Strip):

 This Control is having Main Menu with following items in it:

 

·         Browse: To select the MSI.

·         Analyze: To start analysis of the selected MSI.

·         Export Tables: To export the selected tables present in the MSI database.

·         View Important Properties: To look and modify commonly changing properties like target location, version etc.

·         Commit Changes: To commit the changes made to the MSI.

·         Exit: To exit the application.

 

I had placed a SplitContainer control on the main form. One of the panel is used to show list of tables present in MSI database. And the other panel is used to show the records present in the selected table in a GridView.

 

Whenever we create an MSI, it will internally maintain a database with all the details of it like version, files, author etc. The main functionality of this application is to list all tables present in the MSI database along with updating capability.

 

We will create another form, which will allow us to export the MSI contents to text files for future reference as shown below:

 

MsiImg2.jpg

 

This form will list all the tables present in the selected MSI file. We can select which all tables are needed to be exported.

 

We will create last but not least form, which will allow us to view and update commonly changing properties as shown below:

 

MsiImg3.jpg

 

MsiImg4.jpg

 

 

Here, I placed a Tab panel having two panels in it. One panel will list the properties like Name, Author, files and the other panel will list all the dialogs (like Welcome Screen, Progress Screen etc) of the MSI.

 

By this, we completed Design part. Let's start with the coding details:

 

Functionality present in MSIItems.cs:

 

In order to create buttons for each table present in the selected MSI, I implemented concept of control array of VB here. Here, I just created a class named MSIItems which inherits from System.Collections.CollectionBase. And in constructor of this class, I am passing container (Panel) for this buttons. Than, I added a method to implement add new button (table) to the Panel.

 

Functionality present in frmMSI.cs:

 

After selecting the MSI file by using Browse menuitem. When we click on Analyze menuitem, it will perform following steps:

 

  • Opens the MSI database by using OpenDatabase of the Installer object.
  • It will get the list of tables present in the MSI database from "_Tables".
  • It will create a button in the panel1 for each table of the database.

 

When we click on a table button, it will perform following steps:

 

·         Get the columns details of the selected table using "_Columns".

·         Creates a DataTable and adds the columns to it based on the column details.

·         Creates a DataRow for each record present in the selected table and adds it to the DataTable.

·         Binds the DataTable to the GridView (dgContents).

 

Functionality present in frmExport.cs:

 

This will list all the tables of the MSI. We can select the tables that are needed to be exported. We are using Export method of the MSI database object to store in a text file format.

 

Functionality present in frmImpProperties.cs:

 

This will list commonly changing properties like name, version, dialog buttons etc.

It will load the files present in the MSI. Later, it will load all the dialogs present in the MSI by using "Control" table of the MSI. Here, we can update the properties of all dialogs like caption, height and width of the buttons like Next, Back etc.

 

After making changes to any of the Dialog properties, we have to click on Update button of the corresponding row to commit those changes.

 

Finally, some code is added to include an Update button, highlight the edited row in Dialog grid, list recently opened MSIs etc. And, the final output will be like this:

MsiImg5.jpg

 

 

MsiImg6.jpg

 

A small sample of the MSI changed by the application:

 

 MsiImg7.jpg

 

 

MsiImg8.jpg

 

Like this, we can change the dialog properties, version, author etc without rebuilding it. This tool will be really useful, if we are customizing the same MSI for different clients.

 

We can still enhance this application by improving UI, adding extra functionality like adding, updating files present in the MSI.

 

By using this application, we can easily view and update the MSI without any extra effort. I am attaching source code for further reference. I hope this code will be useful for all.

  


Similar Articles