views:

471

answers:

3

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?

A: 

I think your option 2 is better. If you include the call in a few extra types, so be it. Of course, you would have Library.initialize bail out if it's already been called.

Matthew Flaschen
A: 

Might be a bit of a stretch, but you could create your own wrapper class that exposes only the functions that you want. Within each of those functions you could perform your error handling.

Hugoware
A: 

A couple of other suggestions:

  1. Create an abstract base class as the root for all the wrapper classes and put the initialize call in the constructor for the base class.
  2. Give all the objects internal constructors and force clients to instantiate them through some factory method and then perform the initialization before returning an object instance.
Venr