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

夙愿已清 提交于 2019-12-24 18:18:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!