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.

213 lines
4.5 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. // make sure we have room to write past the length of the string
  36. buf[sizeof(buf)/sizeof(buf[0])-3] = L'\0';
  37. len = wcslen( buf );
  38. if (len > 0) {
  39. if (buf[len-1] != L'\n') {
  40. buf[len] = L'\r';
  41. buf[len+1] = L'\n';
  42. buf[len+2] = 0;
  43. }
  44. OutputDebugString( buf );
  45. }
  46. }
  47. VOID
  48. AssertError(
  49. LPTSTR Expression,
  50. LPTSTR File,
  51. ULONG LineNumber
  52. )
  53. /*++
  54. Routine Description:
  55. Thie function is use together with the Assert MACRO.
  56. It checks to see if an expression is FALSE. if the
  57. expression is FALSE, then you end up here.
  58. Arguments:
  59. Expression - The text of the 'C' expression
  60. File - The file that caused the assertion
  61. LineNumber - The line number in the file.
  62. Return Value:
  63. None.
  64. --*/
  65. {
  66. dprintf(
  67. L"Assertion error: [%s] %s @ %d\n",
  68. Expression,
  69. File,
  70. LineNumber
  71. );
  72. __try {
  73. DebugBreak();
  74. } __except (UnhandledExceptionFilter(GetExceptionInformation())) {
  75. // Nothing to do in here.
  76. }
  77. }
  78. /***************************************************************************\
  79. *
  80. * FUNCTION: FmtMessageBox(HWND hwnd, int dwTitleID, UINT fuStyle,
  81. * BOOL fSound, DWORD dwTextID, ...);
  82. *
  83. * PURPOSE: Formats messages with FormatMessage and then displays them
  84. * in a message box
  85. *
  86. * PARAMETERS:
  87. * hwnd - parent window for message box
  88. * fuStyle - MessageBox style
  89. * fSound - if TRUE, MessageBeep will be called with fuStyle
  90. * dwTitleID - Message ID for optional title, "Error" will
  91. * be displayed if dwTitleID == -1
  92. * dwTextID - Message ID for the message box text
  93. * ... - optional args to be embedded in dwTextID
  94. * see FormatMessage for more details
  95. * History:
  96. * 22-Apr-1993 JonPa Created it.
  97. \***************************************************************************/
  98. int
  99. FmtMessageBox(
  100. HWND hwnd,
  101. UINT fuStyle,
  102. BOOL fSound,
  103. DWORD dwTitleID,
  104. DWORD dwTextID,
  105. ...
  106. )
  107. {
  108. LPTSTR pszMsg;
  109. LPTSTR pszTitle;
  110. int idRet;
  111. va_list marker;
  112. va_start(marker, dwTextID);
  113. if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  114. FORMAT_MESSAGE_FROM_HMODULE |
  115. FORMAT_MESSAGE_MAX_WIDTH_MASK,
  116. hInstance,
  117. dwTextID,
  118. 0,
  119. (LPTSTR)&pszMsg,
  120. 1,
  121. &marker)) {
  122. pszMsg = gpszError;
  123. }
  124. va_end(marker);
  125. GetLastError();
  126. pszTitle = NULL;
  127. if (dwTitleID != -1) {
  128. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  129. FORMAT_MESSAGE_FROM_HMODULE |
  130. FORMAT_MESSAGE_MAX_WIDTH_MASK |
  131. FORMAT_MESSAGE_ARGUMENT_ARRAY,
  132. hInstance,
  133. dwTitleID,
  134. 0,
  135. (LPTSTR)&pszTitle,
  136. 1,
  137. NULL);
  138. //(va_list *)&pszTitleStr);
  139. }
  140. //
  141. // Turn on the beep if requested
  142. //
  143. if (fSound) {
  144. MessageBeep(fuStyle & (MB_ICONASTERISK | MB_ICONEXCLAMATION |
  145. MB_ICONHAND | MB_ICONQUESTION | MB_OK));
  146. }
  147. idRet = MessageBox(hwnd, pszMsg, pszTitle, fuStyle);
  148. if (pszTitle != NULL)
  149. LocalFree(pszTitle);
  150. if (pszMsg != gpszError)
  151. LocalFree(pszMsg);
  152. return idRet;
  153. }