开发者

Invoking method from the System.__ComObject base type

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 "OpenDa开发者_如何学Pythontabase" etc... from the underlying object?


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" }
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜