Execute code on assembly load

Posted by Dmitriy Matveev on Stack Overflow See other posts from Stack Overflow or by Dmitriy Matveev
Published on 2009-06-13T03:04:02Z Indexed on 2010/03/20 0:01 UTC
Read the original article Hit count: 387

Filed under:
|

I'm working on wrapper for some huge unmanaged library. Almost every of it's functions can call some error handler deeply inside. The default error handler writes error to console and calls abort() function. This behavior is undesirable for managed library, so I want to replace the default error handler with my own which will just throw some exception and let program continue normal execution after handling of this exception. The error handler must be changed before any of the wrapped functions will be called.
The wrapper library is written in managed c++ with static linkage to wrapped library, so nothing like "a type with hundreds of dll imports" is present. I also can't find a single type which is used by everything inside wrapper library. So I can't solve that problem by defining static constructor in one single type which will execute code I need.

I currently see two ways of solving that problem:

  1. Define some static method like Library.Initialize() which must be called one time by client before his code will use any part of the wrapper library.

  2. Find the most minimal subset of types which is used by every top-level function (I think the size of this subset will be something like 25-50 types) and add static constructors calling Library.Initialize (which will be internal in that scenario) to every of these types.

I've read this and this questions, but they didn't helped me. Is there any proper ways of solving that problem? Maybe some nice hacks available?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about assemblies