CLR Hosting - exception handling?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.clr.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Vincent Parrett \(Atozed Software\)
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

Reply to this message...
 
    
Vincent Parrett \(Atozed Software\)
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

Reply to this message...
 
    
Nadav (VIP)
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;
}
}

"Vincent Parrett (Atozed Software)" wrote:

[Original message clipped]

Reply to this message...
 
 
System.AppDomain
System.Diagnostics.StackTrace




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com