Srilaxmi
Srilaxmi

Reputation: 109

How to handle managed exception in unmanaged code

I have C++ project which is COM based, in which am accessing the C# assembly. I want to write the code to handle the exception in com project which are thrown by C# assembly.

I tried by placing the try and catch blocks, but exceptions are not been thrown instead the HRESULT value is less than zero. I need the exact message string to display which is thrown by C# API.

Please provide the guidelines for this.

Upvotes: 0

Views: 1458

Answers (1)

Hans Passant
Hans Passant

Reputation: 941724

You have two sources of info available. First off, the HRESULT that is returned is not arbitrary, every managed exception has a distinctive HRESULT that helps you identify the kind of Exception object that was thrown.

Next, the CLR implements the IErrorInfo interface. You can QI on the interface pointer you used to call the managed method to get the IErrorInfo interface pointer. IErrorInfo::GetDescription() returns the Exception.Message property value.

That's where it ends, no way to get the holy stack trace.

Upvotes: 1

Related Questions