• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1110
  • Last Modified:

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

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...
0
darkriser
Asked:
darkriser
  • 2
  • 2
1 Solution
 
Jens FiedererTest Developer/ValidatorCommented:
Just catching an exception from UNMANAGED code, you are going to lose information.

Here
    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/0624f3b3-5244-4cb8-be9c-29d464975d20/
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).
0
 
darkriserAuthor Commented:
thanks...
but this hint requires compilation using /clr option.
this is something I need to avoid.
unfortunately...
0
 
Jens FiedererTest Developer/ValidatorCommented:
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.
0
 
darkriserAuthor Commented:
This is not the answer for my question but this is exactly how to solve my problem:-)
Thanks for those ideas...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now