ErrorManager, errors and warnings, ShowCOMError and ReturnAndShowCOMError

Top  Previous  Next

In InvSuppLib the file InvErrorHandling.cpp contains the error handling functions I use.


An example of an error message would be if you tried to create a circle in a sketch with negative diameter. You'll not get a nice "bad radius" message from COM, just an "invalid parameter" message.


Have a look at the code for ShowCOMError. Not only does it give you dialog with the the text of the error code, but sometimes it gives you an extra hint about what caused the error. I add those hints as, day by day, I understand them. You can do the same.


void ShowCOMError (HRESULT hRes, const wchar_t* const pszExtraMsg, ...) ;

HRESULT ReturnAndShowCOMError (HRESULT hRes, const wchar_t* const pszExtraMsg=nullptr) ;

void ShowInventorErrors () ;


Note also that the first function has trailing parameters ("..."), which means it can be used just like printf. Note the %d and %s in the following call:


ShowComError (hRes,

              L"The doodah called %d with diameter %.1fmm failed",

              pszDooDahName,kDooDahDiameter) ;


Extra tip: When using a COM get_ function you often pass the address of a pointer, which becomes set by the get_ function, and the get_ function also returns a result code (an HRESULT) remember to check both of them. For example the HRESULT may be S_OK (the function went well) but the pointer comes back nullptr (the object you were looking for does not exist).


   CComPtr<FeaturePatternElements> pPatternElements ;

   HRESULT hRes = pRecPattern->get_PatternElements (&pPatternElements) ;

   if (FAILED(hRes) || (pPatternElements == nullptr)) { // TWO CHECKS

       ShowCOMError (hRes,L"get_PatternElements failed") ;

       return false ;



With some get_ functions you don't need to check the returned pointer, but because I have a bad memory I often simply always check the pointer as well as the hRes.


See also HRESULT codes.








Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)