1

I am working on WPF application which internally calls a C/C++ DLL using PInvoke. In the debug mode of the DLL, whenever error occurs the function throw an exception which is basically a structure defined containing specific error message and application code specific to module. This is different from the normal Win32 error logging. Now my problem is I want to catch the exception thrown by the DLL.

If in use a try catch around the Marshalled function, .NET just informed me saying something wrong happened in the external module.

try
{
    // Marshalled function called
}
catch(Exception ex)
{
    MessageBox.show(ex.Message);
}

Now I understand the C/C+= DLL exception is not a class derived from Exception class of .NET hence .NET would not be able to marshal it properly. I cannot change the C++ DLL to managed one or make any source code changes to the dll code.

I did see a similar post on MSDN with the solution provided in VC++.NET where each function of DLL is created into a wrapper throwing an exception derived from .NET class.

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/0624f3b3-5244-4cb8-be9c-29d464975d20

However, this would require another .net assembly project which would create wrappers around some 200 functions and structures in VC++.NET. Right now, all the DLL functions have been imported into WPF application using PInvoke so any add-on to existing scenario would be preferred.

K Singh
  • 1,710
  • 1
  • 17
  • 32
  • 3
    Using pinvoke was the wrong way to get started. You *have* to create C++/CLI wrappers to catch a C++ exception. – Hans Passant Jul 19 '11 at 20:14
  • Is there anything now which can be done other than rewriting the whole DLL APis again and starting from ground zero? The post I referred on MSDN forums had you solution but in VC.NET, is there a way to create such a wrapper in C#.NET? – K Singh Jul 19 '11 at 20:22
  • possible duplicate: http://stackoverflow.com/questions/150544/can-you-catch-a-native-exception-in-c-code – yms Jul 20 '11 at 20:30
  • @yms: like i mentioned in the question the error code returned are not Win32 standard errors which can be mapped so Win32Exception would not be helpful. But I did try it and it didn't give me desired results. Hans Passant has given a solution on MSDN forum to similar issue by creating a managed c++ wrapper, however currently I dont have that option to exercise due to constraints. – K Singh Jul 20 '11 at 21:21
  • K Singh, I was actually thinking on this answer in particular: http://stackoverflow.com/questions/150544/can-you-catch-a-native-exception-in-c-code/150675#150675 – yms Jul 20 '11 at 23:13
  • However some exceptions like StackOverflow and OutOfMemory cannot be catched, even if you do a C++/CLI wrapper. – yms Jul 20 '11 at 23:14
  • @yms: the answer you pointed there, I tested earlier and like I stated in the question that the C++ exception doesnt derive from Exception class of .NET hence if i use try/catch in my .NET app then the Message property of exception in catch just says error occurred in external module however c++ exception contains actual message description of why exception occurred in DLL. And the exceptions raised by DLL are not of StackOverflow etc which will actually bring the whole application down. – K Singh Jul 21 '11 at 01:57
  • Now I am confused...the answer proposes removing '(Exception ex)' from 'catch(Exception ex)', leaving only try{ ... } catch { ... }. In such scenario you will not be able to see a Message property, since there is no message variable to beging with. In this approach you will be able to capture all exceptions but you will not be able to "classify" them, so it will only be a workaround to the problem. I stronly believe that your only good choice is the C++ wrapper. – yms Jul 21 '11 at 02:09
  • like i mentioned the point is to get the exception message sent by the DLL, which would not work if I dont have exception variable. With/without the exception variable try/catch work and informs me something went wrong in DLL, what went wrong is hidden. Since the application is already ported using PInvoke(rework of full DLL APIs would be huge rework and effort), i was looking for work around where in whenever exception happens I get a callback or something in my .NET app with the required information. – K Singh Jul 21 '11 at 02:23

1 Answers1

1

If you want to continue using P/invoke as you indicate in comments, then your only option is to catch the exceptions at the DLL boundary. You can then pass the details on to the managed application using error codes rather than exceptions. If you want to use P/invoke then you can't allow exceptions to cross the DLL boundary.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490