473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 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 2941
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#...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.