views:

43

answers:

0

I have build a .net library that uses the ManagementEventWatcher class. My library is disposable, so normally I would wrap it in a using statement and the ManagementEventWatcher class would get disposed by my library.

My issue is that my library is exposed to COM, and gets used in VB6 which doesn't use the disposable pattern. If a user does not call dispose on the library from within their .net application, or can't because of VB6, the ManagementEventWatcher class will throw an InvalidComObjectException from within SinkForEventQuery.Cancel

I can't capture the exception, so it remains unhandled, which is not good. Are there some workarounds I can try?

System.Runtime.InteropServices.InvalidComObjectException was unhandled
  Message=COM object that has been separated from its underlying RCW cannot be used.
  Source=mscorlib
  StackTrace:
       at System.StubHelpers.StubHelpers.StubRegisterRCW(Object pThis, IntPtr pThread)
       at System.Management.IWbemServices.CancelAsyncCall_(IWbemObjectSink pSink)
       at System.Management.SinkForEventQuery.Cancel()
       at System.Management.ManagementEventWatcher.Stop()
       at System.Management.ManagementEventWatcher.Finalize()
  InnerException: