【问题标题】:How do you access an already running COM object using .NET Interop?如何使用 .NET Interop 访问已经运行的 COM 对象?
【发布时间】:2009-07-08 08:47:56
【问题描述】:

我有一个应用程序,它有自己的数据库作为 COM 组件。我需要使用 C# 编写一个链接到该 COM 数据库的已运行实例的应用程序,以便我可以 tweek 数据库中的值并查看它们如何影响应用程序。

过去我曾使用 Monikers 编写过类似的应用程序。

是否可以从 .NET 应用程序访问已准备好运行的 COM 组件?

【问题讨论】:

    标签: interop


    【解决方案1】:

    要在 .NET 中使用名字对象,您可能需要查看 Marshal.BindToMoniker,它在内部调用 Win32 BindToMoniker 函数:

    void BindToMoniker()
    {
        string pptxFile;
        PowerPoint.Presentation pptx;
    
        pptx = (PowerPoint.Presentation)Marshal.BindToMoniker(pptxFile);
        pptx.Application.Visible = Office.MsoTriState.msoTrue;
    }
    

    另一种选择是使用 AccessibleObjectFromWindow 从窗口句柄获取 IDispatch 指针(完整示例在 this question 中描述):

    // AccessibleObjectFromWindow gets the IDispatch pointer of an object
    // that supports IAccessible, which allows us to get to the native OM.
    [DllImport("Oleacc.dll")]
    private static extern int AccessibleObjectFromWindow(
        int hwnd, uint dwObjectID,
        byte[] riid,
        ref PowerPoint.DocumentWindow ptr);
    

    Andrew Whitechapel 的以下博客文章(与 MS Office 自动化相关)也可能是有关如何在 .NET 中创建和检索 COM 对象的有用资源:

    Launching Office Apps Programmatically

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-01
      • 2018-07-11
      • 2012-08-14
      • 1970-01-01
      • 2015-02-18
      • 2012-05-01
      • 1970-01-01
      • 2010-10-16
      相关资源
      最近更新 更多