public ClassFactoryRegistration(Type type, CLSID clsId) { ClassFactory factory = new ClassFactory(type); IntPtr pFactory = Marshal.GetIUnknownForObject(factory); HRESULT result = ComAPI.CoRegisterClassObject(ref clsId, pFactory, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, out _classRegister); if (result != ComAPI.S_OK) { throw new InvalidOperationException("CoRegisterClassObject failed."); } }
internal static HRESULT DllGetClassObject(CLSID clsid, IID iid, out IntPtr ppunk) { if (iid != ComAPI.guidIClassFactory) { ppunk = IntPtr.Zero; return ComAPI.E_INVALIDARG; } foreach (ExcelComClassType comClass in registeredComClassTypes) { if (comClass.ClsId == clsid) { ClassFactory factory = new ClassFactory(comClass); IntPtr punkFactory = Marshal.GetIUnknownForObject(factory); HRESULT hrQI = Marshal.QueryInterface(punkFactory, ref iid, out ppunk); Marshal.Release(punkFactory); if (hrQI == ComAPI.S_OK) { return ComAPI.S_OK; } else { return ComAPI.E_UNEXPECTED; } } } ppunk = IntPtr.Zero; return ComAPI.CLASS_E_CLASSNOTAVAILABLE; }
static bool TryGetComClassType(CLSID clsId, out ComAPI.IClassFactory factory) { // Check among the persistently registered classes foreach (ExcelComClassType comClass in registeredComClassTypes) { if (comClass.ClsId == clsId) { factory = new ClassFactory(comClass); return true; } } factory = null; return false; }