Click here to Skip to main content
15,880,651 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have some code that is intentionally not compatible in debug and release modes. When I run an exe in release mode when the CLR dll with C++ interface (no .NET classes in the headers) is in debug mode, I want to catch the exception and handle it.

If I compile the exe in debug mode and the CLR dll in release mode, it works fine. The other way around, I get an immediate error "the application was unable to start correctly 0xc0000005.

This is only a problem when I want to explicitly handle the incompatible exe and dll build modes, never any other time. Any ideas how to stop this?

Even if I build with clr, then a pop-up error occurs halting the program instead of letting it handle the errors. It's rather infuriating.

My C++/CLI code catches the exception and prints out debugging info, but then Microsoft Visual Studio creates a popup: Unhandle exception at ....

Break or Continue. I want to suppress the Break or Continue pop-up and use my try catch statement.
Posted
Comments
Sergey Alexandrovich Kryukov 20-Oct-11 16:26pm    
Hm, you see, "intentionally not compatible in debug and release modes"... in this case, do you really use "Debug" configuration for debugging? If so, how can you be sure what are you debugging? :-)
--SA
T2102 20-Oct-11 16:31pm    
In debug mode, I compile all of my code into debug.
Sergey Alexandrovich Kryukov 20-Oct-11 16:49pm    
If you use UI, also tag UI library.
--SA

1 solution

First, please see my comment about your questionable practice of using "Debug" and "Release" configurations. (The proper term for them in "Configuration", not "mode".)

This is not how such things are done. All configurations are in fact custom. "Debug" and "Release" are only suggested names, for example, included by Visual Studio in all project templates. You can have your own. So, if you really need two different configurations which differ not by debug options but something else, you need create 4 (four) configurations.

For example, you need to essentially different (as you say, "intentionally not compatible") configurations, named let's say, "Basic" and "Advanced", you should create configurations like "Basic Debug", "Basic Release", "Advanced Debug" and "Advanced Release". The pairs of "* Debug" and "* Release" configurations still must be differ only in debug options (and XML Documentation maybe), nothing else.

Now, there can be various causes for your particular problem; it's very hard to say what exactly without having your code. However, you cannot talk about C++/CLI at all if you don't use "/clr" option.

Your observations on exception handling under Visual Studio depend not only on you code, but also on the options you can find in [Visual Studio Main Menu] => Debug => Exceptions. Please review them. To see how your code really handle exception, try to run it stand-along. Basically, to handle all exceptions in order to prevent termination of the process, you need to catch all exceptions on the very top of the stack of each thread. In case of UI, you also need to catch all exceptions inside the main event loop of the application. Both System.Windows.Forms.Application and WPF System.Windows.Application provide respective modes for that and exception handling; you only need to handle thread exception handling events: System.Windows.Forms.Application.ThreadException and System.Windows.Application.DispatcherUnhandledException. See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application%28v=VS.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.onthreadexception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.application.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx[^].

—SA
 
Share this answer
 
Comments
T2102 20-Oct-11 17:54pm    
Yes, I agree with you about Configuration instead of mode. I am only using the console in my example, no WPF or MFC form complications. I don't see Exceptions under the debug menu. I am using Visual C++ 2010. My particular application is simply; run a bat file and send an email indicating whether or not it succeeds. The complication is that if the dlls and exe happen to be in different configurations, Visual Studio refuses to start or gives a pop-up error instead of allowing the try catch blocks to work in my dlls or the exe.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900