Suppose the following:
// Unmanaged code
class UnmanagedException /* not visible outside of unmanaged code */
{
};
void DoSomething() /* visible (exported) to managed code */
{
throw new UnmangedException();
}
// Managed code
// Call into unmanaged code
try
{
DoSomething();
}
catch (???)
{
}
What is/are suitable types to catch in the managed code?
I see SEHException and its parent ExternalException, but these (automatic)
wrappers seem to be based on COM hresult types of exceptions.
My understanding is that the managed framework automatically catches,
repackages, and rethrows the unmanaged exception, but as what?
Thanks
--
Bret Pehrson
mailto:br**@infowest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>> 12 13744
Hello Bret,
Thanks for posting in the group.
Based on the description, the question is: How to trap a unmanged type
exception in managed code? Please feel free to post here if I misunderstood
the question.
In fact, we can catch the common language runtime exception
(System::Exception) and the unmanaged exception (C++ struct) in Managed
Extensions for C++ code. Please refer to the following sample code:
#using <mscorlib.dll>
using namespace System;
struct S {
};
void foo(int i) {
if (i == 0) {
throw (new S());
}
else {
Exception *e = new Exception;
throw e;
}
}
int main() {
int nRes = 1;
for (int i = 0; i < 2; i++) {
try {
foo(i);
}
catch (S* s) {
(s);
Console::WriteLine(S"Caught an unmanaged exception!");
nRes -= 2*i;
}
catch (Exception* e) {
Console::WriteLine(S"Caught a managed exception: {0}", e->ToString());
nRes -= i;
}
}
return nRes;
}
Note that the same try block is guarded by both of the catch statements
that catch managed and unmanaged exceptions individually. Also note that
the code generated by this sample is completely managed code.
You can also refer to http://msdn.microsoft.com/library/en...edsampledemons
tratesthrowingcatchingmanagedunmanagedexceptionswi thinsingleprocess.asp?fram
e=true for the details on this sample.
Does that answer your question?
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Hi Bret,
You can catch unmanaged exception in c# but you can't use them. you can only
rethrow them
if you have try/catch block like this
try
{
....
}
catch
{
...
throw;
}
the catch block will catch all exceptions (managed and unmanaged) and the
*thow* operator will rethrow catched exception *AS IS*.
This is the only way you can catch unmanaged exception with try/catch block
in c# and your choices are either to swallow it or rethrow it.
--
B\rgds
100
"Bret Pehrson" <br**@infowest.com> wrote in message
news:40***************@infowest.com... Suppose the following:
// Unmanaged code class UnmanagedException /* not visible outside of unmanaged code */ { }; void DoSomething() /* visible (exported) to managed code */ { throw new UnmangedException(); }
// Managed code // Call into unmanaged code try { DoSomething(); } catch (???) { }
What is/are suitable types to catch in the managed code?
I see SEHException and its parent ExternalException, but these (automatic) wrappers seem to be based on COM hresult types of exceptions.
My understanding is that the managed framework automatically catches, repackages, and rethrows the unmanaged exception, but as what?
Thanks
-- Bret Pehrson mailto:br**@infowest.com NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>>
I know this is really a nit-picky question, but I'm still getting
to grips with MC++'s rules and regs:
//> throw (new S());
throw S(); // Do we need to allocate from the freestore?
//> catch (S* s) {
catch (S& s) // Isn't it preferable to catch by reference?
//> (s); // What does this do?
You mention that all the resulting code is managed. Is the 'new S()'
required to make this the case?
Thanks.
Arnold the Aardvark http://www.codeproject.com/cpp/garbage_collect.asp
Hello,
For your quesitons,
1) and 2):
throw S() --> catch (S& s)
throw ( new S()) --> catch ( S* s)
3) If you add a constructor to struct S, you can see that (s) will call its
constructor.
For an example,
struct S {
S(){Console::WriteLine ("enter structure S");}
};
4) new S() is not related to managed or unmanged.
Thanks.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
> 3) If you add a constructor to struct S, you can see that (s) will call its constructor.
But the object is already fully constructed in the throw statement.
Am I being obtuse? I've never C++ like this before.
4) new S() is not related to managed or unmanged.
Thanks. That's what I wanted to know.
Arnold the Aardvark http://www.codeproject.com/cpp/garbage_collect.asp
Yan-Hong Huang[MSFT] <yh*****@online.microsoft.com> wrote: [...] 4) new S() is not related to managed or unmanged.
I wouldn't know about MC++, but in
std C++ 'throw new S()' certainly
seems a very bad idea.
Thanks.
Best regards, Yanhong Huang
Schobi
-- Sp******@gmx.de is never read
I'm Schobi at suespammers dot org
"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Hi Schobi,
You are correct. This is just extracted from some sample code. throw S()
--> catch (S& s) is better.
While the Standard doesn't specify where the exception object in catch (xc
& x) is stored, most implementation maintain a special exception stack, on
which exception are constructed (however, the Standard does not allow the
implementation to use free store memory for that purpose). Passing an
exception by reference is preferred to passing an exception by value for
several reasons: as you suggested, it short circuits the process of passing
the exception object to its handler (only a reference is copied rather than
a full-blown object). However, pass by reference also ensures that derived
exceptions are not sliced. Finally, it enables you to modify the exception
and then pass it on to another handler (i.e., a re-throw) with the changes
made to that object.
Thanks for pointing it out. :)
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Hi Arnold,
Sorry for my mistake. (s) has no effects here. By using ildasm.exe, you can
see that this sentence is ignored by compiler and have no effect.
It just likes:
struct S a;
(a);
It can pass building.
BTW, if you want the struct is managed code, you need to define it as: __gc
struct...
Thanks very much.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Mhmm. While catching by reference certainly
is better than catching by value (beeing
who I am I mostly catch by const reference),
actually I was refereing to your
throw new S()
as this begs the question of who will be
responsible for deleting the object.
Schobi
Yan-Hong Huang[MSFT] <yh*****@online.microsoft.com> wrote: Hi Schobi,
You are correct. This is just extracted from some sample code. throw S() --> catch (S& s) is better.
While the Standard doesn't specify where the exception object in catch (xc & x) is stored, most implementation maintain a special exception stack, on which exception are constructed (however, the Standard does not allow the implementation to use free store memory for that purpose). Passing an exception by reference is preferred to passing an exception by value for several reasons: as you suggested, it short circuits the process of passing the exception object to its handler (only a reference is copied rather than a full-blown object). However, pass by reference also ensures that derived exceptions are not sliced. Finally, it enables you to modify the exception and then pass it on to another handler (i.e., a re-throw) with the changes made to that object.
Thanks for pointing it out. :)
Best regards, Yanhong Huang Microsoft Community Support
Get Secure! ¨C www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
-- Sp******@gmx.de is never read
I'm Schobi at suespammers dot org
"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Hi Hendrik,
Yes, that is why I said "throw S() --> catch (S& s) is better." in my
former post in the first paragraph. :) In this way, we don't need to delete
S object later.
Have a good day.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Yan-Hong Huang[MSFT] <yh*****@online.microsoft.com> wrote: Hi Hendrik,
Yes, that is why I said [...]
I see. Sorry for the confusion.
Yanhong Huang
Schobi
-- Sp******@gmx.de is never read
I'm Schobi at suespammers dot org
"Sometimes compilers are so much more reasonable than people."
Scott Meyers
:)
BTW, Bret, do you have any more concerns on this question?
Thanks and have a good day.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: James L. Brown |
last post by:
Hello
I'm currently working on a project, that contains one .net (VB) executable and a C++ (MFC) DLL. Now, what I want to do is to call the functions in the dll out of my managed code
//managed...
|
by: Bret Pehrson |
last post by:
Suppose the following:
// Unmanaged code
class UnmanagedException /* not visible outside of unmanaged code */
{
};
void DoSomething() /* visible (exported) to managed code */
{
throw new...
|
by: Brett Styles |
last post by:
Hi Guys,
I am trying to access a class in an unmanaged dll. I have
created a wrapper managed class to access the functions I need but no matter
what I try from the MSDN samples I can not get it to...
|
by: Vasco Lohrenscheit |
last post by:
Hi,
I have a Problem with unmanaged exception. In the debug build it works
fine to catch unmanaged c++ exceptions from other dlls with
//managed code:
try
{
//the form loads unmanaged dlls...
|
by: RaKKeR |
last post by:
Hi,
I'm trying to wrap my unmanaged c++ code into a managed c++ wrapper.
The problem is VC .NET doesn't seem to find the namespaces defined in
my unmanaged code. Let's say I have a file...
|
by: Andy |
last post by:
I'm having trouble accessing an unmanaged long from a managed class in
VC++.NET
When I do, the contents of the variable seem to be mangled. If I
access the same variable byte-by-byte, I get the...
|
by: =?Utf-8?B?Y2pz?= |
last post by:
Hi,
I have CLI/C++ code to override WndProc(Message% m) in order to create NC
area. I tried to marshal unmanaged ptr (m.LParam) to managed object by using
Marshal::PtrToStructure like:
if...
|
by: LeXave |
last post by:
Hi all
In C++/CLI, is there a way to free an array allocated from an
unmanaged dll ? In my managed code, I call a function from that dll
which returns me an array of bytes. But how can I free it...
|
by: sree83 |
last post by:
Hi All,
I am having an executable developed in C#.Net. My app make use of services from some unmanaged dlls it loads. My problem is that my application is termainted due to some exceptions...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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...
| |