#define BOOTUP_EXCEPTION_COMPLUS  0xC0020001

 

You may see an exception with this code, or an HRESULT of this value, when trying to call into managed code.  This can happen if you call in before the runtime has finished initializing, or after the runtime has started shutting down.

 

Sometimes it’s obvious how you run into this situation.  For example, if you call into managed code via a COM object or a marshaled delegate from your unmanaged DllMain while processing a DLL_PROCESS_DETACH notification, then it’s quite likely that the runtime has already shut itself down.

 

Other times, it’s not so obvious.  In particular, you might see this exception being raised when a Windows message is being processed by a thread that is pumping messages during shutdown.  If there are still some managed WinProcs registered, then a message might be dispatched to one of them.  When this happens, the runtime will throw the above exception code to indicate that managed execution is no longer possible.  The WinProcs should have been unregistered by code listening to the AppDomain.ProcessExit event (and the AppDomain.DomainUnload event if you have multiple AppDomains in your process).  In the case of WindowsForms, this is handled for you automatically.  But if you are building your own windowing system, this becomes your responsibility.