Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Marshal.getActiveObject() to get 2 instance of of a running process that has two processes open

Tags:

c#

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?

like image 309
hoooman Avatar asked Nov 17 '12 15:11

hoooman


2 Answers

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

like image 23
MattDG Avatar answered Sep 21 '22 05:09

MattDG


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:

  • http://dotnet-snippets.de/dns/laufende-com-objekte-abfragen-SID526.aspx (German only)
  • http://www.codeproject.com/Articles/17490/Running-Object-Table-Provider-in-NET-consumer-in-M
  • http://www.codeproject.com/Articles/7984/Automating-a-specific-instance-of-Visual-Studio-NE
like image 139
Lukas Winzenried Avatar answered Nov 16 '22 15:11

Lukas Winzenried