Book a Demo

Author Topic: HowTo: Get process id of COM Interop instance  (Read 10477 times)

siaccarino

  • EA User
  • **
  • Posts: 29
  • Karma: +0/-1
    • View Profile
HowTo: Get process id of COM Interop instance
« on: September 11, 2019, 04:08:37 pm »
Hi,

is  there a way to figure out the interop EA process ID when I am using EA via COM interface?

The background is that I want to use EA for some background jobs and if the jobs being terminated (eg. cause of job timeout), the interop EA instance becomes orphaned.

The idea is to write the ID to the console and terminate the orphaned EA instance in the job. I can't just terminate all EA instances cause another job may runnning in parallel.

siaccarino

  • EA User
  • **
  • Posts: 29
  • Karma: +0/-1
    • View Profile
Re: HowTo: Get process id of COM Interop instance
« Reply #1 on: September 11, 2019, 04:37:50 pm »
This solution works but is really an odd hack
Code: [Select]
HashSet<int> previousEAids = new HashSet<int>();
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("EA"))
    previousEAids.Add(p.Id);
EA.Repository myEAInstance = new EA.Repository();
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("EA"))
    if (!ea.Contains(p.Id))
        System.Console.WriteLine("EA process ID=" + p.Id);
« Last Edit: September 11, 2019, 04:40:13 pm by siaccarino »

smendonc

  • EA User
  • **
  • Posts: 148
  • Karma: +5/-0
  • I love YaBB 1 Gold!
    • View Profile
Re: HowTo: Get process id of COM Interop instance
« Reply #2 on: September 12, 2019, 05:16:52 am »
The below will also get the process id's directly from the windows running object table. When an EA instance starts up it registers itself with the process id as part of the name.

This is a slight modification of the code I posted here https://github.com/smend5464/SparxEA for accessing a specific instance of EA when multiple are running.

However, if I understand what you are trying to do correctly you only want the list of instances that you start via the interop dll.  I don't think that is possible as that dll is only a .net wrapper around the Com interfaces and references to it would be removed during the build process.  I posted the link above because there might be something you can do by looking inside the running EA instance that might help with identification.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

namespace SparxProcID
{
    internal class Program
    {
        [DllImport("ole32.dll")]
        private static extern int CreateBindCtx(uint reserved, out IBindCtx bindCtx);

        [DllImport("ole32.dll")]
        private static extern int GetRunningObjectTable(uint reserved,
            out IRunningObjectTable runningObjectTable);
       
        public static void Main(string[] args)
        {
            var pids = new List<int>();
            var monikers = new IMoniker[1];
           
            GetRunningObjectTable(0, out var rot);
            rot.EnumRunning(out var enumMoniker);

            while (enumMoniker.Next(1, monikers, IntPtr.Zero) == 0)
            {
                CreateBindCtx(0, out var ctx);

                monikers[0].GetDisplayName(ctx, null, out var name);

                if (!name.Contains("Sparx.EA.App")) continue;

                // Get the ProcID of the object instance
                var pid = name.Split(':')[1];
                if (!string.IsNullOrEmpty(pid))
                    pids.Add(int.Parse(pid));
            }
           
            // Write out the EA pids
            foreach (var pid in pids)
            {
                Console.WriteLine("EA Process ID: {0}", pid);
            }
        }
    }
}