Catch C++ exception in C#
-
I have a legacy DLL written in unmanaged C++ that I need to use in a C# application. I can use it ok, but testing shows we don't get the string information when we catch any exceptions it throws. It throws a simple TCHAR string exception. example:
throw(_T("My Exception Message."))
We can catch it in C# with the System.Exception class, but the string message is lost. (The Message member is empty.) We tried using a RuntimeWrappedException but that did not catch it. I can not change the code in the DLL. How can I catch these exceptions and get the string info they contain? -
I have a legacy DLL written in unmanaged C++ that I need to use in a C# application. I can use it ok, but testing shows we don't get the string information when we catch any exceptions it throws. It throws a simple TCHAR string exception. example:
throw(_T("My Exception Message."))
We can catch it in C# with the System.Exception class, but the string message is lost. (The Message member is empty.) We tried using a RuntimeWrappedException but that did not catch it. I can not change the code in the DLL. How can I catch these exceptions and get the string info they contain? -
Not Knuth wrote:
I can not change the code in the DLL.
But you can create a C++/CLI assembly that can 1) catch the native exception, 2) Create a managed exception and marshal the message into it 3) throw the managed exception.
True. And thanks for your suggestion. But I hope there is a more direct way to accomplish the same thing. Surely the designers of the framework made some provision to catch unmanged exceptions AND get the data they may contain. (Unlike the RuntimeWrappedException.):sigh:
-
True. And thanks for your suggestion. But I hope there is a more direct way to accomplish the same thing. Surely the designers of the framework made some provision to catch unmanged exceptions AND get the data they may contain. (Unlike the RuntimeWrappedException.):sigh:
-
Not Knuth wrote:
But I hope there is a more direct way to accomplish the same thing.
I prefer to count on C++ rather than hope, but proceed as you like.
-
I prefer to use the best method available. I am not yet convinced that yours is the best method. It will work, but it is very inefficient.
-
Not Knuth wrote:
but it is very inefficient.
Compared to what? It is currently your only known solution so it is the most efficient solution. :rolleyes:
-
I have a legacy DLL written in unmanaged C++ that I need to use in a C# application. I can use it ok, but testing shows we don't get the string information when we catch any exceptions it throws. It throws a simple TCHAR string exception. example:
throw(_T("My Exception Message."))
We can catch it in C# with the System.Exception class, but the string message is lost. (The Message member is empty.) We tried using a RuntimeWrappedException but that did not catch it. I can not change the code in the DLL. How can I catch these exceptions and get the string info they contain?What does the innerexception property show?
I have no blog...
-
What does the innerexception property show?
I have no blog...
-
I have a legacy DLL written in unmanaged C++ that I need to use in a C# application. I can use it ok, but testing shows we don't get the string information when we catch any exceptions it throws. It throws a simple TCHAR string exception. example:
throw(_T("My Exception Message."))
We can catch it in C# with the System.Exception class, but the string message is lost. (The Message member is empty.) We tried using a RuntimeWrappedException but that did not catch it. I can not change the code in the DLL. How can I catch these exceptions and get the string info they contain?Not Knuth wrote:
I can not change the code in the DLL. How can I catch these (C++) exceptions and get the string info they contain?
I have recently encountered a similar situation. After a good bit of research and testing I can tell you that the best approach is to wrap the DLL in a managed assembly where you catch the C++ exception and then throw a managed exception in its place. (As described previously in the reply from led mike.) You can grab the string out of the C++ exception and insert it in one of the exception classes defined in the framework or in your own derived from one of them. The framework doesn't provide any method to automatically marshal the string message in a C++ exception to a managed object. Marshall
If you continue to do the same things you always did,
don't be surprised if you get the same results you always got. -
Not Knuth wrote:
I can not change the code in the DLL. How can I catch these (C++) exceptions and get the string info they contain?
I have recently encountered a similar situation. After a good bit of research and testing I can tell you that the best approach is to wrap the DLL in a managed assembly where you catch the C++ exception and then throw a managed exception in its place. (As described previously in the reply from led mike.) You can grab the string out of the C++ exception and insert it in one of the exception classes defined in the framework or in your own derived from one of them. The framework doesn't provide any method to automatically marshal the string message in a C++ exception to a managed object. Marshall
If you continue to do the same things you always did,
don't be surprised if you get the same results you always got.