Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Catch C++ exception in C#

Catch C++ exception in C#

Scheduled Pinned Locked Moved C#
questioncsharpc++testingbeta-testing
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Not Knuth

    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?

    L Offline
    L Offline
    led mike
    wrote on last edited by
    #2

    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.

    N 1 Reply Last reply
    0
    • L led mike

      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.

      N Offline
      N Offline
      Not Knuth
      wrote on last edited by
      #3

      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:

      L 1 Reply Last reply
      0
      • N Not Knuth

        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:

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #4

        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.

        N 1 Reply Last reply
        0
        • L led mike

          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.

          N Offline
          N Offline
          Not Knuth
          wrote on last edited by
          #5

          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.

          L 1 Reply Last reply
          0
          • N Not Knuth

            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.

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #6

            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:

            N 1 Reply Last reply
            0
            • L led mike

              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:

              N Offline
              N Offline
              Not Knuth
              wrote on last edited by
              #7

              Following that logic, it is also the most inefficient solution! :rolleyes: It might turn out to be the best option. But perhaps there is another one that neither of us know. That is why I posted my question here.

              1 Reply Last reply
              0
              • N Not Knuth

                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?

                L Offline
                L Offline
                Liam OHagan
                wrote on last edited by
                #8

                What does the innerexception property show?

                I have no blog...

                N 1 Reply Last reply
                0
                • L Liam OHagan

                  What does the innerexception property show?

                  I have no blog...

                  N Offline
                  N Offline
                  Not Knuth
                  wrote on last edited by
                  #9

                  Liam O`Hagan wrote:

                  What does the innerexception property show?

                  The inner exception is null.

                  1 Reply Last reply
                  0
                  • N Not Knuth

                    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?

                    M Offline
                    M Offline
                    Marshall
                    wrote on last edited by
                    #10

                    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.

                    N 1 Reply Last reply
                    0
                    • M Marshall

                      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.

                      N Offline
                      N Offline
                      Not Knuth
                      wrote on last edited by
                      #11

                      Thank you.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups