在 C/C 中捕获 DLL 崩溃++
我从 DLL 中调用函数,如下所示:
__declspec ( dllimport ) bool dll_function(...);
int main() {
[...]
if (dll_function(...)) {
[...]
}
}
在某些情况下,我传递给 DLL 函数的数据将导致 DLL 崩溃。是否可以捕获此问题,以便我的应用程序也不会崩溃(无需修改不是由我创建的 DLL)?
I'm calling a function from a DLL, like this:
__declspec ( dllimport ) bool dll_function(...);
int main() {
[...]
if (dll_function(...)) {
[...]
}
}
In some cases, the data I pass to the DLL function will lead to a crash of the DLL. Is it possible to catch this so my application doesn't crash as well (without modifying the DLL which is not created by me)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 MSVC 编译器中使用 __try 和 __ except 关键字捕获 AV。没什么用处,你不知道造成了什么样的损害。您的程序的状态很可能已损坏。例如,堆可能会被破坏,从而导致随后的随机故障。将 DLL 托管在自己的进程中并使用 IPC 与其通信是唯一合适的方法。
You can catch AVs with the __try and __except keywords in the MSVC compiler. Not all that useful, you have no idea what kind of damage was done. The state of your program might well be corrupted. The heap might be blown for example, causing subsequent random failure. Hosting the DLL in its own process and using IPC to talk to it is the only decent approach.
如果只调用带有有效数据的函数,不是可以防止dll崩溃吗?无论如何,这应该是更好的解决方案 - 但如果不知道您想要使用哪个 dll,就很难判断。但在大多数情况下,您应该知道什么“数据”到底会导致崩溃......
Isn't it possible to prevent the dll from crashing if you only call the function with valid data? That should be the preferable solution in any case - but its hard to tell without knowing which dll you want to use. But in most cases, you should have an idea what "data" exactly results in an crash...
尝试查看:
http://msdn.microsoft .com/en-us/library/ms680634%28v=vs.85%29.aspx
和
强制过滤代码,作者:Oleg Starodumov (www.debuginfo.com)
http://www.debuginfo.com/articles/debugfilters.html
但是,这是一个顶级过滤器,而不是 try/catch。您也许可以重新启动您的进程。
您可能需要使用 __try 来处理异常。再说一遍,解决问题或直接崩溃可能比试图抓住它更好。
我同意其他人的观点,而不是抑制或隐藏崩溃,你应该修复它。我不知道你能从崩溃中恢复到什么程度——在发生类似的事情之后继续执行会有用吗?
Try looking at:
http://msdn.microsoft.com/en-us/library/ms680634%28v=vs.85%29.aspx
and
Enforce Filter code by Oleg Starodumov (www.debuginfo.com)
http://www.debuginfo.com/articles/debugfilters.html
However, that is a top level filter and not a try/catch. You can perhaps restart your process.
You might need to use __try for exceptions. Again, probably better to fix the problem or just crash than to try to catch it.
I agree with the others that rather than suppressing or hiding the crash you should fix it. I don't know how well you can recover from the crash - is it going to be useful to continue execution after something like that?
我不确定这是否是问题所在,请尝试指定正确的调用约定。 (
__stdcall
、__cdecl
等)。如果这不是问题,我们需要查看您传递给函数的内容,以及可能的函数代码(如果有)。
I'm not sure if this is the problem, try specifying the correct calling convention. (
__stdcall
,__cdecl
, etc).If that's not the problem, we need to see what you are passing to the function and possibly the function code if you have it.