问题
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?
回答1:
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.
回答2:
In C++11 you call std::get_terminate.
回答3:
You mean somthing like this:
terminate_handler oldHandler;
void onDllLoad()
{
oldHandler = set_terminate (newHandler);
}
void onDllUnload()
{
set_terminate (oldHandler);
}
void newHandler()
{
}
来源:https://stackoverflow.com/questions/2200257/how-can-i-retrieve-current-terminate-handler-without-changing-it