Search code examples
.netinteropautomationms-officecom-interop

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


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.


Solution

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