【问题标题】:What exception type does C++ see when a C# class marked ComVisible throws an exception?当标记为 ComVisible 的 C# 类抛出异常时,C++ 会看到什么异常类型?
【发布时间】:2019-08-19 18:31:06
【问题描述】:

我有一个标记为 ComVisible 的 C# 类,它有一个写入文件的函数。如果文件应该写入的文件夹不存在,则会引发 System.IO.DirectoryNotFoundException。如果我使用 throw;将它提升回 C++ 客户端,它不会被我知道的任何处理程序捕获,除了通用 (...) 处理程序。处理程序将获取的异常对象的类型是什么?

这是客户端方法:

void CRXReport::Export(CCOMString Destination)
{
    CWaitCursor Wait;
    // m_Report->Export("c:/misc/report2.pdf");
    CCOMString message;
    message << _T("Trying to export a report to ") << Destination;
    AfxMessageBox(message);
    if ( m_Report != NULL ) 
    {
        try
        {
            m_Report->Export(Destination.AllocSysString());
        }
        catch (CException& ex)
        {
            AfxMessageBox(_T("Failed to export the report; caught a CException reference."));
        }
        catch (CException* pEx)
        {
            AfxMessageBox(_T("Failed to export the report; caught a CException pointer."));
        }
        catch (_com_error* e)
        {
            AfxMessageBox(_T("Failed to export the report; caught a _com_error reference."));
        }
        catch (...)
        {
            AfxMessageBox(_T("Failed to export the report; caught something else."));
        }
    }
}

而且,虽然我认为这并不重要,但这是服务器方法:

public void Export(string destination)
{
    LogOnToTables();
    try
    {
        _report.ExportToDisk(ExportFormatType.PortableDocFormat, destination);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Failed to export report: " + ex.Message);
        throw;
    }
}

【问题讨论】:

  • 您是否在 C++ 中使用 #import 指令?如果是这样,它将是_com_error&amp;。 (您的代码中有_com_error*。)
  • 非常感谢!我将您的评论称为答案。

标签: com comvisible


【解决方案1】:

第一条评论包含答案。我需要捕获 _com_error 引用,而不是指针。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-10
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多