Exemplo n.º 1
0
        //public static object CreateInstance(Guid classIID)
        //{
        //	Guid iid = typeof(nsISupports).GUID;
        //	return ComponentManager.CreateInstance(ref classIID, null, ref iid);
        //}

        //public static object CreateInstance(string contractID)
        //{
        //	return CreateInstance<nsISupports>(contractID);
        //}

        public static TInterfaceType CreateInstance <TInterfaceType>(string contractID)
        {
            Guid   iid = typeof(TInterfaceType).GUID;
            IntPtr ptr = ComponentManager.CreateInstanceByContractID(contractID, null, ref iid);

            return((TInterfaceType)Xpcom.GetObjectForIUnknown(ptr));
        }
Exemplo n.º 2
0
        public ComPtr <nsISupports> GetGlobalNsObject()
        {
            IntPtr globalObject = SpiderMonkey.CurrentGlobalOrNull(_cx);

            if (globalObject != IntPtr.Zero)
            {
                Guid guid = typeof(nsISupports).GUID;

                IntPtr pUnk = IntPtr.Zero;
                try
                {
                    pUnk = Xpcom.XPConnect.Instance.WrapJS(_cx, globalObject, ref guid);
                    object comObj = Xpcom.GetObjectForIUnknown(pUnk);
                    try
                    {
                        return(Xpcom.QueryInterface <nsISupports>(comObj).AsComPtr());
                    }
                    finally
                    {
                        Xpcom.FreeComObject(ref comObj);
                    }
                }
                finally
                {
                    if (pUnk != IntPtr.Zero)
                    {
                        Marshal.Release(pUnk);
                    }
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts to COM object without null check
        /// </summary>
        /// <returns></returns>
        private object ToComObjectInternal()
        {
            var context = AutoJSContext.Current;

            if (context == null)
            {
                throw new Exception("AutoJSContext not set.");
            }
            {
                var jsObject = SpiderMonkey.JS_ValueToObject(context.ContextPointer, this);

                var guid = typeof(nsISupports).GUID;
                var pUnk = IntPtr.Zero;
                try
                {
                    pUnk = Xpcom.XPConnect.Instance.WrapJS(context.ContextPointer, jsObject, ref guid);
                    var comObj = Xpcom.GetObjectForIUnknown(pUnk);

                    return(comObj);
                }
                finally
                {
                    if (pUnk != IntPtr.Zero)
                    {
                        Marshal.Release(pUnk);
                    }
                }
            }
        }
Exemplo n.º 4
0
        //public static object GetService(string contractID)
        //{
        //    return GetService<nsISupports>(contractID);
        //}

        public static TInterfaceType GetService <TInterfaceType>(string contractID)
        {
            AssertCorrectThread();

            Guid   iid = typeof(TInterfaceType).GUID;
            IntPtr ptr = ServiceManager.GetServiceByContractID(contractID, ref iid);

            return((TInterfaceType)Xpcom.GetObjectForIUnknown(ptr));
        }
Exemplo n.º 5
0
        //public static object CreateInstance(Guid classIID)
        //{
        //	Guid iid = typeof(nsISupports).GUID;
        //	return ComponentManager.CreateInstance(ref classIID, null, ref iid);
        //}

        //public static object CreateInstance(string contractID)
        //{
        //	return CreateInstance<nsISupports>(contractID);
        //}

        public static TInterfaceType CreateInstance <TInterfaceType>(string contractID)
        {
            Guid   iid = typeof(TInterfaceType).GUID;
            IntPtr ptr = ComponentManager.CreateInstanceByContractID(contractID, null, ref iid);
            var    obj = (TInterfaceType)Xpcom.GetObjectForIUnknown(ptr);

            // now owned by the RCW.
            Marshal.Release(ptr);
            return(obj);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the string value of a pref. This is a replacement of nsIPrefBranch::GetCharPref(), which can't handle unicode values.
        /// See also <a href="http://www-archive.mozilla.org/projects/intl/changefontpref2.html">How to change the font setting in Gecko from the Embedding Application</a>
        /// </summary>
        private static string GetUnicodePref(nsIPrefBranch branch, string name)
        {
            var    iid  = typeof(nsISupportsString).GUID;
            IntPtr pStr = branch.GetComplexValue(name, ref iid);

            if (pStr == IntPtr.Zero)
            {
                return(null);
            }
            var sStr = (nsISupportsString)Xpcom.GetObjectForIUnknown(pStr);

            Marshal.Release(pStr);
            return(nsString.Get(sStr.GetDataAttribute));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Converts to COM object without null check
        /// </summary>
        /// <param name="cx"></param>
        /// <returns></returns>
        private object ToComObjectInternal(IntPtr cx)
        {
            using (var context = new AutoJSContext(cx))
            {
                var jsObject = SpiderMonkey.JS_ValueToObject(context.ContextPointer, this);

                var guid = typeof(nsISupports).GUID;
                var pUnk = IntPtr.Zero;
                try
                {
                    pUnk = Xpcom.XPConnect.Instance.WrapJS(context.ContextPointer, jsObject, ref guid);
                    var comObj = Xpcom.GetObjectForIUnknown(pUnk);

                    return(comObj);
                }
                finally
                {
                    if (pUnk != IntPtr.Zero)
                    {
                        Marshal.Release(pUnk);
                    }
                }
            }
        }
Exemplo n.º 8
0
        public nsISupports GetGlobalNsObject()
        {
            IntPtr globalObject = SpiderMonkey.JS_GetGlobalObject(_cx);

            if (globalObject != IntPtr.Zero)
            {
                Guid guid = typeof(nsISupports).GUID;

                IntPtr pUnk = IntPtr.Zero;
                try
                {
                    pUnk = XPConnect.WrapJS(_cx, globalObject, ref guid);
                    return((nsISupports)Xpcom.GetObjectForIUnknown(pUnk));
                }
                finally
                {
                    if (pUnk != IntPtr.Zero)
                    {
                        Marshal.Release(pUnk);
                    }
                }
            }
            return(null);
        }
Exemplo n.º 9
0
        public static object QueryInterface(object obj, Guid iid)
        {
            AssertCorrectThread();

            if (obj == null)
            {
                return(null);
            }

            // get an nsISupports (aka IUnknown) pointer from the object
            IntPtr pUnk = Marshal.GetIUnknownForObject(obj);

            if (pUnk == IntPtr.Zero)
            {
                return(null);
            }

            // query interface
            IntPtr ppv;

            Marshal.QueryInterface(pUnk, ref iid, out ppv);

            // if QueryInterface didn't work, try using nsIInterfaceRequestor instead
            if (ppv == IntPtr.Zero)
            {
                // QueryInterface the object for nsIInterfaceRequestor
                Guid   interfaceRequestorIID = typeof(nsIInterfaceRequestor).GUID;
                IntPtr pInterfaceRequestor;
                Marshal.QueryInterface(pUnk, ref interfaceRequestorIID, out pInterfaceRequestor);

                // if we got a pointer to nsIInterfaceRequestor
                if (pInterfaceRequestor != IntPtr.Zero)
                {
                    // convert it to a managed interface
                    QI_nsIInterfaceRequestor req = (QI_nsIInterfaceRequestor)Xpcom.GetObjectForIUnknown(pInterfaceRequestor);

                    if (req != null)
                    {
                        try
                        {
                            req.GetInterface(ref iid, out ppv);
                            // clean up
                            Marshal.ReleaseComObject(req);
                        }
                        catch (NullReferenceException ex)
                        {
                            Debug.WriteLine("NullRefException from native code.\n" + ex.ToString());
                        }
                    }
                    Marshal.Release(pInterfaceRequestor);
                }
            }

            object result = (ppv != IntPtr.Zero) ? Xpcom.GetObjectForIUnknown(ppv) : null;

            Marshal.Release(pUnk);
            if (ppv != IntPtr.Zero)
            {
                Marshal.Release(ppv);
            }

            return(result);
        }