public void Run() { int hResult; #if false Thread.CurrentThread.SetApartmentState(ApartmentState.STA); hResult = ComAPI.CoInitializeSecurity( IntPtr.Zero, // Add here your Security descriptor -1, IntPtr.Zero, IntPtr.Zero, ComAPI.RPC_C_AUTHN_LEVEL_PKT_PRIVACY, ComAPI.RPC_C_IMP_LEVEL_IDENTIFY, IntPtr.Zero, ComAPI.EOAC_DISABLE_AAA | ComAPI.EOAC_SECURE_REFS | ComAPI.EOAC_NO_CUSTOM_MARSHAL, IntPtr.Zero); if (hResult != 0) { throw new ApplicationException("CoIntializeSecurity failed" + hResult.ToString("X")); } #endif Guid CLSID_MyObject = typeof(CoClass).GUID; hResult = ComAPI.CoRegisterClassObject( ref CLSID_MyObject, this, ComAPI.CLSCTX_LOCAL_SERVER, ComAPI.REGCLS_SINGLEUSE, out _cookie); if (hResult != 0) { Marshal.ThrowExceptionForHR(hResult); } _nLockCnt = 0; try { using ( var timer = new Timer( new TimerCallback((s) => { GC.Collect(); }), null, 5000, 5000 ) ) { _track.WaitOne(); } } finally { ComAPI.CoRevokeClassObject(_cookie); } }
[ComUnregisterFunction] static void Unregister(Type t) { ComAPI.RegasmUnregisterLocalServer(t); }