.NET 互操作:使用 C# 查找正在运行的 COM 对象的所有实例 [英] .NET Interop: Find All instances of of a running COM object with C#

查看:21
本文介绍了.NET 互操作:使用 C# 查找正在运行的 COM 对象的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am automating some Office application (Word and PowerPoint) via command-line tool.

我的工具需要做的一件事是定位所有正在运行的 Word 实例.

One thing my tool needs to do is locate all the running instances of Word.

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

I know how to get a reference to one of the instances...

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

我的问题

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

My question

How to find all the instances of the application I am looking for, not just one of them.

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

NOTE: Although my specifics question is about Office applications, am am also interested in answers that are more general.

推荐答案

还没试过.但它看起来是正确的解决方案.来自 奥利弗博克博客.

Have not tried it. But it looks like the right solution. From Oliver Bock blog.

这篇关于.NET 互操作:使用 C# 查找正在运行的 COM 对象的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆