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

Posted on 2008-11-10
Medium Priority
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
  • 2
  • 2
LVL 23

Expert Comment

by:Jens Fiederer
ID: 22922532
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).

Author Comment

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

Accepted Solution

Jens Fiederer earned 800 total points
ID: 22922668
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.

Author Closing Comment

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

839 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