CoGetClassObject() приватный Метод

private CoGetClassObject ( System.Guid &rclsid, CLSCTX dwClsContext, IntPtr pServerInfo, System.Guid &riid, IntPtr &ppv ) : int
rclsid System.Guid
dwClsContext CLSCTX
pServerInfo System.IntPtr
riid System.Guid
ppv System.IntPtr
Результат int
Пример #1
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);
                }
            }
        }
Пример #2
0
        public object CreateClassFactory(string server)
        {
            IntPtr obj;
            Guid   iid   = COMInterfaceEntry.IID_IUnknown;
            Guid   clsid = Clsid;

            COSERVERINFO server_info = server != null ? new COSERVERINFO(server) : null;

            int hr = COMUtilities.CoGetClassObject(ref clsid, server_info != null ? CLSCTX.REMOTE_SERVER
                : CreateContext, server_info, ref iid, out obj);

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

            object ret = Marshal.GetObjectForIUnknown(obj);

            Marshal.Release(obj);
            return(ret);
        }
Пример #3
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);
            }
        }
Пример #4
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);
                }
            }
        }
Пример #5
0
        private async void menuViewCreateInstanceFromCLSID_Click(object sender, EventArgs e)
        {
            using (CreateCLSIDForm frm = new CreateCLSIDForm())
            {
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        COMCLSIDEntry ent = null;
                        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))
                        {
                            ent        = m_registry.Clsids[g];
                            strObjName = ent.Name;
                            props.Add("CLSID", ent.Clsid.FormatGuid());
                            props.Add("Name", ent.Name);
                            props.Add("Server", ent.DefaultServer);
                            await ent.LoadSupportedInterfacesAsync(false);

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

                            if (frm.ClassFactory)
                            {
                                hr = COMUtilities.CoGetClassObject(ref g, frm.ClsCtx, null, ref unk, out pObj);
                            }
                            else
                            {
                                hr = COMUtilities.CoCreateInstance(ref g, IntPtr.Zero, frm.ClsCtx,
                                                                   ref unk, out pObj);
                            }

                            if (hr == 0)
                            {
                                try
                                {
                                    ints       = m_registry.GetInterfacesForIUnknown(pObj);
                                    comObj     = Marshal.GetObjectForIUnknown(pObj);
                                    strObjName = g.FormatGuid();
                                    props.Add("CLSID", g.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);
                    }
                }
            }
        }