1

I have a COM object written in C#. Under some circumstances, the COM object may throw an Exception with a descriptive message if it encounters an error (for example throw new Exception("error message")).

This COM object is called from VB6 code and from C++ code.

From the VB6 code, I can retrieve the error message using Err.Message.

In C++, I get an HRESULT 0x80131500 as specified in the System.Exception documentation.

Once I have this HRESULT in C++, how can I get the error message of the Exception (Exception.Message) like the message returned by Err.Message in VB6?

I searched the web and found a few examples using FormatMessage and _com_error, but none of these return the message I want.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
ghigad
  • 341
  • 1
  • 12
  • Have you tried `IErrorInfo`? Check in `winerror.h` what facility that `HRESULT` defines. I strongly suspect it's the user defined facility, which means asking the object. – Cheers and hth. - Alf Jan 20 '14 at 17:08
  • `_com_error` would appear to be the answer. Why you have no luck with that is hard to tell because you did not show your code. – David Heffernan Jan 20 '14 at 17:08
  • @Cheersandhth.-Alf `_com_error` relies on `IErrorInfo`. – David Heffernan Jan 20 '14 at 17:09
  • @DavidHeffernan: `_com_error` *can* use that interface, but defaults to not using it. one must pass the object's interface to the constructor. i am sure the OP did not do that, or it would be mentioned. – Cheers and hth. - Alf Jan 20 '14 at 17:12
  • @Cheersandhth.-Alf Are you sure. I would have expected `_com_error` to call `GetErrorInfo` if no `IErrorInfo` is supplied. Otherwise, how can `_com_error` do anything at all? – David Heffernan Jan 20 '14 at 17:31
  • @DavidHeffernan: Of old i used `_com_error` as just a convenient VC way to call `FormatMessage`, so it certainly does that (directly or indirectly), with no `IErrorInfo` in the picture, which works for most HRESULT values. On the other hand I didn't think this through, that it could even do what you mention, so thanks! But on the 3rd hand, as I recall those "intrinsic" VC support classes are not very well documented, so whatever it does it not necessarily guaranteed. – Cheers and hth. - Alf Jan 21 '14 at 06:50

1 Answers1

2

Updated.

Try to QueryInterface the failing object for the ISupportErrorInfo interface, then call the InterfaceSupportsErrorInfo method with the REFIID of the interface throwing the exception. If it returns S_OK, then just calls the GetErrorInfo function to get an IErrorInfo interface. Then use the GetDescription method.

manuell
  • 7,528
  • 5
  • 31
  • 58