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); } }
private void GetInterfacesInternal(NtToken token) { IntPtr punk = IntPtr.Zero; IntPtr pfactory = IntPtr.Zero; Guid IID_IUnknown = COMInterfaceEntry.IID_IUnknown; CLSCTX clsctx = _clsctx; if (token != null) { clsctx |= CLSCTX.ENABLE_CLOAKING; } using (var imp = token?.Impersonate()) { 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()) { if (Guid.TryParse(iid_string, out Guid 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); } } } }