代码之家  ›  专栏  ›  技术社区  ›  Nissim

正在从系统调用方法。\ uuu ComObject基类型

  •  2
  • Nissim  · 技术社区  · 15 年前

    我正试图从msi文件中获取一些信息

    我用过:

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

    我很清楚可以添加对文件C:\windows\system32\msi.dll的引用,并将installerInstance强制转换为WindowsInstaller.Install,但由于我的应用程序将运行在许多不同的操作系统(xp、2003、vista、7、2008)和处理器(x86-x64)上,因此我希望动态使用该实例。

    问题是我无法访问底层的“WindowsInstaller.Installer”类型,只有System.\u ComObject方法可见且可执行。

    1 回复  |  直到 15 年前
        1
  •  4
  •   Darin Dimitrov    15 年前

    您需要使用反射来调用方法。下面是一个调用 Run 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" }
    );