在 C/C 中捕获 DLL 崩溃++

发布于 2024-10-16 06:17:41 字数 247 浏览 3 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深空失忆 2024-10-23 06:17:41

您可以在 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.

焚却相思 2024-10-23 06:17:41

在某些情况下,我传递给的数据
DLL函数会导致崩溃
DLL。有可能抓住这个吗
所以我的应用程序不会崩溃
嗯?

如果只调用带有有效数据的函数,不是可以防止dll崩溃吗?无论如何,这应该是更好的解决方案 - 但如果不知道您想要使用哪个 dll,就很难判断。但在大多数情况下,您应该知道什么“数据”到底会导致崩溃......

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?

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...

烟雨扶苏 2024-10-23 06:17:41

尝试查看:

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?

只为一人 2024-10-23 06:17:41

我不确定这是否是问题所在,请尝试指定正确的调用约定。 (__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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文