none
Exceptions Between NATIVE and .NET CLIENT HANDLING RRS feed

  • Question

  • hi,

     i have a NATIVE component  in ATL  which has  event with an  out/ref  parameter. This native component raises events which  has a sink made in C#  using MANAGED C++ Wrapper and not interops. Whenever the C# has any exceptions in code written in the event delegate it ends up corrupting the Native side too resulting in lots of First chance exceptions which eventually crashes the system later. We did correct the the C# code and it solved the exceptions in the native side too. But since this C# code is client part in which any user can write their own code how can we make sure to make this safe. Would like to mention this event is synchronous. What  are the best practices in this case ?

    Thanks n Regards

    Bishwaroop

    Tuesday, January 29, 2013 7:09 AM

All replies

  • Hi Bishwar,

    Welcome to the MSDN Forum.

    I am trying to involve some other one into this case. Wait it patiently, please.

    Thank you.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 30, 2013 10:26 AM
    Moderator
  • hi,

     i have a NATIVE component  in ATL  which has  event with an  out/ref  parameter. This native component raises events which  has a sink made in C#  using MANAGED C++ Wrapper and not interops. Whenever the C# has any exceptions in code written in the event delegate it ends up corrupting the Native side too resulting in lots of First chance exceptions which eventually crashes the system later. We did correct the the C# code and it solved the exceptions in the native side too. But since this C# code is client part in which any user can write their own code how can we make sure to make this safe. Would like to mention this event is synchronous. What  are the best practices in this case ?

    Thanks n Regards

    Bishwaroop

    Can you post a code example? A bare bones solution that compiles would make it easier to understand your problem and provide a thoughtful answer.

    Thanks!

    Jason

    Thursday, January 31, 2013 6:44 PM
  • Thanks Mike

    Jason,

    please give me some time. Ill post a sample regarding this.

    Bishwaroop


    Friday, February 1, 2013 10:22 AM
  • hi,

    https://docs.google.com/file/d/0B2vIDSwNwS70RllnZGx3Rm9fTEk/edit?usp=sharing

    The above link has a sample ATL server and C# client. This is interop based. ATL server raises an Event . The sink in C# client has a line of code which throws an exception on purpose. This exception is caught at the ATL side. Its simple reproduction.


    Thanks

    Bishwaroop

    Monday, February 11, 2013 6:46 AM
  • Both the native and the managed part of your application relay on Windows Structured Exception Handling (SEH) to handle exceptions, if the callee raises an exception, the caller will be notified by SEH; in this case, the callee is managed code, and the caller is the native part.

     

    Usually, there are two methods to invoke managed code from native application:

    1. Register .NET dll as COM component, and we can invoke .NET code from C/C++ application directly.
    2. Use Common Language Runtime (CLR) hosting. For instance, C/C++ application start a CLR runtime, and execute .NET code within CLR runtime.

     

    Each exception has its own exception code, for .NET exceptions, the exception codes are:

    0xE0434352 --> .NET Framwork 4.0 Exceptions

    0xE0434F4D --> .NET Framework 1.0 - 3.5 Exceptions

     

    If you're using method #1, in your exception handler method of native part, you can distinguish whether the exception is thrown from managed part or not, and catch all managed exception.

     

    If you're using method #2, since the managed code can be written by your customers, the managed code acts as an extension, you may start different Application Domain (AppDomain) for managed code. AppDomain provide lightweight isolation concept within a Process, and hence the crash of the managed code will not break your native application.

     

    Reference:

    [1] AppDomain Class

    http://msdn.microsoft.com/en-us/library/system.appdomain.aspx

     

    [2] Structured Exception Handling (Windows)

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms680657(v=vs.85).aspx

     

    [3] CLR Hosting APIs

    http://msdn.microsoft.com/en-us/magazine/cc163567.aspx


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Regards,
    Eric Yang
    Microsoft Online Community Support

    Thursday, February 14, 2013 3:38 AM