Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.7 KiB

  1. /*** try_catch.h - Error processing through structured exceptions
  2. *
  3. * Copyright (c) 1998-1999 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * Author: Yan Leshinsky (YanL)
  6. * Created 10/08/98
  7. *
  8. * MODIFICATION HISTORY
  9. */
  10. #pragma once
  11. #pragma warning( disable : 4102 ) // many of the labels will remain unreferenced
  12. inline HRESULT MakeHRESULT(int iRet) { return HRESULT_FROM_WIN32(iRet); }
  13. inline HRESULT MakeHRESULT(LONG lRet) { return HRESULT_FROM_WIN32(lRet); }
  14. inline HRESULT MakeHRESULT(DWORD lRet) { return HRESULT_FROM_WIN32(lRet); }
  15. #ifdef USE_EXCEPTIONS
  16. #define try_block try { HRESULT __hr = S_OK;
  17. #define catch_all } catch (HRESULT){}
  18. #define catch_return } catch (HRESULT __hr){ return __hr; }
  19. #define catch_set(hr) } catch (HRESULT __hr){ hr = __hr; }
  20. #define throw_now throw __hr
  21. // Compiler com support
  22. inline void __stdcall _com_issue_error(HRESULT hr) { throw hr; }
  23. #else
  24. #define try_block { HRESULT __hr = S_OK;
  25. #define catch_all catch_block: ; }
  26. #define catch_return catch_block: return __hr; }
  27. #define catch_set(hr) catch_block: hr = __hr; }
  28. #define throw_now goto catch_block
  29. // Disable throw in compiler com support
  30. inline void __stdcall _com_issue_error(HRESULT hr) {}
  31. #endif
  32. #ifdef _DEBUG
  33. // Print erroneous line
  34. #define THIS_FILE _T(__FILE__)
  35. inline void __dump(LPCTSTR szFileName, int nLine, LPCTSTR szPrompt, HRESULT hr) {
  36. TCHAR szMsg[ 1024 ];
  37. TCHAR szErr[ 256 ];
  38. DWORD cMsgLen = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, (DWORD)hr, LANG_NEUTRAL, szErr, sizeof(szErr), NULL);
  39. wsprintf(szMsg, _T("ERROR File %hs, Line %d\r\n\t\t\t%s\r\n\t\t\t0x%08X\t%s"), szFileName, nLine, szPrompt, (DWORD)hr,
  40. cMsgLen ? szErr : _T("Unknown error") );
  41. OutputDebugString(szMsg);
  42. }
  43. #else
  44. #define __dump(szFileName, nLine, szPrompt, hr)
  45. #endif /* _DEBUG */
  46. // If return code FAILED throw it
  47. #define throw_hr(f) { __hr = (f); if (FAILED(__hr)) { __dump(THIS_FILE, __LINE__, #f, __hr); throw_now; } }
  48. // If return code isn't ERROR_SUCCESS convert it to HRESULT and throw it
  49. #define throw_err(f) { __hr = MakeHRESULT(f); if (FAILED(__hr)) { __dump(THIS_FILE, __LINE__, #f, __hr); throw_now; } }
  50. // If return code false throw last error
  51. #define throw_flast(f) if (!(f)) { __hr = MakeHRESULT(GetLastError()); __dump(THIS_FILE, __LINE__, #f, __hr); throw_now; }
  52. // If return code false throw specific HRESULT
  53. #define throw_fhr(f, hr) if (!(f)) { __hr = hr; __dump(THIS_FILE, __LINE__, #f, __hr); throw_now; }
  54. // If return code false throw specific Win32 error
  55. #define throw_ferr(f, err) if (!(f)) { __hr = MakeHRESULT(err); __dump(THIS_FILE, __LINE__, #f, __hr); throw_now; }
  56. #define throw_fmem(f) throw_fhr(f, E_OUTOFMEMORY)
  57. #define throw_fpar(f) throw_fhr(f, E_INVALIDARG)