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.

210 lines
5.5 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. // All rights reserved.
  5. //
  6. // File Name:
  7. // msg.c
  8. //
  9. // Description:
  10. // This file contains the low-level error reporting routines.
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "pch.h"
  14. #define MAX_ERROR_MSG_LEN 1024
  15. #define MAX_ERROR_CAPTION_LEN 64
  16. #define MAJORTYPE_MASK 0xff
  17. static TCHAR *StrError = NULL;
  18. int
  19. ReportErrorLow(
  20. HWND hwnd,
  21. DWORD dwMsgType,
  22. LPTSTR lpMessageStr,
  23. va_list arglist);
  24. int
  25. __cdecl
  26. ReportErrorId(
  27. HWND hwnd, // calling window
  28. DWORD dwMsgType, // combo of MSGTYPE_*
  29. UINT StringId,
  30. ...)
  31. {
  32. int iRet;
  33. va_list arglist;
  34. TCHAR *Str;
  35. Str = MyLoadString(StringId);
  36. if ( Str == NULL ) {
  37. AssertMsg(FALSE, "Invalid StringId");
  38. return IDCANCEL;
  39. }
  40. va_start(arglist, StringId);
  41. iRet = ReportErrorLow(hwnd,
  42. dwMsgType,
  43. Str,
  44. arglist);
  45. va_end(arglist);
  46. free(Str);
  47. return iRet;
  48. }
  49. //---------------------------------------------------------------------------
  50. //
  51. // Function: ReportErrorLow
  52. //
  53. // Purpose: This is the routine to report errors to the user for the
  54. // Setup Manager wizard.
  55. //
  56. // Arguments:
  57. // HWND hwnd - calling window
  58. // DWORD dwMsgType - combo of MSGTYPE_* (see supplib.h)
  59. // LPTSTR lpMessageStr - message string
  60. // va_list arglist - args to expand
  61. //
  62. // Returns:
  63. // Whatever MessageBox returns.
  64. //
  65. //---------------------------------------------------------------------------
  66. int
  67. ReportErrorLow(
  68. HWND hwnd, // calling window
  69. DWORD dwMsgType, // combo of MSGTYPE_*
  70. LPTSTR lpMessageStr, // passed to sprintf
  71. va_list arglist)
  72. {
  73. DWORD dwLastError;
  74. DWORD dwMajorType;
  75. TCHAR MessageBuffer[MAX_ERROR_MSG_LEN] = _T("");
  76. TCHAR CaptionBuffer[MAX_ERROR_CAPTION_LEN] = _T("");
  77. DWORD dwMessageBoxFlags;
  78. HRESULT hrPrintf;
  79. //
  80. // Hurry and get the last error before it changes.
  81. //
  82. if ( dwMsgType & MSGTYPE_WIN32 )
  83. dwLastError = GetLastError();
  84. if( StrError == NULL )
  85. {
  86. StrError = MyLoadString( IDS_ERROR );
  87. }
  88. //
  89. // Caller must specify _err or _warn or _yesno or _retrycancel, and
  90. // Caller must specify only one of them
  91. //
  92. // Note, we reserved 8 bits for the "MajorType".
  93. //
  94. dwMajorType = dwMsgType & MAJORTYPE_MASK;
  95. if ( dwMajorType != MSGTYPE_ERR &&
  96. dwMajorType != MSGTYPE_WARN &&
  97. dwMajorType != MSGTYPE_YESNO &&
  98. dwMajorType != MSGTYPE_RETRYCANCEL ) {
  99. AssertMsg(FALSE, "Invalid MSGTYPE");
  100. }
  101. //
  102. // Expand the string and varargs the caller might have passed in.
  103. //
  104. if ( lpMessageStr )
  105. hrPrintf=StringCchVPrintf(MessageBuffer, AS(MessageBuffer), lpMessageStr, arglist);
  106. //
  107. // Retrieve the error message for the Win32 error code and suffix
  108. // it onto the callers expanded string.
  109. //
  110. if ( dwMsgType & MSGTYPE_WIN32 ) {
  111. TCHAR *pEndOfBuff = MessageBuffer + lstrlen(MessageBuffer);
  112. hrPrintf=StringCchPrintf(pEndOfBuff, (AS(MessageBuffer)-lstrlen(MessageBuffer)), _T("\r\n\r%s #%d: "), StrError, dwLastError);
  113. pEndOfBuff += lstrlen(pEndOfBuff);
  114. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  115. 0,
  116. dwLastError,
  117. 0,
  118. pEndOfBuff,
  119. (DWORD)(MAX_ERROR_MSG_LEN - (pEndOfBuff-MessageBuffer)),
  120. NULL);
  121. }
  122. if( g_StrWizardTitle == NULL )
  123. {
  124. g_StrWizardTitle = MyLoadString( IDS_WIZARD_TITLE );
  125. }
  126. //
  127. // Set the caption and compute the flags to pass to MessageBox()
  128. //
  129. lstrcpyn( CaptionBuffer, g_StrWizardTitle, AS(CaptionBuffer) );
  130. dwMessageBoxFlags = MB_OK | MB_ICONERROR;
  131. if ( dwMajorType == MSGTYPE_YESNO )
  132. dwMessageBoxFlags = MB_YESNO | MB_ICONQUESTION;
  133. else if ( dwMajorType == MSGTYPE_WARN )
  134. dwMessageBoxFlags = MB_OK | MB_ICONWARNING;
  135. else if ( dwMajorType == MSGTYPE_RETRYCANCEL )
  136. dwMessageBoxFlags = MB_RETRYCANCEL | MB_ICONERROR;
  137. //
  138. // Display the error message
  139. //
  140. return MessageBox(hwnd,
  141. MessageBuffer,
  142. CaptionBuffer,
  143. dwMessageBoxFlags);
  144. }
  145. //---------------------------------------------------------------------------
  146. //
  147. // Function: SetupMgrAssert
  148. //
  149. // Purpose: Reports DBG assertion failures.
  150. //
  151. // Note: Only pass ANSI strings.
  152. // Use the macros in supplib.h.
  153. //
  154. //---------------------------------------------------------------------------
  155. #if DBG
  156. VOID __cdecl SetupMgrAssert(char *pszFile, int iLine, char *pszFormat, ...)
  157. {
  158. char Buffer[MAX_ERROR_MSG_LEN], *pEnd;
  159. va_list arglist;
  160. HRESULT hrPrintf;
  161. if ( pszFormat ) {
  162. va_start(arglist, pszFormat);
  163. hrPrintf=StringCchVPrintfA(Buffer, AS(Buffer), pszFormat, arglist);
  164. va_end(arglist);
  165. }
  166. hrPrintf=StringCchPrintfA(Buffer+strlen(Buffer), MAX_ERROR_MSG_LEN-strlen(Buffer), "\r\nFile: %s\r\nLine: %d", pszFile, iLine);
  167. MessageBoxA(NULL,
  168. Buffer,
  169. "Assertion Failure",
  170. MB_OK);
  171. }
  172. #endif