views:

484

answers:

2

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  A: 

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

Igal Serban
A: 

The general question does not have an answer. What would the "application instance" mean in the general case? Most applications won't register an object for other applications to interact with.

erikkallen