Пример #1
0
        private static List <DteInstanceInfo> RetrieveDteInstances()
        {
            var candidates = new List <DteInstanceInfo>();

            var retVal = WinAPI.GetRunningObjectTable(0, out IRunningObjectTable rot);

            if (retVal != 0)
            {
                return(candidates);
            }

            rot.EnumRunning(out IEnumMoniker enumMoniker);

            var monikers = new IMoniker[1];

            while (enumMoniker.Next(1, monikers, IntPtr.Zero) == 0)
            {
                WinAPI.CreateBindCtx(0, out var bindCtx);
                monikers[0].GetDisplayName(bindCtx, null, out var displayName);

                if (!DteProgIds.Any(progId => displayName.StartsWith($"!{progId}:")))
                {
                    continue;
                }

                try
                {
                    rot.GetObject(monikers[0], out dynamic dte);
                    var instanceInfo = new DteInstanceInfo
                    {
                        DisplayName = displayName,
                        DteInstance = dte
                    };
                    candidates.Add(instanceInfo);
                }
                catch (COMException)
                {
                    // DTE might not be ready at this time
                }
            }

            return(candidates);
        }