How can I get the current terminate () handler without changing it?

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

The start address of the terminate() handler is not stored by this library - it doesn’t care and just changes the handler and never restores it. As soon as the DLL is unloaded, its code is no longer in the process memory, therefore, if terminate() is now called, the program starts in undefined behavior.

I want to get and save the address of the initial terminate() handler so that I can recover it. How can i do this?

0
source share
3 answers

Standard C ++ does not have a built-in method.

Of course, you could just call terminate () twice: the first time with some dummy handler that you have (and then save the handler that completes () returned you); the second is to restore the handler that you just saved;) A simple trick.

+6
source

In C ++ 11, you call std :: get_terminate.

+2
source

You mean something like this:

 terminate_handler oldHandler; void onDllLoad() { oldHandler = set_terminate (newHandler); } void onDllUnload() { set_terminate (oldHandler); } void newHandler() { } 
+1
source

Source: https://habr.com/ru/post/1392633/


All Articles