.NET Interop: Find All instances of of a running COM object with C#

江枫思渺然 提交于 2019-12-30 07:03:08

问题


Background

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

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.

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


回答1:


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



来源:https://stackoverflow.com/questions/393167/net-interop-find-all-instances-of-of-a-running-com-object-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!