2

Is there anyway to throw an exception message(could be any string literal) from unmanaged code (c++ dll) to managed code(C#) ?

I have gone throw several posts about unmanaged exceptions but no where I have found anything related to string message being thrown. Is it at all possible ?

The exception is raised using the usual throw statement in C++. If I simply throw the exception, I get error message while running application in C# "External component has thrown an exception".

Let me know if you need any more info.

Thanks,

Rustagi
  • 41
  • 8
  • Possible Duplicate of http://stackoverflow.com/questions/150544/can-you-catch-a-native-exception-in-c-sharp-code, though I will put in my two cents. If you want nuanced exception handling, then you have to wrap the exceptions in the mixed side, so you should write a C++/CLI wrapper. – IdeaHat Aug 02 '13 at 13:38
  • @MadScienceDreams I thought about it but I don't like it. May be I will have to do that way. Else, can I somehow use the Callback methodology to raise such kinds of exception message in C#? The issue with callback would be to store the function pointer of C# method. I cannot pass that every time I call unmanaged code, that would be overkill. What do you think ? – Rustagi Aug 02 '13 at 14:02
  • @NREZ why do you think I have not tried solving it? – Rustagi Aug 02 '13 at 14:08
  • Because I found similar solved links over SO... I apologize if I misjudged you... – NREZ Aug 02 '13 at 14:11
  • @Rustagi I actually used that method to handle specific errors in a multithreaded application, but without a global, static "error handler" you'd have to pass it to every function and you would STILL need a C++/CLI wrapper (to catch the native exception and call the managed code)...it is probably just as much work as the try-catch in the native. IMHO if you are going to be using exceptions you should be putting that much effort in anyway. – IdeaHat Aug 02 '13 at 14:15
  • http://blogs.msdn.com/b/thottams/archive/2007/06/02/pinvoke-reverse-pinvoke-and-stdcall-cdecl.aspx I have been able to get the native exceptions (in string format) using the reverse P/Invoke. this blog helped me a lot. we can close this query now. Thanks. – Rustagi Aug 08 '13 at 13:45

0 Answers0