472,780 Members | 1,418 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

Custom native/managed exception class

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

Jun 30 '08 #1
2 2878
Bob Altman wrote:
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.
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

Jun 30 '08 #2
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
Jun 30 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Kantowitz | last post by:
I am trying to wrap a native-C++ DLL in managed C++, to use in a .NET project. The native code is compiled into a DLL, and I have created a .def file that exports the mangled names of the...
2
by: Martin Zenkel | last post by:
Dear VS Team, using the Beta 2 of VS 2005 I've encontered the following problem. Let's assume threre are three Dll's, one unmanaged and two managed. In the unmanaged we put a simple unmanged...
9
by: Herby | last post by:
Is possible to have a managed method within a Native(un-managed) class within a \clr project? E.g. class myClass { public: #pragma managed void myMethod(void);
5
by: Lonewolf | last post by:
Hi, I'm not sure if this has been asked before so please pardon me if this is a repeated question. Basically I have some performance critical directshow codes which is implemented in native,...
3
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following...
14
by: Ben Voigt | last post by:
Under certain circumstances I get: First-chance exception at 0x7c812a5b (kernel32.dll) in LTMGUI.exe: 0xC0020001: The string binding is invalid. First-chance exception at 0x7c812a5b (kernel32.dll)...
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
0
by: ChopStickr | last post by:
I have a custom control that is embedded (using the object tag) in an html document. The control takes a path to a local client ini file. Reads the file. Executes the program specified in...
5
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have a class that is writen in unmanaged pure native C++. This class files (h and cpp) are inserted to a managed C++ (VC++ 2005, C++/CLI) DLL compoenet. This DLL compoenet is used in a C#...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.