throw .net-wrappable exception from native COM-Dll


In a native-C++-Dll (MFC): how to throw an Exception (with some additional text) that can be catched and handled from a .NET-Client?

This is the native-MFC-Dll which wants to throw an Exception:

void RGaugeTVDDataReader::LoadDataFromFile_HandleException(const CString& szPath) const
    CString sMessage;
    sMessage.Format(_T("TVD-File %s"), static_cast<LPCTSTR>(szPath));

    /*1*/ AfxThrowFileException(CFileException::genericException, -1, static_cast<LPCTSTR>(sMessage));
    /*2*/ throw std::runtime_error(CT2A(sMessage));

The Caller is a .Net-Dll which uses the native dll via a .net-generated-COM-Wrapper. Both variants (/1/ and /2/) fall into the catch-Block of the .Net-Component but what I get here is just a ┬┤System.Runtime.InteropServices.SEHException┬┤ and the exceptionmessage says "External component has thrown an exception.". Included ErrorCode and HResult is 0x80004005. There is no inner exception, nothing to find about my given text and the stacktrace only contains the managed part.

So the question is: how to throw an excpetion from the native-c++-dll so that a .net-client using it via COM can at least see the message-string?

Alternative question: how to catch and handle the thrown exception in .net correctly?


asked on Stack Overflow Dec 3, 2020 by suriel • edited Dec 3, 2020 by suriel

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0