Question

I'm trying to get some information from an msi file

I used:

Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
object installerInstance = installerType.CreateInstance(installerType);

i'm well aware of the option to add reference to the file C:\windows\system32\msi.dll, and cast installerInstance to WindowsInstaller.Install, but since my application will run on many different operating systems (xp, 2003, vista, 7, 2008) and processors (x86 - x64), I want to dynamically use the instance.

Problem is that I can't reach the underlying "WindowsInstaller.Installer" type, only System.__ComObject methods are visible and executable.

How can I dynamically invoke methods, such as "OpenDatabase" etc... from the underlying object?

Was it helpful?

Solution

You need to use reflection to invoke methods. Here's an example invoking the Run method of Windows Script Host:

// obtain the COM type:
Type type = Type.GetTypeFromProgID("WScript.Shell");
// create an instance of the COM type
object instance = Activator.CreateInstance(type);
// Invoke the Run method on this instance by passing an argument
type.InvokeMember(
    "Run", 
    BindingFlags.InvokeMethod, 
    null, 
    instance, 
    new[] { @"c:\windows\notepad.exe" }
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top