gpt4 book ai didi

调用dll函数后未捕获C++异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:58:32 27 4
gpt4 key购买 nike

我遇到了以下问题:有导出函数的DLL。

代码示例如下:[动态链接库]

__declspec(dllexport) int openDevice(int,void**)

[应用]

开发者.h:

__declspec(dllimport) int openDevice(int,void**)

开发者.cpp:

try {
void *p = NULL;
int devError = openDevice(some_integer,&p);
if(devError)
throw (int)devError;
} catch(int i) {
cerr << "Device opening error: " << i << endl;
}
catch(...) {
//other handler
}

异常没有传递到 catch block ,我不知道为什么。请帮忙解决这个问题。我在 MSVC 9.0 中使用 Qt Creator。

最佳答案

首先,删除 throw 表达式中的 (int) 不安全转换。

然后,在if(devError) block 中添加一个cerr语句来证明它确实被执行了。

然后,编写一个测试程序来证明异常是有效的。只是一个抛出和捕获的 main()

关于调用dll函数后未捕获C++异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13799042/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com