CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2005
    Posts
    126

    C# COM client error handling

    hey guys, im looking for some information on how to handle exceptions within a C# client from a C++ COM server. I'm use to error handling in C++ and am not quite sure what to do with being able to say SUCCEEDED or have the HRESULT available to me. how do i manage this in C#? anyone know?

    p.s sorry if this should be in the C# section, but it is a bit of both worlds.

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: C# COM client error handling

    [ redirected ]

    Regards,
    Siddhartha

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: C# COM client error handling

    If an HRESULT returns an error code then a COMException is thrown. The property ErrorCode on this exception gives the error code.

    e.g.

    Code:
    uint hResult = 0;
    
    try
    {
        // make COM call here
    }
    catch (COMException exception)
    {
        hResult = exception.ErrorCode;
    }
    
    return hResult;
    I'm unsure if it throws this exception if S_FALSE is returned : you'll have to try it out.

    Alternatively, you can do your own COM interop as shown in my article.

    http://www.codeguru.com/Csharp/Cshar...cle.php/c9065/

    Define [PreserveSig] on each method and an exception will not be thrown : the HRESULT will be passed back.

    e.g.

    Code:
    [Guid("----")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ITestInterface
    {
        [PreserveSig] uint MyMethod();
    }
    Darwen.
    Last edited by darwen; September 29th, 2005 at 06:00 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: C# COM client error handling

    S_FALSE, S_OK, and S_* in general are success return codes.
    E_FAIL, E_POINTER, and E_* in general are error return codes.

    An Error returned by a Native C++ COM Server will throw an exception in the C# client.
    Success (even if S_FALSE) will not.

    Attached is a sample ATL C++ COM Server accessed by a C# client.

    Should you return E_FAIL in CDialogInterface::Show (exposed via IDialogInterface), you will get an unhandled exception - one that needs to be handled via the technique in the previous post.

    In general, the sample displays a Dialog implemented in a C++ COM Server invoked via a C-Sharp client.

    Detailed explanation of the sample is in my post here.
    (Ah, it seems that you have already seen it!)

    On a side note - in C++, the macro SUCCEEDED returns TRUE for S_FALSE as well - reason being that S_FALSE is also a return code implying SUCCESS.
    Attached Files Attached Files
    Last edited by Siddhartha; September 29th, 2005 at 06:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured