/// 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); }
public static IEnumerable <dynamic> Enum() { // get Running Object Table ... IRunningObjectTable Rot; GetRunningObjectTable(0, out Rot); // get enumerator for ROT entries IEnumMoniker monikerEnumerator = null; Rot.EnumRunning(out monikerEnumerator); IntPtr pNumFetched = new IntPtr(); IMoniker[] monikers = new IMoniker[1]; IBindCtx bindCtx; CreateBindCtx(0, out bindCtx); while (monikerEnumerator.Next(1, monikers, pNumFetched) == 0) { string applicationName = ""; dynamic workBook = null; try { Guid IUnknown = new Guid("{00000000-0000-0000-C000-000000000046}"); monikers[0].BindToObject(bindCtx, null, ref IUnknown, out workBook); applicationName = workBook.Application.Name; } catch { } if (applicationName == "Microsoft Excel") { yield return(workBook); } } }
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); }