private async void btnCreate_Click(object sender, EventArgs e) { try { object comObj = m_clsid.CreateInstanceAsObject(m_clsid.CreateContext, null); if (comObj != null) { await EntryPoint.GetMainForm(m_registry).HostObject(m_clsid, comObj, false); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private async void menuObjectFromFile_Click(object sender, EventArgs e) { try { using (OpenFileDialog dlg = new OpenFileDialog()) { dlg.Filter = "All Files (*.*)|*.*"; if (dlg.ShowDialog(this) == DialogResult.OK) { COMCLSIDEntry entry = m_registry.GetFileClass(dlg.FileName); if (entry != null) { IPersistFile ps = (IPersistFile)entry.CreateInstanceAsObject(entry.CreateContext, null); ps.Load(dlg.FileName, (int)STGM.READ); await HostObject(entry, ps, false); } } } } catch (Exception ex) { MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
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); } }