HowTo throw custom C++ Exception and catch it in C# ?

Posted on 2008-11-10
Last Modified: 2012-05-05
I need to throw a custom exception in C++ DLL and catch it for further processing in C# code using this DLL. I need to pass a simple STRUCT having few unsigned int member variables. How to manage that?
Of course, I have access to both source codes, C++ and C#, as well...
Question by:darkriser
    LVL 23

    Expert Comment

    by:Jens Fiederer
    Just catching an exception from UNMANAGED code, you are going to lose information.

    they show (at the bottom) how to generate a wrapper that translates an exception (in their case, a string exception, but the unsigned would be similar).
    LVL 1

    Author Comment

    but this hint requires compilation using /clr option.
    this is something I need to avoid.
    LVL 23

    Accepted Solution

    Remember this is for linking the wrapper assembly (with your original dll), not the compilation of your DLL itself.

    Another option is to save the information inside your DLL, and provide a function or functions to retrieve it.
    That way, when you call originally, you get a nondescript exception (exception thrown inside external component), but your exception handling can go back to the DLL to find out more using normal function mechanisms.
    LVL 1

    Author Closing Comment

    This is not the answer for my question but this is exactly how to solve my problem:-)
    Thanks for those ideas...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now