views:

50

answers:

2

Here's the problem. My application calls CoCreateInstance() to create a COM object implemented in a third-party DLL. That DLL calls set_terminate() to change the terminate() handler and passes an address of its own terminate() handler there.

The initial terminate() handler address is not saved by that library - it doesn't care and simply changes the handler and never restores it. As soon as the DLL gets unloaded its code is no longer in the process memory, so if now terminate() is called the program runs into undefined behavior.

I'd like to retrieve and store the address of initial terminate() handler to be able to restore it. How can I do it?

+6  A: 

Standard C++ provides no built-in way.

Of course you could just call terminate() twice: first time with whatever dummy handler you have (and then store handler that terminate() returned you); second -- to restore handler you've just stored ;) Simple trick.

Alexander Poluektov
+1  A: 

You mean somthing like this:

terminate_handler oldHandler;

void onDllLoad()
{
    oldHandler = set_terminate (newHandler);
}

void onDllUnload()
{
    set_terminate (oldHandler);
}

void newHandler()
{
}
Martin York