views:

3702

answers:

3

Hi,

I have an msi file that installs an application. I need to know the product name of that application before the installation starts.

I tried the following:

{ 

...
Type type = Type.GetType("Windows.Installer");
WindowsInstaller.Installer installer = (WindowsInstaller.Installer)
Activator.CreateInstance(type);

installer.OpenDatabase(msiFile, 0); //this is my guess to pass in the msi file name...
...
}

but now? Type is null, which throws me an error. And where do I pass in the name of the MSI file?

Thanks for any hints & comments.

+1  A: 

Where did you get the "Windows.Installer" stuff from?

...because:

  1. Type.GetType takes a .NET type name, not a COM ProgId.
  2. Windows Installer (at least on Windows 2003) doesn't have a ProgId.

In summary: Use P/Invoke (DllImport, etc.) to talk to the MSI API.

Roger Lipscombe
Great, I do not know exactly what P/Invoke is; I did not manage to find a runable code on the web, it seems to be a well respected secret. So it could be a solution but currently I more or less end up with some DllImport. Please regard me as a newbie in this matter.
+1  A: 

Wouldn't it be easier to use this code:

Type type = typeof(Windows.Installer);

If you prefer the Type.GetType(String) overload you must include correct assembly name after full path to class, eg.:

Type type = Type.GetType("Windows.Installer, <assembly for MsiInstaller>");

Jozef Izso
Thank you for your answer. I have no preferences, I am just googling the web and hope to find some way to solve my problem. What do you mean with "assembly for MsiInstaller"? I just have the msi file. But the "typeof" method seems not to work, either (type Installer does not exist).
If the "Windows.Installer" is not in mscorlib or in currently executing assembly then you must specificy the name of assembly in Type.GetType(string).There is a typo in typeof(). It should be typeof(WindowsInstaller) without the dot.
Jozef Izso
+3  A: 

You need to use:

        Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");

Here is a sample from some of my code - in my case I get the installer version:

        // Get the type of the Windows Installer object
        Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");

        // Create the Windows Installer object
        Installer installer = (Installer)Activator.CreateInstance(installerType);

        // Open the MSI database in the input file
        Database database = installer.OpenDatabase(inputFile, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);

        // Open a view on the Property table for the version property
        View view = database.OpenView("SELECT * FROM Property WHERE Property = 'ProductVersion'");

        // Execute the view query
        view.Execute(null);

        // Get the record from the view
        Record record = view.Fetch();

        // Get the version from the data
        string version = record.get_StringData(2);
Chris Kaczor