예제 #1
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);
                }
            }
        }