.NET互操作:找到一个正在运行的COM对象与C#的所有实例实例、正在运行、对象、操作

2023-09-04 03:25:05 作者:安颜

我通过自动化命令行工具,一些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.