HighTechTalks DotNet Forums  

Catching managed exceptions in unmanaged code

Dotnet Framework (CLR) microsoft.public.dotnet.framework.clr


Discuss Catching managed exceptions in unmanaged code in the Dotnet Framework (CLR) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Erlend
 
Posts: n/a

Default Catching managed exceptions in unmanaged code - 08-11-2006 , 10:02 AM






Hello,

I need to be able to catch clr exceptions in unmanaged code. Anyone
know how to do that?

I'm able to catch native types, so there is obviously some marshalling
going on, but I don't know what type the Exception^ is cast to. Any
help will be greatly appreciated!

The following example illustrates my problem:

//Compile with /clr
#include "stdafx.h"
#include <iostream>
#include <stdexcept>

using namespace System;

class Interop
{
public:
static void Managed_stde() {throw std::runtime_error("std
exception");}
static void Managed_clre() {throw gcnew ApplicationException("clr
exception");}

static void Unmanaged();
};

int main(array<System::String ^> ^args)
{
Interop::Unmanaged();

return 0;
}

#pragma unmanaged

void Interop::Unmanaged()
{
try
{
Managed_stde();
}
catch(std::exception& e)
{
std::cout << e.what() << std::endl;
}

try
{
Managed_clre();
}
catch(...) // Can only catch all to trap the clr exception!
{
std::cout << "unknown exception" << std::endl;
}
}


Reply With Quote
  #2  
Old   
Vadym Stetsyak
 
Posts: n/a

Default Re: Catching managed exceptions in unmanaged code - 08-11-2006 , 10:54 AM






Hello, Erlend!

Is it important for you to compile void Interop::Unmanaged() as unmanaged?
If not you can remove #pragma unmanaged directive and add another catch.

...
try
{
Managed_clre();
}
catch(Exception ^ex) // Can only catch all to trap the clr exception!
{
Console::WriteLine(ex->Message);
}
...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Reply With Quote
  #3  
Old   
Barry Kelly
 
Posts: n/a

Default Re: Catching managed exceptions in unmanaged code - 08-11-2006 , 11:15 AM



"Erlend" <erotvik (AT) broadpark (DOT) no> wrote:

Quote:
I need to be able to catch clr exceptions in unmanaged code. Anyone
know how to do that?

I'm able to catch native types, so there is obviously some marshalling
going on, but I don't know what type the Exception^ is cast to. Any
help will be greatly appreciated!
I think it would probably be easiest to write a wrapper managed function
that does the catch, and marshal the managed exception manually to your
unmanaged code.

-- Barry

--
http://barrkel.blogspot.com/


Reply With Quote
  #4  
Old   
Erlend
 
Posts: n/a

Default Re: Catching managed exceptions in unmanaged code - 08-14-2006 , 03:52 AM



Yes, unfortunately it needs to be unmanaged.

We have a large legacy application written in MFC/c++. We provide an
API for our clients so they can make plug-in modules. This API is also
available in .NET. for writing managed plug-in. So, we need to handle
managed exceptions in our API code, and obviously in our clients
plug-in code.

I think its a bit strange the IJW does not handle this case. I think
the natural way would be to marshal the Exception class into the
<stdexcept> model.

Erlend

Vadym Stetsyak wrote:
Quote:
Hello, Erlend!

Is it important for you to compile void Interop::Unmanaged() as unmanaged?
If not you can remove #pragma unmanaged directive and add another catch.

...
try
{
Managed_clre();
}
catch(Exception ^ex) // Can only catch all to trap the clr exception!
{
Console::WriteLine(ex->Message);
}
...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com


Reply With Quote
  #5  
Old   
Erlend
 
Posts: n/a

Default Re: Catching managed exceptions in unmanaged code - 08-14-2006 , 03:54 AM



Yes, at the moment it looks like that is the solution. But we have some
200 functions in our API so if we could avoid doing the marshalling
manually, that would be the best.

Erlend

Barry Kelly wrote:
Quote:
"Erlend" <erotvik (AT) broadpark (DOT) no> wrote:

I need to be able to catch clr exceptions in unmanaged code. Anyone
know how to do that?

I'm able to catch native types, so there is obviously some marshalling
going on, but I don't know what type the Exception^ is cast to. Any
help will be greatly appreciated!

I think it would probably be easiest to write a wrapper managed function
that does the catch, and marshal the managed exception manually to your
unmanaged code.

-- Barry

--
http://barrkel.blogspot.com/


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.