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.

238 lines
5.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: msgbox.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #pragma hdrstop
  12. #include "msgbox.h"
  13. //
  14. // Helper to report system errors.
  15. // Merely ensures a little consistency with respect to message box
  16. // flags.
  17. //
  18. // Example:
  19. //
  20. // CscWin32Message(hwndMain,
  21. // ERROR_NOT_ENOUGH_MEMORY,
  22. // CSCUI::SEV_ERROR);
  23. //
  24. int
  25. CscWin32Message(
  26. HWND hwndParent,
  27. DWORD dwError, // From GetLastError().
  28. CSCUI::Severity severity
  29. )
  30. {
  31. UINT uType = MB_OK;
  32. switch(severity)
  33. {
  34. case CSCUI::SEV_ERROR:
  35. uType |= MB_ICONERROR;
  36. break;
  37. case CSCUI::SEV_WARNING:
  38. uType |= MB_ICONWARNING;
  39. break;
  40. case CSCUI::SEV_INFORMATION:
  41. uType |= MB_ICONINFORMATION;
  42. break;
  43. default: break;
  44. }
  45. return CscMessageBox(hwndParent, uType, Win32Error(dwError));
  46. }
  47. //
  48. // Display a system error message in a message box.
  49. // The Win32Error class was created to eliminate any signature ambiguities
  50. // with the other versions of CscMessageBox.
  51. //
  52. // Example:
  53. //
  54. // CscMessageBox(hwndMain,
  55. // MB_OK | MB_ICONERROR,
  56. // Win32Error(ERROR_NOT_ENOUGH_MEMORY));
  57. //
  58. int
  59. CscMessageBox(
  60. HWND hwndParent,
  61. UINT uType,
  62. const Win32Error& error
  63. )
  64. {
  65. int iResult = -1;
  66. LPTSTR pszBuffer = NULL;
  67. FormatSystemError(&pszBuffer, error.Code());
  68. if (NULL != pszBuffer)
  69. {
  70. iResult = CscMessageBox(hwndParent, uType, pszBuffer);
  71. LocalFree(pszBuffer);
  72. }
  73. return iResult;
  74. }
  75. //
  76. // Display a system error message in a message box with additional
  77. // text.
  78. //
  79. // Example:
  80. //
  81. // CscMessageBox(hwndMain,
  82. // MB_OK | MB_ICONERROR,
  83. // Win32Error(ERROR_NOT_ENOUGH_MEMORY),
  84. // IDS_FMT_LOADINGFILE,
  85. // pszFile);
  86. //
  87. int
  88. CscMessageBox(
  89. HWND hwndParent,
  90. UINT uType,
  91. const Win32Error& error,
  92. LPCTSTR pszMsgText
  93. )
  94. {
  95. int iResult = -1;
  96. size_t cchMsg = lstrlen(pszMsgText) + MAX_PATH;
  97. LPTSTR pszBuffer = (LPTSTR)LocalAlloc(LMEM_FIXED, cchMsg * sizeof(TCHAR));
  98. if (NULL != pszBuffer)
  99. {
  100. LPTSTR pszRemaining;
  101. HRESULT hr = StringCchCopyEx(pszBuffer, cchMsg, pszMsgText, &pszRemaining, &cchMsg, 0);
  102. // We allocated a big enough buffer, so this should never fail
  103. ASSERT(SUCCEEDED(hr));
  104. hr = StringCchCopyEx(pszRemaining, cchMsg, TEXT("\n\n"), &pszRemaining, &cchMsg, 0);
  105. ASSERT(SUCCEEDED(hr));
  106. int cchLoaded = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  107. NULL,
  108. error.Code(),
  109. 0,
  110. pszRemaining,
  111. cchMsg,
  112. NULL);
  113. if (0 != cchLoaded)
  114. {
  115. iResult = CscMessageBox(hwndParent, uType, pszBuffer);
  116. }
  117. LocalFree(pszBuffer);
  118. }
  119. return iResult;
  120. }
  121. int
  122. CscMessageBox(
  123. HWND hwndParent,
  124. UINT uType,
  125. const Win32Error& error,
  126. HINSTANCE hInstance,
  127. UINT idMsgText,
  128. va_list *pargs
  129. )
  130. {
  131. int iResult = -1;
  132. LPTSTR pszBuffer = NULL;
  133. if (0 != vFormatStringID(&pszBuffer, hInstance, idMsgText, pargs))
  134. {
  135. iResult = CscMessageBox(hwndParent, uType, error, pszBuffer);
  136. }
  137. LocalFree(pszBuffer);
  138. return iResult;
  139. }
  140. int
  141. CscMessageBox(
  142. HWND hwndParent,
  143. UINT uType,
  144. const Win32Error& error,
  145. HINSTANCE hInstance,
  146. UINT idMsgText,
  147. ...
  148. )
  149. {
  150. va_list args;
  151. va_start(args, idMsgText);
  152. int iResult = CscMessageBox(hwndParent, uType, error, hInstance, idMsgText, &args);
  153. va_end(args);
  154. return iResult;
  155. }
  156. //
  157. // Example:
  158. //
  159. // CscMessageBox(hwndMain,
  160. // MB_OK | MB_ICONWARNING,
  161. // TEXT("File %1 could not be deleted"), pszFilename);
  162. //
  163. int
  164. CscMessageBox(
  165. HWND hwndParent,
  166. UINT uType,
  167. HINSTANCE hInstance,
  168. UINT idMsgText,
  169. va_list *pargs
  170. )
  171. {
  172. int iResult = -1;
  173. LPTSTR pszMsg = NULL;
  174. if (0 != vFormatStringID(&pszMsg, hInstance, idMsgText, pargs))
  175. {
  176. iResult = CscMessageBox(hwndParent, uType, pszMsg);
  177. }
  178. LocalFree(pszMsg);
  179. return iResult;
  180. }
  181. int
  182. CscMessageBox(
  183. HWND hwndParent,
  184. UINT uType,
  185. HINSTANCE hInstance,
  186. UINT idMsgText,
  187. ...
  188. )
  189. {
  190. va_list args;
  191. va_start(args, idMsgText);
  192. int iResult = CscMessageBox(hwndParent, uType, hInstance, idMsgText, &args);
  193. va_end(args);
  194. return iResult;
  195. }
  196. //
  197. // All of the other variations of CscMessageBox() end up calling this one.
  198. //
  199. int
  200. CscMessageBox(
  201. HWND hwndParent,
  202. UINT uType,
  203. LPCTSTR pszMsgText
  204. )
  205. {
  206. TCHAR szCaption[80] = { TEXT('\0') };
  207. LoadString(g_hInstance, IDS_APPLICATION, szCaption, ARRAYSIZE(szCaption));
  208. return MessageBox(hwndParent, pszMsgText, szCaption, uType);
  209. }