12

Currently my code uses

SurferApp = Marshal.GetActiveObject("Surfer.Application") as Surfer.Application

to get the running instance of a software called surfer, for the sake of simplicity we can replace Surfer to Word that everyone knows about. Now let's say I have 2 MS word application running and I want to get both of them using Marshal.GetActiveObject(), how can I get both the running instances and associate each with a separate object?

kmatyaszek
  • 18,648
  • 9
  • 57
  • 63
hoooman
  • 572
  • 3
  • 6
  • 15

2 Answers2

13

Marshal.GetActiveObject returns the first instance it finds on ROT (running object table). If you have more than one instance running with the same name/id, you have to get it directly from ROT.

A few links to start:

Lukas Winzenried
  • 1,889
  • 1
  • 14
  • 22
3

In addition to the links in the accepted answer, I found the following very helpful for walking the ROT, especially if the application you're interested in is AutoCAD:

http://adndevblog.typepad.com/autocad/2013/12/accessing-com-applications-from-the-running-object-table.html

MattDG
  • 373
  • 3
  • 14
  • 2
    I tried this code out and there seems to be an issue with it - it only ever seems to return the same instance of a running process if you have multiple instances open. You get the same process times the number of instances you have open, not the individual instances, if that makes sense. Looking at the comments in below the code, it looks like a number of people are having the same issue. I'll report back if I find a fix for this. – VincentH Feb 22 '22 at 12:24