Ejemplo n.º 1
0
        /// Obtains the DTE2 interface for this instance of VS from the RunningObjectTable
        private static DTE2 GetDTE2ForCurrentInstance(DTE DTE)
        {
            // Find the ROT entry for visual studio running under current process.
            IRunningObjectTable Rot;

            NativeMethods.GetRunningObjectTable(0, out Rot);
            IEnumMoniker EnumMoniker;

            Rot.EnumRunning(out EnumMoniker);
            EnumMoniker.Reset();
            uint Fetched = 0;

            IMoniker[] Moniker = new IMoniker[1];
            while (EnumMoniker.Next(1, Moniker, out Fetched) == 0)
            {
                object ComObject;
                Rot.GetObject(Moniker[0], out ComObject);
                DTE2 CandidateDTE2 = ComObject as DTE2;

                if (CandidateDTE2 != null)
                {
                    if (CandidateDTE2.DTE == DTE)
                    {
                        return(CandidateDTE2);
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
    public static List <VSSolution> GetRunningSolutions()
    {
        var instances = new List <VSSolution>();
        // Get running object table reference iterator
        IRunningObjectTable Rot;
        int hr = GetRunningObjectTable(0, out Rot);

        if (hr < 0)
        {
            throw new COMException("No rot?", hr);
        }
        IEnumMoniker monikerEnumerator;

        Rot.EnumRunning(out monikerEnumerator);
        // And iterate
        IntPtr pNumFetched = new IntPtr();

        IMoniker[] monikers = new IMoniker[1];
        while (monikerEnumerator.Next(1, monikers, pNumFetched) == 0)
        {
            IBindCtx bindCtx;
            int      hr2 = CreateBindCtx(0, out bindCtx);
            if (hr < 0)
            {
                continue;
            }
            // Check if display ends with ".sln"
            string displayName;
            monikers[0].GetDisplayName(bindCtx, null, out displayName);
            if (displayName.EndsWith(".sln", StringComparison.CurrentCultureIgnoreCase))
            {
                object obj;
                Rot.GetObject(monikers[0], out obj);
                if (obj is EnvDTE.Solution)
                {
                    instances.Add(new VSSolution {
                        Name = displayName, Solution = (EnvDTE.Solution)obj
                    });
                }
                else
                {
                    Marshal.ReleaseComObject(obj);
                }
            }
        }
        return(instances);
    }
Ejemplo n.º 3
0
        // Requires Using System.Runtime.InteropServices.ComTypes

        // Get all running instance by querying ROT

        public static IEnumerable <RunningObjectTableEntry> GetActiveEntries()
        {
            // get Running Object Table ...
            GetRunningObjectTable(0, out var Rot);
            if (Rot == null)
            {
                yield break;
            }

            // get enumerator for ROT entries
            Rot.EnumRunning(out var monikerEnumerator);

            if (monikerEnumerator == null)
            {
                yield break;
            }


            monikerEnumerator.Reset();



            var pNumFetched = new IntPtr();

            var monikers = new IMoniker[1];



            // go through all entries and identifies app instances

            while (monikerEnumerator.Next(1, monikers, pNumFetched) == 0)
            {
                var ret = default(RunningObjectTableEntry);

                try
                {
                    CreateBindCtx(0, out var bindCtx);

                    if (bindCtx == null)
                    {
                        continue;
                    }

                    Rot.GetObject(monikers[0], out var ComObject);
                    monikers[0].GetDisplayName(bindCtx, null, out var displayName);
                    monikers[0].GetClassID(out var ClassId);

                    ret = new RunningObjectTableEntry()
                    {
                        Object      = ComObject,
                        DisplayName = displayName,
                        ClassID     = ClassId,
                    };
                }
                catch (Exception ex)
                {
                    ex.Ignore();
                }

                if (ret != null)
                {
                    yield return(ret);
                }
            }
        }