CoCreateInstance() private method

private CoCreateInstance ( System.Guid &rclsid, IntPtr pUnkOuter, CLSCTX dwClsContext, System.Guid &riid, IntPtr &ppv ) : int
rclsid System.Guid
pUnkOuter System.IntPtr
dwClsContext CLSCTX
riid System.Guid
ppv System.IntPtr
return int
Exemplo n.º 1
0
        public static object OleLoadFromStream(Stream stm)
        {
            using (BinaryReader reader = new BinaryReader(stm))
            {
                Guid clsid = new Guid(reader.ReadBytes(16));

                Guid   unk = COMInterfaceEntry.IID_IUnknown;
                IntPtr pObj;
                object ret;

                int iError = COMUtilities.CoCreateInstance(ref clsid, IntPtr.Zero, CLSCTX.CLSCTX_SERVER,
                                                           ref unk, out pObj);

                if (iError != 0)
                {
                    Marshal.ThrowExceptionForHR(iError);
                }

                ret = Marshal.GetObjectForIUnknown(pObj);
                Marshal.Release(pObj);

                LoadObjectFromStream(ret, stm);

                return(ret);
            }
        }
Exemplo n.º 2
0
        private async void menuViewCreateInstanceFromCLSID_Click(object sender, EventArgs e)
        {
            using (CreateCLSIDForm frm = new CreateCLSIDForm())
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        Guid g = frm.Clsid;
                        Dictionary <string, string> props = new Dictionary <string, string>();
                        object comObj     = null;
                        string strObjName = "";
                        IEnumerable <COMInterfaceEntry> ints = null;

                        if (m_registry.Clsids.ContainsKey(g))
                        {
                            COMCLSIDEntry ent = m_registry.Clsids[g];
                            strObjName = ent.Name;
                            props.Add("CLSID", ent.Clsid.ToString("B"));
                            props.Add("Name", ent.Name);
                            props.Add("Server", ent.Server);

                            comObj = ent.CreateInstanceAsObject(frm.ClsCtx);
                            await ent.LoadSupportedInterfacesAsync(false);
                        }
                        else
                        {
                            Guid   unk = COMInterfaceEntry.IID_IUnknown;
                            IntPtr pObj;

                            if (COMUtilities.CoCreateInstance(ref g, IntPtr.Zero, frm.ClsCtx,
                                                              ref unk, out pObj) == 0)
                            {
                                ints       = m_registry.GetInterfacesForIUnknown(pObj);
                                comObj     = Marshal.GetObjectForIUnknown(pObj);
                                strObjName = g.ToString("B");
                                props.Add("CLSID", g.ToString("B"));
                                Marshal.Release(pObj);
                            }
                        }

                        if (comObj != null)
                        {
                            /* Need to implement a type library reader */
                            Type dispType = COMUtilities.GetDispatchTypeInfo(comObj);

                            HostControl(new ObjectInformation(m_registry, strObjName, comObj, props, ints.ToArray()));
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void GetInterfacesInternal()
        {
            IntPtr punk         = IntPtr.Zero;
            IntPtr pfactory     = IntPtr.Zero;
            Guid   IID_IUnknown = COMInterfaceEntry.IID_IUnknown;

            int hr = 0;

            hr = COMUtilities.CoGetClassObject(ref _clsid, _clsctx, null, ref IID_IUnknown, out pfactory);
            // If we can't get class object, no chance we'll get object.
            if (hr != 0)
            {
                throw new Win32Exception(hr);
            }

            hr = COMUtilities.CoCreateInstance(ref _clsid, IntPtr.Zero, _clsctx,
                                               ref IID_IUnknown, out punk);
            if (hr != 0)
            {
                punk = IntPtr.Zero;
            }

            try
            {
                Dictionary <IntPtr, string> module_names = new Dictionary <IntPtr, string>();
                QueryInterface(punk, COMInterfaceEntry.IID_IMarshal, module_names, _interfaces);
                QueryInterface(pfactory, COMInterfaceEntry.IID_IMarshal, module_names, _factory_interfaces);
                QueryInterface(punk, COMInterfaceEntry.IID_IPSFactoryBuffer, module_names, _interfaces);
                QueryInterface(pfactory, COMInterfaceEntry.IID_IPSFactoryBuffer, module_names, _factory_interfaces);

                using (RegistryKey interface_key = Registry.ClassesRoot.OpenSubKey("Interface"))
                {
                    foreach (string iid_string in interface_key.GetSubKeyNames())
                    {
                        Guid iid;
                        if (Guid.TryParse(iid_string, out iid))
                        {
                            QueryInterface(punk, iid, module_names, _interfaces);
                            QueryInterface(pfactory, iid, module_names, _factory_interfaces);
                        }
                    }
                }
            }
            finally
            {
                if (pfactory != IntPtr.Zero)
                {
                    Marshal.Release(pfactory);
                }

                if (punk != IntPtr.Zero)
                {
                    Marshal.Release(punk);
                }
            }
        }
Exemplo n.º 4
0
        private IMoniker ParseMoniker(IBindCtx bind_context, string moniker_string)
        {
            if (moniker_string == "new")
            {
                Guid   IID_IUnknown = COMInterfaceEntry.IID_IUnknown;
                IntPtr unk;
                int    hr = COMUtilities.CoCreateInstance(ref CLSID_NewMoniker, IntPtr.Zero, CLSCTX.INPROC_SERVER, ref IID_IUnknown, out unk);
                if (hr != 0)
                {
                    Marshal.ThrowExceptionForHR(hr);
                }

                try
                {
                    return((IMoniker)Marshal.GetObjectForIUnknown(unk));
                }
                finally
                {
                    Marshal.Release(unk);
                }
            }
            else
            {
                if (moniker_string.StartsWith("file:", StringComparison.OrdinalIgnoreCase) ||
                    moniker_string.StartsWith("http:", StringComparison.OrdinalIgnoreCase) ||
                    moniker_string.StartsWith("https:", StringComparison.OrdinalIgnoreCase))
                {
                    IMoniker moniker;
                    int      hr = COMUtilities.CreateURLMonikerEx(null, moniker_string, out moniker, CreateUrlMonikerFlags.Uniform);
                    if (hr != 0)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }
                    return(moniker);
                }

                int eaten = 0;
                return(COMUtilities.MkParseDisplayName(bind_context, moniker_string, out eaten));
            }
        }
Exemplo n.º 5
0
        public IntPtr CreateInstance(CLSCTX dwContext)
        {
            IntPtr pInterface = IntPtr.Zero;
            bool   blValid    = false;

            if (dwContext == CLSCTX.CLSCTX_ALL)
            {
                if (ServerType == COMServerType.InProcServer32)
                {
                    dwContext = CLSCTX.CLSCTX_INPROC_SERVER;
                    blValid   = true;
                }
                else if (ServerType == COMServerType.LocalServer32)
                {
                    dwContext = CLSCTX.CLSCTX_LOCAL_SERVER;
                    blValid   = true;
                }
            }
            else
            {
                blValid = true;
            }

            if (blValid)
            {
                Guid iid    = COMInterfaceEntry.CreateKnownInterface(COMInterfaceEntry.KnownInterfaces.IUnknown).Iid;
                Guid clsid  = Clsid;
                int  iError = COMUtilities.CoCreateInstance(ref clsid, IntPtr.Zero, dwContext, ref iid, out pInterface);

                if (iError != 0)
                {
                    Marshal.ThrowExceptionForHR(iError);
                }
            }

            return(pInterface);
        }
Exemplo n.º 6
0
        public async Task CreateInstanceFromCLSID(Guid clsid, CLSCTX clsctx, bool class_factory)
        {
            try
            {
                COMCLSIDEntry ent = null;
                Dictionary <string, string> props = new Dictionary <string, string>();
                object comObj     = null;
                string strObjName = "";
                IEnumerable <COMInterfaceEntry> ints = null;

                if (m_registry.Clsids.ContainsKey(clsid))
                {
                    ent        = m_registry.Clsids[clsid];
                    strObjName = ent.Name;
                    props.Add("CLSID", ent.Clsid.FormatGuid());
                    props.Add("Name", ent.Name);
                    props.Add("Server", ent.DefaultServer);
                    await ent.LoadSupportedInterfacesAsync(false, null);

                    if (class_factory)
                    {
                        comObj = ent.CreateClassFactory();
                        ints   = ent.FactoryInterfaces.Select(i => m_registry.MapIidToInterface(i.Iid));
                    }
                    else
                    {
                        comObj = ent.CreateInstanceAsObject(clsctx, null);
                        ints   = ent.Interfaces.Select(i => m_registry.MapIidToInterface(i.Iid));
                    }
                }
                else
                {
                    Guid   unk = COMInterfaceEntry.IID_IUnknown;
                    IntPtr pObj;
                    int    hr;

                    if (class_factory)
                    {
                        hr = COMUtilities.CoGetClassObject(ref clsid, clsctx, null, ref unk, out pObj);
                    }
                    else
                    {
                        hr = COMUtilities.CoCreateInstance(ref clsid, IntPtr.Zero, clsctx,
                                                           ref unk, out pObj);
                    }

                    if (hr != 0)
                    {
                        Marshal.ThrowExceptionForHR(hr);
                    }

                    try
                    {
                        ints       = m_registry.GetInterfacesForIUnknown(pObj).ToArray();
                        comObj     = Marshal.GetObjectForIUnknown(pObj);
                        strObjName = clsid.FormatGuid();
                        props.Add("CLSID", clsid.FormatGuid());
                    }
                    finally
                    {
                        Marshal.Release(pObj);
                    }
                }

                if (comObj != null)
                {
                    /* Need to implement a type library reader */
                    Type dispType = COMUtilities.GetDispatchTypeInfo(this, comObj);

                    HostControl(new ObjectInformation(m_registry, ent, strObjName, comObj, props, ints.ToArray()));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 7
0
        private void GetInterfacesInternal()
        {
            IntPtr punk         = IntPtr.Zero;
            IntPtr pfactory     = IntPtr.Zero;
            Guid   IID_IUnknown = COMInterfaceEntry.IID_IUnknown;

            int hr = 0;

            if (_winrt_component)
            {
                hr = COMUtilities.RoGetActivationFactory(_activatable_classid, ref IID_IUnknown, out pfactory);
            }
            else
            {
                hr = COMUtilities.CoGetClassObject(ref _clsid, _clsctx, null, ref IID_IUnknown, out pfactory);
            }
            // If we can't get class object, no chance we'll get object.
            if (hr != 0)
            {
                throw new Win32Exception(hr);
            }

            if (_winrt_component)
            {
                hr = COMUtilities.RoActivateInstance(_activatable_classid, out punk);
            }
            else
            {
                hr = COMUtilities.CoCreateInstance(ref _clsid, IntPtr.Zero, _clsctx,
                                                   ref IID_IUnknown, out punk);
            }
            if (hr != 0)
            {
                punk = IntPtr.Zero;
            }

            try
            {
                Dictionary <IntPtr, string> module_names = new Dictionary <IntPtr, string>();
                QueryInterface(punk, COMInterfaceEntry.IID_IMarshal, module_names, _interfaces);
                QueryInterface(pfactory, COMInterfaceEntry.IID_IMarshal, module_names, _factory_interfaces);
                QueryInterface(punk, COMInterfaceEntry.IID_IPSFactoryBuffer, module_names, _interfaces);
                QueryInterface(pfactory, COMInterfaceEntry.IID_IPSFactoryBuffer, module_names, _factory_interfaces);

                var actctx = ActivationContext.FromProcess();
                if (actctx != null)
                {
                    foreach (var intf in actctx.ComInterfaces)
                    {
                        QueryInterface(punk, intf.Iid, module_names, _interfaces);
                        QueryInterface(pfactory, intf.Iid, module_names, _factory_interfaces);
                    }
                }

                using (RegistryKey interface_key = Registry.ClassesRoot.OpenSubKey("Interface"))
                {
                    foreach (string iid_string in interface_key.GetSubKeyNames())
                    {
                        Guid iid;
                        if (Guid.TryParse(iid_string, out iid))
                        {
                            QueryInterface(punk, iid, module_names, _interfaces);
                            QueryInterface(pfactory, iid, module_names, _factory_interfaces);
                        }
                    }
                }

                QueryInspectableInterfaces(punk, module_names, _interfaces);
                QueryInspectableInterfaces(pfactory, module_names, _factory_interfaces);
            }
            finally
            {
                if (pfactory != IntPtr.Zero)
                {
                    Marshal.Release(pfactory);
                }

                if (punk != IntPtr.Zero)
                {
                    Marshal.Release(punk);
                }
            }
        }
Exemplo n.º 8
0
        public IntPtr CreateInstance(CLSCTX dwContext, string server)
        {
            IntPtr pInterface = IntPtr.Zero;

            if (dwContext == CLSCTX.ALL)
            {
                if (DefaultServerType == COMServerType.InProcServer32)
                {
                    dwContext = CLSCTX.INPROC_SERVER;
                }
                else if (DefaultServerType == COMServerType.LocalServer32)
                {
                    dwContext = CLSCTX.LOCAL_SERVER;
                }
                else if (DefaultServerType == COMServerType.InProcHandler32)
                {
                    dwContext = CLSCTX.INPROC_HANDLER;
                }
                else
                {
                    dwContext = CLSCTX.SERVER;
                }
            }

            Guid iid   = COMInterfaceEntry.CreateKnownInterface(COMInterfaceEntry.KnownInterfaces.IUnknown).Iid;
            Guid clsid = Clsid;

            int hr = 0;

            if (server != null)
            {
                MULTI_QI[] qis = new MULTI_QI[1];
                qis[0] = new MULTI_QI(iid);
                COSERVERINFO server_info = new COSERVERINFO(server);
                try
                {
                    hr = COMUtilities.CoCreateInstanceEx(ref clsid, IntPtr.Zero, dwContext, server_info, 1, qis);
                    if (hr == 0)
                    {
                        hr = qis[0].HResult();
                        if (hr == 0)
                        {
                            pInterface = qis[0].GetObjectPointer();
                        }
                    }
                }
                finally
                {
                    ((IDisposable)qis[0]).Dispose();
                }
            }
            else
            {
                hr = COMUtilities.CoCreateInstance(ref clsid, IntPtr.Zero, dwContext, ref iid, out pInterface);
            }

            if (hr != 0)
            {
                Marshal.ThrowExceptionForHR(hr);
            }

            return(pInterface);
        }