.NET 互操作 : Find All instances of of a running COM object with C#

标签 .net interop automation ms-office com-interop

背景

我正在通过命令行工具自动化一些 Office 应用程序(Word 和 PowerPoint)。

我的工具需要做的一件事是找到所有正在运行的 Word 实例。

我知道如何获得对其中一个实例的引用...

Object running_obj = null;
{
    running_obj = System.Runtime.InteropServices.Marshal.GetActiveObject(progid);
}
catch (System.Exception)
{
    //failed to find the object;
}
if (running_obj!=null)
{
   var running_obj_type = System.Type.GetTypeFromProgID(progid);
   Microsoft.Office.Interop.Word.Application running_obj_wrapper;
   running_obj_wrapper = 
            (Microsoft.Office.Interop.Word.Application)
            System.Runtime.InteropServices.Marshal.CreateWrapperOfType(
                  running_obj, running_obj_type);
}

我的问题

如何找到我正在寻找的应用程序的所有实例,而不仅仅是其中之一。

注意:虽然我的具体问题是关于 Office 应用程序的,但我也对更一般的答案感兴趣。

最佳答案

没试过。但它看起来是正确的解决方案。来自 Oliver Bock blog .

关于.NET 互操作 : Find All instances of of a running COM object with C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/393167/

相关文章:

c# - 用委托(delegate)或 lambda 包装秒表计时?

c# - 什么是好的唯一 PC 标识符?

c# - 压缩/解压缩文件夹和文件

jquery - ASP.NET - 将文件上传到服务器 - jQuery 与 .NET 类

javascript - 自动 console.log 脚本中所有变量的名称和值

C# P/调用 : How to achieve double indirection for a field of a structured parameter

com - 从托管 C++ (C++/CLI) 生成 COM 可见程序集

c# - 如何将 struct 从 C# managed 传递到 C++ unmanaged DLL 并获得 struct 结果?

java - 如何在 maven-surefire-plugin 中设置并行测试

java - 如何管理大型项目验收测试的测试数据治具?