Carl
Carl

Reputation: 1

Getting HRESULT error text for an OLE object exception

I have an HRESULT scode (0x800A0034) in an EXCEPINFO structure from an IDispatch::Invoke() call on an OLE Object, and I'm trying to get the text associated with this error ("Bad file name or number").

Using the usual FormatMessage() function with FORMAT_MESSAGE_FROM_SYSTEM doesn't return any text. E.g.

int main()
{
   HRESULT hr = 0x800A0034;
   LPWSTR lpMsgBuf = nullptr;

   DWORD dwChars = FormatMessage(
      FORMAT_MESSAGE_ALLOCATE_BUFFER |
      FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_IGNORE_INSERTS,
      nullptr,
      hr,
      MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
      reinterpret_cast<LPWSTR>( &lpMsgBuf ),
      0,
      nullptr );

   if ( dwChars > 0 )
   {
      std::wcout << L"Error message: " << lpMsgBuf << std::endl;
   }
   else
   {
      std::cerr << "Failed to get error message for HRESULT " << hr << std::endl;
   }

   LocalFree( lpMsgBuf );

   return 0;
}

Looking at the HRESULT further it's related to the FACILITY_CONTROL facility with a error code of 52 as defined in OleCtl.h (CTL_E_BADFILENAMEORNUMBER).

So, does anyone know the API for getting FACILITY_CONTROL error message text from Windows at runtime?

Upvotes: 0

Views: 162

Answers (1)

Ian Boyd
Ian Boyd

Reputation: 256671

COM is standardized on the use of an HRESULT return type for method calls. The HRESULT is the language-agnostic way to return errors—which other languages might return to the caller through an exception.

But COM also allows the "exception thrower" to pass back rich error information.

They create an IErrorInfo object, which provides a Description property:

HRESULT GetDescription([out] BSTR *pBstrDescription);

They then store this error information using SetErrorInfo.

You then call GetErrorInfo to retrieve that IErrorInfo object, and read the description:

String GetErrorInfoDescription
{
   IErrorInfo errorInfo;
   GetErrorInfo(0, out errorInfo);

   String description;
   errorInfo.GetDescription(out description);

   return description;
}

Upvotes: 0

Related Questions