【问题标题】:Try catch blocks in C++ dll's in MVS 2010在 MVS 2010 中尝试 C++ dll 中的 catch 块
【发布时间】:2013-09-23 07:29:14
【问题描述】:

我有一个代码可以导入一个 dll 并多次调用它的函数。对于某些输入,dll 函数会引发异常,但对于其他输入,它可以正常工作。在对 dll here 中的异常进行了一些研究之后,似乎在 dll 的情况下运行时异常没有以一种简单的方法处理。

这是我的代码

int main( void ) 
{ 
    WORD_DATABASE wd=parse_data();

    const char* WorkingDirPath="C:\\Users\\Koustav\\Dropbox\\Project\\July07_PT
    int UserID=1;
    DEVICE_INFO_T test= TOP_LEFT;

    HINSTANCE hinstLib; 
    //    MYPROC ProcAdd; 
    BOOL fFreeResult, fRunTimeLinkSuccess = FALSE; 

    // Get a handle to the DLL module.

    hinstLib = LoadLibrary(TEXT("Recog_Malayalam.dll"));
    // If the handle is valid, try to get the function address.pppppp

    if (hinstLib != NULL)


    { 



    f_funci init = (f_funci)GetProcAddress(hinstLib,"abc");
    f_funci1 reco = (f_funci1)GetProcAddress(hinstLib,"xyz");
    // If the function address is valid, call the function.

    int a = init(WorkingDirPath,1,(DEVICE_INFO_T)1);


    for (int c3=0;c3<120;c3++)
    {   
        try{
            "<<wd.annotation_detail[c3].uni_val<<endl;
            for (int c4=0;c4<wd.annotation_detail[c3].stroke_count;c4++)
            {
                log_cl<<wd.annotation_detail[c3].stroke_labels[c4]-1<<" ";
            }
            log_cl<<"Actual Values"<<endl;
            cout<<endl;
            //cout<<"Supplied stroke label"<<wd.annotation_detail[c3].stroke_labels[0]<<endl;
            int b=0;
            try{
                b = reco(wd.word_db[c3],wd.annotation_detail[c3].stroke_count,PLAIN,0,'\0',1);
            }
            catch(exception e){
                b=0;
                cout<<"try_catch_success"<<endl;
            }
            //cout<<"Supplied stroke label"<<wd.annotation_detail[c3].stroke_labels[0]<<endl;
            cout<<endl;


        }




        catch(exception e){
            cout<<"There is an exception"<<endl;
        }
    }
    fFreeResult = FreeLibrary(hinstLib); 


} 


// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess) 


    getch();
return 0;

}

我也在 dll 中使用了 try catch 块。错误来自多次调用的第二个函数。

我已经更改了herehere 中提到的项目属性。但我仍然收到此错误。 (我已经更改了创建 dll 的项目的属性以及我调用 dll 的项目的属性)

假设我无法访问 dll ,我该如何修复此代码?如果不可能,如果我可以访问dll,是否有可能?

任何帮助将不胜感激

【问题讨论】:

  • 您确定问题不在您的代码中吗?我的意思是 wd.word_db, annotation_detail ... 向量?您是否会超出其中之一的范围?
  • 我已经检查了很多次。看起来不错。说我的断言失败是因为你提到的。有什么方法可以跟踪错误吗?
  • 如果您在点击此对话框时按重试,调试器会将您带到导致问题的行。如果您然后退回调用堆栈,应该很容易确定问题是在您的代码中还是在 dll 中。

标签: c++ visual-studio-2010 exception dll


【解决方案1】:

Try-catch 用于异常。此对话框用于断言。不同的野兽,几乎没有关系。

【讨论】:

  • 有没有办法绕过这些断言失败,就像 try catch 块在异常中所做的那样?
  • 你也不能绕过异常。 try catch 允许您继续,但一般情况下您不能对之后的程序状态说任何话。出现未记录的异常后的最佳建议是将用户的工作保存到新文件中。
  • 我可以在此断言失败中以某种方式使用“继续”吗?但是我怎么知道 DLL 中发生此故障的确切行?
  • @KoustavGhosal:检查调用堆栈。但是断言消息很清楚:DLL 使用了std::vector,但传递了一个无效的索引(即超过了结尾)。
  • 感谢已修复。有一个变量被声明为 const,它用于索引可变长度数组。因此出现错误。
猜你喜欢
  • 1970-01-01
  • 2021-11-26
  • 2010-12-25
  • 1970-01-01
  • 2023-03-18
  • 2023-03-15
  • 1970-01-01
  • 2011-07-05
  • 1970-01-01
相关资源
最近更新 更多