Custom native/managed exception class

B

Bob Altman

Hi all,

We have a native class modeled after the System::Exception class, and all
exceptions that we throw derive from this class. For now this class is
quite simple: just Description and InnerException public members. One of
these days I'll dig into how to implement a StackTrace property (I assume
that this is possible using something like dbghelp.dll, but I've never had
the time to look into it).

I've recently written a managed wrapper class, written in C++/CLI (VS 2005).
This class exposes a native C++ interface to its callers, and internally
calls into managed routines written in VB. Now, this class can't allow any
CLR exceptions to attempt to flow back to the native callers, so I wrap all
of the wrapper routines in try/catch blocks. If the managed code catches an
exception then what I'd really like to do is to throw a native exception
whose InnerException object contains information about the managed exception
(in particular, the stack trace and names of loaded assemblies).

Is there a way that I can modify my native Exception class so that it
exposes a constructor to managed callers (C++ code compiled with /clr) that
takes a System::Exception as an argument? Under the covers, that
constructor would just fetch the StackTrace from the System::Exception, and
use a little reflection to get the list of loaded assemblies, and store both
results in private std::string variables. When a native caller eventually
catches my new, improved Exception object and calls StackTrace then my
Exception object would (hopefully) use dbghelp to get the stack trace back
to the point where the native Exception object was constructed and thrown,
and append to that the text stored in the magic string variable that
contains the managed stack trace.

I guess this question really comes down to two specific questions:

1. How can I write a class that can be consumed by native callers, but which
has one or more member functions that accept or return managed types and
which are only intended to be called by managed callers? (Note that I
didn't ask if it was possible. I've finally had it beaten into my skull by
this NG that darned near anything is possible in C++; it's just a question
of how tricky you have to be to get it done ;-)

2. I can't believe that I'm the first developer trying to write a native
equivalent to the managed Exception class (although I may well be the first
to try to get it to work in quite this way across the managed-to-native
boundary). Is there a native Exception object already available?

TIA - Bob
 
B

Ben Voigt [C++ MVP]

Bob said:
Hi all,

We have a native class modeled after the System::Exception class, and
all exceptions that we throw derive from this class. For now this
class is quite simple: just Description and InnerException public
members. One of these days I'll dig into how to implement a
StackTrace property (I assume that this is possible using something
like dbghelp.dll, but I've never had the time to look into it).

I've recently written a managed wrapper class, written in C++/CLI (VS
2005). This class exposes a native C++ interface to its callers, and
internally calls into managed routines written in VB. Now, this
class can't allow any CLR exceptions to attempt to flow back to the
native callers, so I wrap all of the wrapper routines in try/catch
blocks. If the managed code catches an exception then what I'd
really like to do is to throw a native exception whose InnerException
object contains information about the managed exception (in
particular, the stack trace and names of loaded assemblies).
Is there a way that I can modify my native Exception class so that it
exposes a constructor to managed callers (C++ code compiled with
/clr) that takes a System::Exception as an argument? Under the

Instead of a constructor or other member function, how about a friend
factory function? Friend functions don't affect the layout of the class, so
you can put the friend declaration inside #if __cplusplus_cli and the
managed code of the friend function can write your private member variables,
etc.
 
B

Bob Altman

Instead of a constructor or other member function, how about a friend factory
function? Friend functions don't affect the layout of the class, so you can
put the friend declaration inside #if __cplusplus_cli and the managed code of
the friend function can write your private member variables, etc.

Thanks Ben, that's just what I was looking for.

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top