Source code of Windows XP (NT5)

209 lines
4.1 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. util.cpp
  5. Abstract:
  6. This file implements utility functions.
  7. Environment:
  8. WIN32 User Mode
  9. Author:
  10. Wesley Witt (wesw) 3-Dec-1997
  11. --*/
  12. #include "ntoc.h"
  13. #pragma hdrstop
  14. WCHAR gpszError[] = L"Unknown Error";
  15. void
  16. dprintf(
  17. LPTSTR Format,
  18. ...
  19. )
  20. /*++
  21. Routine Description:
  22. Prints a debug string
  23. Arguments:
  24. format - printf() format string
  25. ... - Variable data
  26. Return Value:
  27. None.
  28. --*/
  29. {
  30. WCHAR buf[1024];
  31. DWORD len;
  32. va_list arg_ptr;
  33. va_start(arg_ptr, Format);
  34. _vsnwprintf(buf, sizeof(buf)/sizeof(buf[0]), Format, arg_ptr);
  35. len = wcslen( buf );
  36. if (buf[len-1] != L'\n') {
  37. buf[len] = L'\r';
  38. buf[len+1] = L'\n';
  39. buf[len+2] = 0;
  40. }
  41. OutputDebugString( buf );
  42. }
  43. VOID
  44. AssertError(
  45. LPTSTR Expression,
  46. LPTSTR File,
  47. ULONG LineNumber
  48. )
  49. /*++
  50. Routine Description:
  51. Thie function is use together with the Assert MACRO.
  52. It checks to see if an expression is FALSE. if the
  53. expression is FALSE, then you end up here.
  54. Arguments:
  55. Expression - The text of the 'C' expression
  56. File - The file that caused the assertion
  57. LineNumber - The line number in the file.
  58. Return Value:
  59. None.
  60. --*/
  61. {
  62. dprintf(
  63. L"Assertion error: [%s] %s @ %d\n",
  64. Expression,
  65. File,
  66. LineNumber
  67. );
  68. __try {
  69. DebugBreak();
  70. } __except (UnhandledExceptionFilter(GetExceptionInformation())) {
  71. // Nothing to do in here.
  72. }
  73. }
  74. /***************************************************************************\
  75. *
  76. * FUNCTION: FmtMessageBox(HWND hwnd, int dwTitleID, UINT fuStyle,
  77. * BOOL fSound, DWORD dwTextID, ...);
  78. *
  79. * PURPOSE: Formats messages with FormatMessage and then displays them
  80. * in a message box
  81. *
  82. * PARAMETERS:
  83. * hwnd - parent window for message box
  84. * fuStyle - MessageBox style
  85. * fSound - if TRUE, MessageBeep will be called with fuStyle
  86. * dwTitleID - Message ID for optional title, "Error" will
  87. * be displayed if dwTitleID == -1
  88. * dwTextID - Message ID for the message box text
  89. * ... - optional args to be embedded in dwTextID
  90. * see FormatMessage for more details
  91. * History:
  92. * 22-Apr-1993 JonPa Created it.
  93. \***************************************************************************/
  94. int
  95. FmtMessageBox(
  96. HWND hwnd,
  97. UINT fuStyle,
  98. BOOL fSound,
  99. DWORD dwTitleID,
  100. DWORD dwTextID,
  101. ...
  102. )
  103. {
  104. LPTSTR pszMsg;
  105. LPTSTR pszTitle;
  106. int idRet;
  107. va_list marker;
  108. va_start(marker, dwTextID);
  109. if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  110. FORMAT_MESSAGE_FROM_HMODULE |
  111. FORMAT_MESSAGE_MAX_WIDTH_MASK,
  112. hInstance,
  113. dwTextID,
  114. 0,
  115. (LPTSTR)&pszMsg,
  116. 1,
  117. &marker)) {
  118. pszMsg = gpszError;
  119. }
  120. va_end(marker);
  121. GetLastError();
  122. pszTitle = NULL;
  123. if (dwTitleID != -1) {
  124. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  125. FORMAT_MESSAGE_FROM_HMODULE |
  126. FORMAT_MESSAGE_MAX_WIDTH_MASK |
  127. FORMAT_MESSAGE_ARGUMENT_ARRAY,
  128. hInstance,
  129. dwTitleID,
  130. 0,
  131. (LPTSTR)&pszTitle,
  132. 1,
  133. NULL);
  134. //(va_list *)&pszTitleStr);
  135. }
  136. //
  137. // Turn on the beep if requested
  138. //
  139. if (fSound) {
  140. MessageBeep(fuStyle & (MB_ICONASTERISK | MB_ICONEXCLAMATION |
  141. MB_ICONHAND | MB_ICONQUESTION | MB_OK));
  142. }
  143. idRet = MessageBox(hwnd, pszMsg, pszTitle, fuStyle);
  144. if (pszTitle != NULL)
  145. LocalFree(pszTitle);
  146. if (pszMsg != gpszError)
  147. LocalFree(pszMsg);
  148. return idRet;
  149. }