none
Throw Custom Exception from C++ COM+ to C# client RRS feed

  • Question

  • Hi All,

    I have below scenario:

    1)A COM+ server application written in C++.

    2)A C# client which calls one of API of above COM+.

    I want to throw a custom exception from C++ COM+ and catch it in C# client. How can I do it? 

    C++ COM 

    HRESULT CPLUSCOMPLUS::DoSomething(/* [retval][out] */ LONG *retValue )
    
    {
            retValue = 100;
    	if(Error())
               return MyErrorCode;
    }

    One way I think is by handling through retValue (having negative values for different errors ).

    Other way is to , may be throw std::exception and catch SEHException ( std::exception does not have error code). I dont want to do message comparison.

    If I return non standard value as HRESULT, then interop does not throw COMException to C# client. How can I write a custom Exception class in C++ COM+ and catch in C#?

    Thanks & Regards

    VV

    Wednesday, June 20, 2012 5:26 AM

Answers

  • It is news to me that a custom HRESULT with the fail bit on is not recognized by .net.  Are you sure about that?

    Also, have you tried using the standard COM mechanism to provide error information (ISupportErrorInfo)?


    Jose R. MCP
    Code Samples

    • Marked as answer by Vic Vega Wednesday, June 20, 2012 6:55 AM
    Wednesday, June 20, 2012 6:10 AM

All replies

  • Return a standard HRESULT and add an Error property on the COM server.


    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    Wednesday, June 20, 2012 5:37 AM
  • Thanks Sheng Jiang.

    This means to say I need to change COM+ interface (which I can't).

    In DOSomething API, I need to store error code in object and access it later through "Error" property. Is maintaining a value in COM+ object and retrieving later proper? 

    Any other way?

    Thanks & Regards

    VV

    Wednesday, June 20, 2012 6:02 AM
  • It is news to me that a custom HRESULT with the fail bit on is not recognized by .net.  Are you sure about that?

    Also, have you tried using the standard COM mechanism to provide error information (ISupportErrorInfo)?


    Jose R. MCP
    Code Samples

    • Marked as answer by Vic Vega Wednesday, June 20, 2012 6:55 AM
    Wednesday, June 20, 2012 6:10 AM
  • Hi Jose,

    It works :-). I think I missed unchecked checks.

    I could get custom error working.

    Thanks Everyone,

    VV

    Wednesday, June 20, 2012 6:55 AM