Leaked source code of windows server 2003
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.

65 lines
1.9 KiB

  1. //--------------------------------------------------------------------
  2. // ErrorHandling - implementation
  3. // Copyright (C) Microsoft Corporation, 2001
  4. //
  5. // Created by: Duncan Bryce (duncanb), 11-11-2001
  6. //
  7. #include "pch.h"
  8. //--------------------------------------------------------------------
  9. HRESULT GetSystemErrorString(HRESULT hrIn, WCHAR ** pwszError) {
  10. HRESULT hr=S_OK;
  11. DWORD dwResult;
  12. WCHAR * rgParams[2]={
  13. NULL,
  14. (WCHAR *)(ULONG_PTR)hrIn
  15. };
  16. // must be cleaned up
  17. WCHAR * wszErrorMessage=NULL;
  18. WCHAR * wszFullErrorMessage=NULL;
  19. // initialize input params
  20. *pwszError=NULL;
  21. // get the message from the system
  22. dwResult=FormatMessage(
  23. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  24. NULL/*ignored*/, hrIn, 0/*language*/, (WCHAR *)&wszErrorMessage, 0/*min-size*/, NULL/*valist*/);
  25. if (0==dwResult) {
  26. if (ERROR_MR_MID_NOT_FOUND==GetLastError()) {
  27. rgParams[0]=L"";
  28. } else {
  29. _JumpLastError(hr, error, "FormatMessage");
  30. }
  31. } else {
  32. rgParams[0]=wszErrorMessage;
  33. // trim off \r\n if it exists
  34. if (L'\r'==wszErrorMessage[wcslen(wszErrorMessage)-2]) {
  35. wszErrorMessage[wcslen(wszErrorMessage)-2]=L'\0';
  36. }
  37. }
  38. // add the error number
  39. dwResult=FormatMessage(
  40. FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  41. L"%1 (0x%2!08X!)", 0, 0/*language*/, (WCHAR *)&wszFullErrorMessage, 0/*min-size*/, (va_list *)rgParams);
  42. if (0==dwResult) {
  43. _JumpLastError(hr, error, "FormatMessage");
  44. }
  45. // success
  46. *pwszError=wszFullErrorMessage;
  47. wszFullErrorMessage=NULL;
  48. hr=S_OK;
  49. error:
  50. if (NULL!=wszErrorMessage) {
  51. LocalFree(wszErrorMessage);
  52. }
  53. if (NULL!=wszFullErrorMessage) {
  54. LocalFree(wszFullErrorMessage);
  55. }
  56. return hr;
  57. }