开发者

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

开发者 https://www.devze.com 2022-12-18 22:33 出处:网络
Here\'s the problem. My applica开发者_Go百科tion calls CoCreateInstance() to create a COM object implemented in a third-party DLL. That DLL calls set_terminate() to change the terminate() handler and

Here's the problem. My applica开发者_Go百科tion 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?


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.


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


You mean somthing like this:

terminate_handler oldHandler;

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

void onDllUnload()
{
    set_terminate (oldHandler);
}

void newHandler()
{
}
0

精彩评论

暂无评论...
验证码 换一张
取 消