Example #1
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;
                }
            }
        }
Example #2
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);
                }
            }
        }
Example #3
0
        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)
                    {
                        ;
                    }
                }
            }
        }
Example #4
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);
        }
        public DsROTEntry(IFilterGraph graph)
        {
            int hr = 0;
            IRunningObjectTable rot = null;
            IMoniker            mk  = null;

            try
            {
                // First, get a pointer to the running object table
                hr = GetRunningObjectTable(0, out rot);
                new HRESULT(hr).Throw();

                // Build up the object to add to the table
                int    id    = System.Diagnostics.Process.GetCurrentProcess().Id;
                IntPtr iuPtr = Marshal.GetIUnknownForObject(graph);
                string s;
                try
                {
                    s = iuPtr.ToString("x");
                }
                catch
                {
                    s = "";
                }
                finally
                {
                    Marshal.Release(iuPtr);
                }
                string item = string.Format("FilterGraph {0} pid {1}", s, id.ToString("x8"));
                hr = CreateItemMoniker("!", item, out mk);
                new HRESULT(hr).Throw();

                // Add the object to the table
                m_cookie = rot.Register((int)ROTFlags.RegistrationKeepsAlive, graph, mk);
            }
            finally
            {
                if (mk != null)
                {
                    Marshal.ReleaseComObject(mk);
                    mk = null;
                }
                if (rot != null)
                {
                    Marshal.ReleaseComObject(rot);
                    rot = null;
                }
            }
        }
Example #6
0
        /// <summary>Adds a graph to the ROT.</summary>
        /// <param name="graph">The graph to be added.</param>
        /// <returns>A cookie that can be used to remove this graph from the ROT.</returns>
        private static RunningObjectTableCookie AddGraphToRot(IGraphBuilder graph)
        {
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }
            IRunningObjectTable rot     = null;
            IMoniker            moniker = null;

            try
            {
                // Get the ROT.
                rot = GetRunningObjectTable(0);

                // Create a moniker for the grpah
                int pid;
                using (Process p = Process.GetCurrentProcess())
                {
                    pid = p.Id;
                }
                IntPtr unkPtr = Marshal.GetIUnknownForObject(graph);
                string item   = string.Format("FilterGraph {0} pid {1}", ((int)unkPtr).ToString("x8"), pid.ToString("x8"));
                Marshal.Release(unkPtr);
                moniker = CreateItemMoniker("!", item);

                // Registers the graph in the running object table
                int cookieValue = rot.Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, graph, moniker);
                return(new RunningObjectTableCookie(cookieValue));
            }
            finally
            {
                // Releases the COM objects
                if (moniker != null)
                {
                    while (Marshal.ReleaseComObject(moniker) > 0)
                    {
                        ;
                    }
                }
                if (rot != null)
                {
                    while (Marshal.ReleaseComObject(rot) > 0)
                    {
                        ;
                    }
                }
            }
        }
Example #7
0
        public static bool AddGraphToRot(object graph, out int cookie)
        {
            cookie = 0;
            int hr = 0;
            IRunningObjectTable rot = null;
            IMoniker            mk  = null;

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

                int    id    = GetCurrentProcessId();
                IntPtr iuPtr = Marshal.GetIUnknownForObject(graph);
                int    iuInt = (int)iuPtr;
                Marshal.Release(iuPtr);
                string item = string.Format("FilterGraph {0} pid {1}", iuInt.ToString("x8"), id.ToString("x8"));
                hr = CreateItemMoniker("!", item, out mk);
                if (hr < 0)
                {
                    Marshal.ThrowExceptionForHR(hr);
                }

                rot.Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, graph, mk); //, out cookie );
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                if (mk != null)
                {
                    Marshal.ReleaseComObject(mk);
                }
                mk = null;
                if (rot != null)
                {
                    Marshal.ReleaseComObject(rot);
                }
                rot = null;
            }
        }
        public RotRegistration(string moniker, object o)
        {
            if (String.IsNullOrEmpty(moniker))
            {
                throw new ArgumentException("Contract assertion not met: !String.IsNullOrEmpty(moniker)", nameof(moniker));
            }
            if (o == null)
            {
                throw new ArgumentNullException(nameof(o), "Contract assertion not met: o != null");
            }

            this.Target = o;

            rot = GetRunningObjectTable(0);
            var imoniker = CreateItemMoniker("!", moniker);

            hRotEntry = rot.Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, o, imoniker);
        }
Example #9
0
 private void Register()
 {
     if (moniker == null)
     {
         return;                 // No need to raise an error. This feature is merely a nicety anyway.
     }
     if (cookie == null)
     {
         IRunningObjectTable table = GetTable();
         if (table != null)
         {
             object comObject = (IFileIsInUse)this;
             cookie = table.Register((int)(NativeMethods.RotFlags.RegistrationKeepsAlive), comObject, moniker);
         }
     }
     else
     {
         throw new InvalidOperationException("File is already registered as in use");
     }
 }
Example #10
0
        /// <summary>
        /// Initializes new instance of the <see cref="DsROTEntry"/> with graph
        /// </summary>
        /// <param name="graph">The filter graph</param>
        public DsROTEntry(IFilterGraph graph)
        {
            int hr = 0;
            IRunningObjectTable rot = null;
            IMoniker            mk  = null;

            try
            {
                // First, get a pointer to the running object table
                hr = GetRunningObjectTable(0, out rot);
                DsError.ThrowExceptionForHR(hr);

                // Build up the object to add to the table
                int    id    = Process.GetCurrentProcess().Id;
                IntPtr iuPtr = Marshal.GetIUnknownForObject(graph);
                string s;
                try
                {
                    s = iuPtr.ToString("x");
                }
                catch
                {
                    s = "";
                }
                finally
                {
                    Marshal.Release(iuPtr);
                }
                string item = string.Format("FilterGraph {0} pid {1:x8}", s, id);
                hr = CreateItemMoniker("!", item, out mk);
                DsError.ThrowExceptionForHR(hr);

                // Add the object to the table
                _cookie = rot.Register((int)ROTFlags.RegistrationKeepsAlive, graph, mk);
            }
            finally
            {
                DsUtils.ReleaseComObject(mk);
                DsUtils.ReleaseComObject(rot);
            }
        }
Example #11
0
        public static bool AddToRot(object pUnkGraph, out int pdwRegister)
        {
            IMoniker            moniker = null;
            IRunningObjectTable rot     = null;

            try
            {
                Marshal.ThrowExceptionForHR(GetRunningObjectTable(0, out rot));
                IntPtr iuPtr = Marshal.GetIUnknownForObject(pUnkGraph);
                int    iuInt = (int)iuPtr;
                Marshal.Release(iuPtr);
                string item = String.Format("FilterGraph {0} pid {1}",
                                            iuInt.ToString("x8"),
                                            Process.GetCurrentProcess().Id.ToString("x8"));
                Marshal.ThrowExceptionForHR(CreateItemMoniker("!", item, out moniker));
                pdwRegister = rot.Register(DsHlp.ROTFLAGS_REGISTRATIONKEEPSALIVE /* | DsHlp.ROTFLAGS_ALLOWANYCLIENT*/, pUnkGraph,
                                           moniker);
                return(true);
            }
            catch
            {
                pdwRegister = 0;
                return(false);
            }
            finally
            {
                if (moniker != null)
                {
                    Marshal.ReleaseComObject(moniker);
                }
                if (rot != null)
                {
                    Marshal.ReleaseComObject(rot);
                }
            }
        }