Groups | Blog | Home
all groups > dotnet clr > august 2004 >

dotnet clr : CLR Hosting - exception handling?


Vincent Parrett (Atozed Software)
8/30/2004 8:41:18 AM
Hi

I'm hosting the CLR in my win32 unmanaged application, it's all working
really well however I have one issue left to resolve. When a managed
exception is raised, it is propagated to the win32 host as an ole exception.
Is there a way to get access to the managed exception object (I want to get
access to the stack trace)? It seems the com interop is changing the way
unhandled exceptions are dealt with, setting
AppDomain.CurrentDomain.UnhandledException (in managed code) has no effect,
my handler method is never called. Is there a way to override how com
interop deals with managed exceptions?

--
Regards

Vincent Parrett
Atozed Software http://www.atozedsoftware.com
My blog : http://blogs.atozed.com/vincent
----------------------------------------
Automate your Build Process with FinalBuilder

Vincent Parrett (Atozed Software)
8/30/2004 10:39:33 PM
Ok, I'm getting closer to a solution. I found that I can call GetErrorInfo
in my win32 host and the QueryInterface on the IErrorInfo reference for the
CLR Exception interface in mscorlib. This works because when hosting the clr
interop is involved and the CCW automatically does the SetErrorInfo when a
managed exception is thrown. This gives me access to the real exception
information and the stack trace, well almost...

The issue I have now is that if the exception is thrown in an AppDomain that
I created from my win32 host, the StackTrace property of the exception
object always returns an empty string. If the exception is thrown from the
default AppDomain then I get the StackTrace ok. I suspect this is an
interface marshalling issue, but I don't have a clue how to resolve this. I
really don't want to have to load my assemblies into the default AppDomain
as I would like to be able to unload/reload the assemblies without having to
restart the app.

Any clues?

--
Regards

Vincent Parrett
Atozed Software http://www.atozedsoftware.com
My blog : http://blogs.atozed.com/vincent
----------------------------------------
Automate your Build Process with FinalBuilder

Nadav
9/11/2004 12:51:03 PM
Here is the code

#import <mscorlib.tlb>
....
....
....
try
{
...
}
catch(_com_error &e)
{
hr = e.Error();
mscorlib::_ExceptionPtr Exception = e.ErrorInfo();
if(Exception)
{
char *bstrMessage = new char[Exception->ToString.length() + MAX_PATH];
sprintf(bstrMessage, "%s\nPress any key to terminate...\n",
(char*)Exception->ToString);
if(bConsole)
{
printf(bstrMessage);
getch();
}
else
MessageBoxA(0, bstrMessage, "The program has generated an unhandled
exception", MB_OK);
delete bstrMessage;
bstrMessage = 0;
}
}

[quoted text, click to view]
AddThis Social Bookmark Button