예제 #1
0
        public static void Register(object obj, string name)
        {
            IRunningObjectTable rot     = null;
            IMoniker            moniker = null;

            try
            {
                rot = GetRunningObjectTable(0);

                moniker = CreateItemMoniker("!", name);

                const int ROTFLAGS_REGISTRATIONKEEPSALIVE = 1;
                var       cookie = rot.Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, obj, moniker);
            }
            finally
            {
                if (moniker != null)
                {
                    Marshal.ReleaseComObject(moniker);
                }
                if (rot != null)
                {
                    Marshal.ReleaseComObject(rot);
                }
            }
        }
예제 #2
0
        private static readonly string _ballmersPlace = "Microsoft "; // field name is depricated of course (but no compiler warning...and Nadella sucks!!!)

        #endregion

        #region Methods

        /// <summary>
        /// Determines one or more items exists in the table
        /// </summary>
        /// <returns>true if one or more item exists, otherwise false</returns>
        public static bool Any()
        {
            IRunningObjectTable runningObjectTable = null;

            try
            {
                int totalCount = 0;
                if (GetRunningObjectTable(0, out runningObjectTable) != 0 || runningObjectTable == null)
                {
                    return(totalCount > 0);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (runningObjectTable != null)
                {
                    TryMarshalReleaseComObject(runningObjectTable);
                }
            }
        }
예제 #3
0
        public static IEnumerable <KeyValuePair <Process, EnvDTE._DTE> > GetVSInstances()
        {
            IRunningObjectTable runningObjectTable = WinApiHelper.GetRunningObjectTable();
            IEnumMoniker        enumMoniker;

            runningObjectTable.EnumRunning(out enumMoniker);
            IMoniker[] monikers = new IMoniker[1];
            for (enumMoniker.Reset(); enumMoniker.Next(1, monikers, IntPtr.Zero) == 0;)
            {
                EnvDTE._DTE dte;
                Process     dteProcess;
                try {
                    IBindCtx ctx = WinApiHelper.NewBindCtx();
                    string   runningObjectName;
                    monikers[0].GetDisplayName(ctx, null, out runningObjectName);
                    if (!runningObjectName.StartsWith("!VisualStudio") && !runningObjectName.StartsWith("!WDExpress.DTE"))
                    {
                        continue;
                    }
                    object runningObjectVal;
                    runningObjectTable.GetObject(monikers[0], out runningObjectVal);
                    dte = runningObjectVal as EnvDTE._DTE;
                    if (dte == null)
                    {
                        continue;
                    }
                    int dteProcessId = int.Parse(runningObjectName.Split(':')[1]);
                    dteProcess = Process.GetProcessById(dteProcessId);
                } catch {
                    continue;
                }
                yield return(new KeyValuePair <Process, EnvDTE._DTE>(dteProcess, dte));
            }
        }
예제 #4
0
        public DsROTEntry(IFilterGraph graph)
        {
            IRunningObjectTable pprot = null;
            IMoniker            ppmk  = null;

            try
            {
                DsError.ThrowExceptionForHR(GetRunningObjectTable(0, out pprot));
                int    id = Process.GetCurrentProcess().Id;
                IntPtr iUnknownForObject = Marshal.GetIUnknownForObject(graph);
                int    num3 = (int)iUnknownForObject;
                Marshal.Release(iUnknownForObject);
                string item = string.Format("FilterGraph {0} pid {1}", num3.ToString("x8"), id.ToString("x8"));
                DsError.ThrowExceptionForHR(CreateItemMoniker("!", item, out ppmk));
                this.m_cookie = pprot.Register(1, graph, ppmk);
            }
            finally
            {
                if (ppmk != null)
                {
                    Marshal.ReleaseComObject(ppmk);
                    ppmk = null;
                }
                if (pprot != null)
                {
                    Marshal.ReleaseComObject(pprot);
                    pprot = null;
                }
            }
        }
예제 #5
0
        /// <summary>Removes the graph from the running object table.</summary>
        /// <param name="cookie">The cookie value for the registered graph.</param>
        private static void RemoveGraphFromRot(RunningObjectTableCookie cookie)
        {
            if (!cookie.IsValid)
            {
                throw new ArgumentException("cookie");
            }
            IRunningObjectTable rot = null;

            try
            {
                // Get the running object table and revoke the cookie
                rot = GetRunningObjectTable(0);
                rot.Revoke(cookie.Value);
                cookie.IsValid = false;
            }
            finally
            {
                // Release the ROT
                if (rot != null)
                {
                    while (Marshal.ReleaseComObject(rot) > 0)
                    {
                        ;
                    }
                }
            }
        }
예제 #6
0
파일: RotHelper.cs 프로젝트: persadewh/xcad
        public static void UnregisterComObject(int id)
        {
            IBindCtx            context = null;
            IRunningObjectTable rot     = null;

            CreateBindCtx(0, out context);
            context.GetRunningObjectTable(out rot);

            try
            {
                rot.Revoke(id);
            }
            finally
            {
                if (rot != null)
                {
                    while (Marshal.ReleaseComObject(rot) > 0)
                    {
                        ;
                    }
                }

                if (context != null)
                {
                    while (Marshal.ReleaseComObject(context) > 0)
                    {
                        ;
                    }
                }
            }
        }
        private EnvDTE.DTE GetDevToolsEnvironment(int processId)
        {
            object runningObject = null;

            IBindCtx            bindCtx      = null;
            IRunningObjectTable rot          = null;
            IEnumMoniker        enumMonikers = null;

            try
            {
                Marshal.ThrowExceptionForHR(CreateBindCtx(reserved: 0, ppbc: out bindCtx));
                bindCtx.GetRunningObjectTable(out rot);
                rot.EnumRunning(out enumMonikers);

                var moniker       = new IMoniker[1];
                var numberFetched = IntPtr.Zero;

                while (enumMonikers.Next(1, moniker, numberFetched) == 0)
                {
                    var runningObjectMoniker = moniker[0];

                    string name = null;

                    try
                    {
                        runningObjectMoniker?.GetDisplayName(bindCtx, null, out name);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        // Do nothing, there is something in the ROT that we do not have access to
                    }

                    var monikerRegex = new Regex(@"!VisualStudio.DTE\.\d+\.\d+\:" + processId, RegexOptions.IgnoreCase);
                    if (!string.IsNullOrEmpty(name) && monikerRegex.IsMatch(name))
                    {
                        Marshal.ThrowExceptionForHR(rot.GetObject(runningObjectMoniker, out runningObject));
                        break;
                    }
                }
            }
            finally
            {
                if (enumMonikers != null)
                {
                    Marshal.ReleaseComObject(enumMonikers);
                }

                if (rot != null)
                {
                    Marshal.ReleaseComObject(rot);
                }

                if (bindCtx != null)
                {
                    Marshal.ReleaseComObject(bindCtx);
                }
            }

            return(runningObject as EnvDTE.DTE);
        }
예제 #8
0
        public static bool RemoveGraphFromRot(ref int cookie)
        {
            IRunningObjectTable rot = null;

            try {
                int hr = GetRunningObjectTable(0, out rot);
                if (hr < 0)
                {
                    Marshal.ThrowExceptionForHR(hr);
                }

                rot.Revoke(cookie);
                cookie = 0;
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                if (rot != null)
                {
                    Marshal.ReleaseComObject(rot);
                }
                rot = null;
            }
        }
예제 #9
0
    object GetComObject(IRunningObjectTable table, IMoniker moniker)
    {
        object runningObjectVal;
        var    res = table.GetObject(moniker, out runningObjectVal);

        return(runningObjectVal);
    }
예제 #10
0
        private static IEnumerable <IMoniker> GetRunningMonikers(this IRunningObjectTable rot)
        {
            IEnumMoniker monikersEnum;

            rot.EnumRunning(out monikersEnum);
            return(monikersEnum.ToEnumerable());
        }
예제 #11
0
        public static IEnumerable <object> GetRunningObjects(string namePattern = ".*")
        {
            IEnumMoniker        enumMoniker = null;
            IRunningObjectTable rot         = null;
            IBindCtx            bindCtx     = null;

            try
            {
                Marshal.ThrowExceptionForHR(Ole32.CreateBindCtx(0, out bindCtx));
                bindCtx.GetRunningObjectTable(out rot);
                rot.EnumRunning(out enumMoniker);

                var moniker = new IMoniker[1];
                var fetched = IntPtr.Zero;

                while (enumMoniker.Next(1, moniker, fetched) == S_OK)
                {
                    object runningObject = null;
                    try
                    {
                        var roMoniker = moniker.First();
                        if (roMoniker == null)
                        {
                            continue;
                        }
                        roMoniker.GetDisplayName(bindCtx, null, out var name);
                        if (!Regex.IsMatch(name, namePattern))
                        {
                            continue;
                        }
                        Marshal.ThrowExceptionForHR(rot.GetObject(roMoniker, out runningObject));
                    }
                    catch (UnauthorizedAccessException)
                    {
                        continue;
                    }

                    if (runningObject != null)
                    {
                        yield return(runningObject);
                    }
                }
            }
            finally
            {
                if (enumMoniker != null)
                {
                    Marshal.ReleaseComObject(enumMoniker);
                }
                if (rot != null)
                {
                    Marshal.ReleaseComObject(rot);
                }
                if (bindCtx != null)
                {
                    Marshal.ReleaseComObject(bindCtx);
                }
            }
        }
예제 #12
0
        internal static DTE GetInstance(string solution)
        {
            const string visualStudioProgId = "!VisualStudio.DTE.";

            IRunningObjectTable runningObjectTable = null;
            IEnumMoniker        enumMoniker        = null;
            IBindCtx            bindCtx            = null;

            try
            {
                Marshal.ThrowExceptionForHR(GetRunningObjectTable(0, out runningObjectTable));
                runningObjectTable.EnumRunning(out enumMoniker);

                IMoniker[] monikers = new IMoniker[1];
                enumMoniker.Reset();

                Marshal.ThrowExceptionForHR(CreateBindCtx(0, out bindCtx));

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

                    if (displayName.StartsWith(visualStudioProgId))
                    {
                        object o;
                        Marshal.ThrowExceptionForHR(runningObjectTable.GetObject(monikers[0], out o));

                        var d = (DTE)o;

                        if (d.Solution.FullName.EndsWith(solution, StringComparison.InvariantCultureIgnoreCase))
                        {
                            return(d);
                        }
                    }
                }
            }
            finally
            {
                if (runningObjectTable != null)
                {
                    Marshal.ReleaseComObject(runningObjectTable);
                }

                if (enumMoniker != null)
                {
                    Marshal.ReleaseComObject(enumMoniker);
                }

                if (bindCtx != null)
                {
                    Marshal.ReleaseComObject(bindCtx);
                }
            }

            return(null);
        }
예제 #13
0
        /// <summary>
        /// Create a list of DSGrapheditROTEntry from IFilterGraphs on the ROT
        /// </summary>
        /// <returns></returns>
        public static List <DSGrapheditROTEntry> GetFilterGraphsFromROT()
        {
            IRunningObjectTable        rtt     = null;
            IEnumMoniker               enumMon = null;
            List <DSGrapheditROTEntry> rots    = new List <DSGrapheditROTEntry>();

            int hr = GetRunningObjectTable(0, out rtt);

            rtt.EnumRunning(out enumMon);
            enumMon.Reset();
            if (hr != 1)
            {
                try
                {
                    IMoniker[] mon = new IMoniker[1];
                    while ((enumMon.Next(1, mon, IntPtr.Zero) == 0))
                    {
                        try
                        {
                            IBindCtx pctx; string displayName;
                            CreateBindCtx(0, out pctx);
                            // Get the name of the file
                            mon[0].GetDisplayName(pctx, null, out displayName);
                            // Clean up
                            Marshal.ReleaseComObject(pctx);
                            if (displayName.StartsWith("!FilterGraph"))
                            {
                                DSGrapheditROTEntry ds1 = new DSGrapheditROTEntry(displayName, mon[0]);
                                if (ds1._processName == "MediaPortal")
                                {
                                    rots.Add(ds1);
                                }
                                else
                                {
                                    ds1.Dispose();
                                }
                            }
                            else
                            {
                                Marshal.ReleaseComObject(mon[0]);
                            }
                        }
                        catch
                        {
                            Marshal.ReleaseComObject(mon[0]);
                            //throw;
                        }
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(enumMon);
                }
            }

            return(rots);
        }
예제 #14
0
        public IFilterGraph ConnectToROTEntry()
        {
            IRunningObjectTable rtt = null;
            int hr = DaggerDSUtils.GetRunningObjectTable(0, out rtt);

            hr = rtt.GetObject(_mon, out _filterGraph);
            Marshal.ReleaseComObject(rtt);
            return(_filterGraph as IFilterGraph);
        }
예제 #15
0
파일: RotHelper.cs 프로젝트: persadewh/xcad
        public static int RegisterComObject(object obj, string monikerName)
        {
            IBindCtx            context = null;
            IRunningObjectTable rot     = null;
            IMoniker            moniker = null;

            CreateBindCtx(0, out context);
            context.GetRunningObjectTable(out rot);

            try
            {
                const int ROTFLAGS_REGISTRATIONKEEPSALIVE = 1;

                context.GetRunningObjectTable(out rot);

                const int S_OK = 0;

                if (CreateItemMoniker("", monikerName, out moniker) != S_OK)
                {
                    throw new Exception("Failed to create moniker");
                }

                var id = rot.Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, obj, moniker);

                if (id == 0)
                {
                    throw new Exception("Failed to register object in ROT");
                }

                return(id);
            }
            finally
            {
                if (moniker != null)
                {
                    while (Marshal.ReleaseComObject(moniker) > 0)
                    {
                        ;
                    }
                }
                if (rot != null)
                {
                    while (Marshal.ReleaseComObject(rot) > 0)
                    {
                        ;
                    }
                }
                if (context != null)
                {
                    while (Marshal.ReleaseComObject(context) > 0)
                    {
                        ;
                    }
                }
            }
        }
        private static DTE GetDTE(int processId, Version version)
        {
            MessageFilter.Register();

            var    process    = Process.GetProcessById(processId);
            string progIdName = "VisualStudio";

            switch (process.MainModule.ModuleName.ToLowerInvariant())
            {
            case "wdexpress.exe":
                progIdName = "WDExpress";
                break;

            case "vwdexpress.exe":
                progIdName = "VWDExpress";
                break;
            }

            string progId        = string.Format("!{0}.DTE.{1}:{2}", progIdName, version, processId);
            object runningObject = null;

            IBindCtx            bindCtx      = null;
            IRunningObjectTable rot          = null;
            IEnumMoniker        enumMonikers = null;

            Marshal.ThrowExceptionForHR(CreateBindCtx(reserved: 0, ppbc: out bindCtx));
            bindCtx.GetRunningObjectTable(out rot);
            rot.EnumRunning(out enumMonikers);

            IMoniker[] moniker       = new IMoniker[1];
            uint       numberFetched = 0;

            while (enumMonikers.Next(1, moniker, out numberFetched) == 0)
            {
                IMoniker runningObjectMoniker = moniker[0];

                string name = null;

                try {
                    if (runningObjectMoniker != null)
                    {
                        runningObjectMoniker.GetDisplayName(bindCtx, null, out name);
                    }
                } catch (UnauthorizedAccessException) {
                    // Do nothing, there is something in the ROT that we do not have access to.
                }

                if (!string.IsNullOrEmpty(name) && string.Equals(name, progId, StringComparison.Ordinal))
                {
                    rot.GetObject(runningObjectMoniker, out runningObject);
                    break;
                }
            }

            return((DTE)runningObject);
        }
예제 #17
0
        public static DTE GetDte(int processId)
        {
            var    progId        = "!TcXaeShell.DTE.15.0:" + processId;
            object runningObject = null;

            IBindCtx            bindCtx      = null;
            IRunningObjectTable rot          = null;
            IEnumMoniker        enumMonikers = null;

            try
            {
                Marshal.ThrowExceptionForHR(CreateBindCtx(0, out bindCtx));
                bindCtx.GetRunningObjectTable(out rot);
                rot.EnumRunning(out enumMonikers);

                var moniker       = new IMoniker[1];
                var numberFetched = IntPtr.Zero;
                while (enumMonikers.Next(1, moniker, numberFetched) == 0)
                {
                    var    runningObjectMoniker = moniker[0];
                    string name = null;
                    try
                    {
                        runningObjectMoniker?.GetDisplayName(bindCtx, null, out name);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        // Do nothing, there is something in the ROT that we do not have access to.
                    }

                    if (string.IsNullOrEmpty(name) || !string.Equals(name, progId, StringComparison.Ordinal))
                    {
                        continue;
                    }
                    Marshal.ThrowExceptionForHR(rot.GetObject(runningObjectMoniker, out runningObject));
                    break;
                }
            }
            finally
            {
                if (enumMonikers != null)
                {
                    Marshal.ReleaseComObject(enumMonikers);
                }
                if (rot != null)
                {
                    Marshal.ReleaseComObject(rot);
                }
                if (bindCtx != null)
                {
                    Marshal.ReleaseComObject(bindCtx);
                }
            }
            return((DTE)runningObject);
        }
예제 #18
0
        /// <summary>Locates the DTE object for the specified process.</summary>
        public static DTE TryLocateDteForProcess(Process process)
        {
            object dte = null;
            IRunningObjectTable runningObjectTable = null;
            IEnumMoniker        enumMoniker        = null;
            IBindCtx            bindContext        = null;
            var monikers = new IMoniker[1];
            var vsProgId = VisualStudioInstanceFactory.VsProgId;

            Ole32.GetRunningObjectTable(0, out runningObjectTable);
            runningObjectTable.EnumRunning(out enumMoniker);
            Ole32.CreateBindCtx(0, out bindContext);

            do
            {
                monikers[0] = null;

                var monikersFetched = 0u;
                var hresult         = enumMoniker.Next(1, monikers, out monikersFetched);

                if (hresult == VSConstants.S_FALSE)
                {
                    // There's nothing further to enumerate, so fail
                    return(null);
                }
                else
                {
                    Marshal.ThrowExceptionForHR(hresult);
                }

                var    moniker         = monikers[0];
                string fullDisplayName = null;

                moniker.GetDisplayName(bindContext, null, out fullDisplayName);

                var displayNameProcessId = 0;

                // FullDisplayName will look something like: <ProgID>:<ProccessId>
                if (!int.TryParse(fullDisplayName.Split(':').Last(), out displayNameProcessId))
                {
                    continue;
                }

                var displayName = fullDisplayName.Substring(0, (fullDisplayName.Length - (displayNameProcessId.ToString().Length + 1)));
                var fullProgId  = vsProgId.StartsWith("!") ? vsProgId : $"!{vsProgId}";

                if (displayName.Equals(fullProgId, StringComparison.OrdinalIgnoreCase) &&
                    (displayNameProcessId == process.Id))
                {
                    runningObjectTable.GetObject(moniker, out dte);
                }
            }while (dte == null);

            return((DTE)(dte));
        }
예제 #19
0
        public static void ROTUnregister(ref int cookie)
        {
            // Revoke any existing file moniker. See Brockschmidt, Inside Ole 2nd ed. p988
            IRunningObjectTable rot = GetROT();

            if (0 != cookie)
            {
                rot.Revoke(cookie);
                cookie = 0;
            }
        }
예제 #20
0
        private void InitializeDteObject(int pid, string version)
        {
            IMoniker            moniker = this.GetItemMoniker(pid, version);
            IRunningObjectTable rot     = this.GetRunningObjectTable();

            Engine.DteObject = this.GetDteObject(rot, moniker);

            if (Engine.DteObject == null)
            {
                throw new InvalidOperationException();
            }
        }
예제 #21
0
        private object GetDteObject(IRunningObjectTable rot, IMoniker moniker)
        {
            object dteObject;
            int    hr = rot.GetObject(moniker, out dteObject);

            if (ErrorHandler.Failed(hr))
            {
                ErrorHandler.ThrowOnFailure(hr, null);
            }

            return(dteObject);
        }
        public static object GetRunningCOMObjectByName(string objectDisplayName)
        {
            IRunningObjectTable runningObjectTable = null;
            IEnumMoniker        monikerList        = null;

            try
            {
                if (GetRunningObjectTable(0, out runningObjectTable) != 0 || runningObjectTable == null)
                {
                    return(null);
                }

                runningObjectTable.EnumRunning(out monikerList);
                monikerList.Reset();

                IMoniker[] monikerContainer       = new IMoniker[1];
                IntPtr     pointerFetchedMonikers = IntPtr.Zero;

                while (monikerList.Next(1, monikerContainer, pointerFetchedMonikers) == 0)
                {
                    IBindCtx bindInfo;
                    string   displayName;

                    CreateBindCtx(0, out bindInfo);
                    monikerContainer[0].GetDisplayName(bindInfo, null, out displayName);
                    Marshal.ReleaseComObject(bindInfo);

                    if (displayName.IndexOf(objectDisplayName, StringComparison.OrdinalIgnoreCase) != -1)
                    {
                        object comInstance;
                        runningObjectTable.GetObject(monikerContainer[0], out comInstance);
                        return(comInstance);
                    }
                }
            }
            catch
            {
                return(null);
            }
            finally
            {
                if (runningObjectTable != null)
                {
                    Marshal.ReleaseComObject(runningObjectTable);
                }

                if (monikerList != null)
                {
                    Marshal.ReleaseComObject(monikerList);
                }
            }
            return(null);
        }
 /// <summary>
 /// Releases the Running Object Table instance.
 /// </summary>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         GC.SuppressFinalize(this);
         if (_rot != null)
         {
             Marshal.ReleaseComObject(_rot);
         }
         _rot = null;
     }
 }
예제 #24
0
        public excel.Workbook GetActiveWorkbook(string xlfile)
        {
            IRunningObjectTable prot      = null;
            IEnumMoniker        pmonkenum = null;

            try
            {
                IntPtr pfetched = IntPtr.Zero;
                // Query the running object table (ROT)
                if (GetRunningObjectTable(0, out prot) != 0 || prot == null)
                {
                    return(null);
                }
                prot.EnumRunning(out pmonkenum);
                pmonkenum.Reset();
                IMoniker[] monikers = new IMoniker[1];
                while (pmonkenum.Next(1, monikers, pfetched) == 0)
                {
                    IBindCtx pctx; string filepathname;
                    CreateBindCtx(0, out pctx);
                    // Get the name of the file
                    monikers[0].GetDisplayName(pctx, null, out filepathname);
                    // Clean up
                    Marshal.ReleaseComObject(pctx);
                    // Search for the workbook
                    if (filepathname.IndexOf(xlfile) != -1)
                    {
                        object roval;
                        // Get a handle on the workbook
                        prot.GetObject(monikers[0], out roval);
                        return(roval as excel.Workbook);
                    }
                }
            }
            catch
            {
                return(null);
            }
            finally
            {
                // Clean up
                if (prot != null)
                {
                    Marshal.ReleaseComObject(prot);
                }
                if (pmonkenum != null)
                {
                    Marshal.ReleaseComObject(pmonkenum);
                }
            }
            return(null);
        }
예제 #25
0
        public static void ROTRegisterAsRunning(IMoniker new_moniker, object o, ref int rot_cookie, Type intf)
        {
            // Revoke any existing file moniker. See Brockschmidt, Inside Ole 2nd ed. p988
            ROTUnregister(ref rot_cookie);

            // Register the moniker in the running object table (ROT).
            ComDebug.ReportInfo("Registering {0} in ROT", DataObjectHelper.GetDisplayName(new_moniker));
            IRunningObjectTable rot = GetROT();

            // This flag solved a terrible problem where Word would stop
            // communicating after its first call to GetObject().
            rot_cookie = rot.Register(1 /*ROTFLAGS_REGISTRATIONKEEPSALIVE*/, o, new_moniker);
        }
예제 #26
0
        static DTE FindRunningVSProWithOurSolution(string solutionPath)
        {
            DTE                 dte           = null;
            object              runningObject = null;
            IBindCtx            bindCtx       = null;
            IRunningObjectTable rot           = null;
            IEnumMoniker        enumMonikers  = null;

            try
            {
                Marshal.ThrowExceptionForHR(CreateBindCtx(reserved: 0, ppbc: out bindCtx));
                bindCtx.GetRunningObjectTable(out rot);
                rot.EnumRunning(out enumMonikers);

                IMoniker[] moniker       = new IMoniker[1];
                IntPtr     numberFetched = IntPtr.Zero;
                while (enumMonikers.Next(1, moniker, numberFetched) == 0)
                {
                    IMoniker runningObjectMoniker = moniker[0];
                    Marshal.ThrowExceptionForHR(rot.GetObject(runningObjectMoniker, out runningObject));
                    var dte2 = runningObject as DTE;
                    if (dte2 != null)
                    {
                        if (dte2.Solution.FullName == solutionPath)
                        {
                            dte = dte2;
                            break;
                        }
                    }
                }
            }
            finally
            {
                if (enumMonikers != null)
                {
                    Marshal.ReleaseComObject(enumMonikers);
                }

                if (rot != null)
                {
                    Marshal.ReleaseComObject(rot);
                }

                if (bindCtx != null)
                {
                    Marshal.ReleaseComObject(bindCtx);
                }
            }

            return(dte);
        }
예제 #27
0
        public static object GetActiveObject(string progId)
        {
            // Convert the prog id into a class id
            string classId = ConvertProgIdToClassId(progId);

            IRunningObjectTable prot      = null;
            IEnumMoniker        pMonkEnum = null;

            try
            {
                IntPtr Fetched = IntPtr.Zero;
                // Open the running objects table.
                NativeMethods.GetRunningObjectTable(0, out prot);
                prot.EnumRunning(out pMonkEnum);
                pMonkEnum.Reset();
                IMoniker[] pmon = new IMoniker[1];

                // Iterate through the results
                while (pMonkEnum.Next(1, pmon, Fetched) == 0)
                {
                    IBindCtx pCtx;

                    NativeMethods.CreateBindCtx(0, out pCtx);

                    string displayName;
                    pmon[0].GetDisplayName(pCtx, null, out displayName);

                    Marshal.ReleaseComObject(pCtx);
                    if (displayName.IndexOf(classId) != -1)
                    {
                        // Return the matching object
                        object objReturnObject;
                        int    ret = prot.GetObject(pmon[0], out objReturnObject);
                        return(objReturnObject);
                    }
                }
                return(null);
            }
            finally
            {
                // Free resources
                if (prot != null)
                {
                    Marshal.ReleaseComObject(prot);
                }
                if (pMonkEnum != null)
                {
                    Marshal.ReleaseComObject(pMonkEnum);
                }
            }
        }
예제 #28
0
    /// <summary>
    /// Returns an object from the ROT, given a prog Id.
    /// </summary>
    /// <param name="progId">The prog id of the object to return.</param>
    /// <returns>The requested object, or null if the object is not found.</returns>
    public static object GetActiveObject(string progId)
    {
        // Convert the prog id into a class id
        string classId = ConvertProgIdToClassId(progId);

        IRunningObjectTable runningObjectTable = null;
        IEnumMoniker        monikerEnumerator  = null;
        IBindCtx            ctx = null;

        try
        {
            IntPtr numFetched = new IntPtr();
            // Open the running objects table.
            CreateBindCtx(0, out ctx);
            ctx.GetRunningObjectTable(out runningObjectTable);
            runningObjectTable.EnumRunning(out monikerEnumerator);
            monikerEnumerator.Reset();
            IMoniker[] monikers = new IMoniker[1];

            // Iterate through the results
            while (monikerEnumerator.Next(1, monikers, numFetched) == 0)
            {
                string runningObjectName;
                monikers[0].GetDisplayName(ctx, null, out runningObjectName);
                if (runningObjectName.IndexOf(classId) != -1)
                {
                    // Return the matching object
                    object objReturnObject;
                    runningObjectTable.GetObject(monikers[0], out objReturnObject);
                    return(objReturnObject);
                }
            }
            return(null);
        }
        finally
        {
            // Free resources
            if (runningObjectTable != null)
            {
                Marshal.ReleaseComObject(runningObjectTable);
            }
            if (monikerEnumerator != null)
            {
                Marshal.ReleaseComObject(monikerEnumerator);
            }
            if (ctx != null)
            {
                Marshal.ReleaseComObject(ctx);
            }
        }
    }
예제 #29
0
        public static DTE2 GetDTE2(DTE dte)
        {
            try
            {
                List <DTE2> dte2List = new List <DTE2>();

                IRunningObjectTable runningObjectTable = null;
                NativeMethods.GetRunningObjectTable(0, out runningObjectTable);

                IEnumMoniker enumMoniker = null;
                runningObjectTable.EnumRunning(out enumMoniker);

                enumMoniker.Reset();
                IntPtr     fetched = IntPtr.Zero;
                IMoniker[] moniker = new IMoniker[1];

                while (enumMoniker.Next(1, moniker, fetched) == 0)
                {
                    IBindCtx bindCtx = null;
                    NativeMethods.CreateBindCtx(0, out bindCtx);

                    string displayName = "";
                    moniker[0].GetDisplayName(bindCtx, null, out displayName);
                    // add all VisualStudio ROT entries to list
                    if (displayName.StartsWith("!VisualStudio"))
                    {
                        object comObject;
                        runningObjectTable.GetObject(moniker[0], out comObject);
                        dte2List.Add((DTE2)comObject);
                    }
                }

                // find the correct dte2 instance (each running VS instance has one...)
                KeyValuePair <DTE2, int> maxMatch = new KeyValuePair <DTE2, int>(null, 0);
                foreach (DTE2 dte2 in dte2List)
                {
                    int m = StringUtility.GetMatchingCharsFromStart(dte.Solution.FullName, dte2.Solution.FullName);
                    if (m > maxMatch.Value)
                    {
                        maxMatch = new KeyValuePair <DTE2, int>(dte2, m);
                    }
                }

                return(maxMatch.Key);
            }
            catch (Exception e)
            {
                Logging.Logging.LogError("Exception: " + e.Message);
                return(null);
            }
        }
예제 #30
0
        private static object GetRunningObject(string displayName, out IEnumerable <string> runningObjectDisplayNames)
        {
            IBindCtx bindContext = null;

            NativeMethods.CreateBindCtx(0, out bindContext);

            IRunningObjectTable runningObjectTable = null;

            bindContext.GetRunningObjectTable(out runningObjectTable);

            IEnumMoniker monikerEnumerator = null;

            runningObjectTable.EnumRunning(out monikerEnumerator);

            object        runningObject = null;
            List <string> runningObjectDisplayNameList = new List <string>();

            IMoniker[] monikers      = new IMoniker[1];
            IntPtr     numberFetched = IntPtr.Zero;

            while (monikerEnumerator.Next(1, monikers, numberFetched) == 0)
            {
                IMoniker moniker = monikers[0];

                string objectDisplayName = null;
                try
                {
                    moniker.GetDisplayName(bindContext, null, out objectDisplayName);
                }
                catch (UnauthorizedAccessException)
                {
                    // Some ROT objects require elevated permissions.
                }

                if (!string.IsNullOrWhiteSpace(objectDisplayName))
                {
                    runningObjectDisplayNameList.Add(objectDisplayName);
                    if (objectDisplayName.EndsWith(displayName, StringComparison.Ordinal))
                    {
                        runningObjectTable.GetObject(moniker, out runningObject);
                        if (runningObject == null)
                        {
                            throw new InvalidOperationException($"Failed to get running object with display name {displayName}");
                        }
                    }
                }
            }

            runningObjectDisplayNames = runningObjectDisplayNameList;
            return(runningObject);
        }
 internal static extern void GetRunningObjectTable(int reserved,
     out IRunningObjectTable prot);
예제 #32
0
파일: DesignTime.cs 프로젝트: awf/autodiff
 public static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable prot);
예제 #33
0
 private static extern int GetRunningObjectTable(uint reserved,
     out IRunningObjectTable prot);
예제 #34
0
 static extern uint GetRunningObjectTable(uint reserved, out IRunningObjectTable ROT);
예제 #35
0
        private object GetDteObject(IRunningObjectTable rot, IMoniker moniker)
        {
            object dteObject;
            int hr = rot.GetObject(moniker, out dteObject);
            if (ErrorHandler.Failed(hr))
            {
                ErrorHandler.ThrowOnFailure(hr, null);
            }

            return dteObject;
        }
예제 #36
0
 private static extern int GetRunningObjectTable(
     int reserved, out IRunningObjectTable ROT);
 /// <summary>
 /// Releases the Running Object Table instance.
 /// </summary>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         GC.SuppressFinalize(this);
         if (_rot != null)
             Marshal.ReleaseComObject(_rot);
         _rot = null;
     }
 }
예제 #38
0
 public static extern HRESULT GetRunningObjectTable(DWORD reserved, out IRunningObjectTable pprot);
예제 #39
0
 internal static extern int GetRunningObjectTable(int r, out IRunningObjectTable pprot);